Database
  Home arrow Database arrow Page 4 - Using SQLite for Simple Database Storage
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Using SQLite for Simple Database Storage
By: Clay Dowling
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 24
    2004-10-19

    Table of Contents:
  • Using SQLite for Simple Database Storage
  • SQLite in Action
  • An Example User Object
  • Saving a Record
  • Storing it in the Database
  • The Last Step

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using SQLite for Simple Database Storage - Saving a Record


    (Page 4 of 6 )

    Saving a record is slightly more complicated, because the query can be either an update or an insert.  In the case of an insert, it's important for us to retrieve the value of the newly created primary key.  SQLite has an SQL function called LAST_INSERT_ROWID() which will return this value for us.

    Public Sub Save()

      Dim Query As New SQLiteCommand()

      Query.Connection = Conn
      If Id > 0 Then
        Query.CommandText = "UPDATE user SET name=?, email=? WHERE id=?"
        Query.CreateAndAddUnnamedParameters()
        Query.Parameters(0).Value = Name
        Query.Parameters(1).Value = Email
        Query.Parameters(2).Value = Id
        Query.Prepare()
        Query.ExecuteNonQuery()
      Else
        Query.CommandText = "INSERT INTO user (name, email, password) " _
                            + "VALUES (?, ?, '*')"
        Query.CreateAndAddUnnamedParameters()
        Query.Parameters(0).Value = Name
        Query.Parameters(1).Value = Email
        Query.Prepare
        Query.ExecuteNonQuery()
        Query.CommandText = "SELECT LAST_INSERT_ROWID()"
        Id = Query.ExecuteScalar
      End If

    End Sub

    The last of our storage methods is Delete().

    Public Sub Delete()

      Dim Query As New SQLiteCommand("DELETE FROM user WHERE id=?", Conn)
     
      Query.CreateAndAddUnnamedParameters()
      Query.Parameters(0).Value = Id
      Query.Prepare()
      Query.ExecuteNonQuery()

    End Sub

    For the sake of completeness, I'll also show the implementations of the methods for password and account verification handling.  The first function that's necessary is the password setting function.  Passwords will be stored as md5 encoded strings, so even if the contents of the user table are compromised, password information will not be divulged.  In VB.NET, this is a two step process.  First, the password must be encoded:

    Private Function HashPassword(ByVal s As String) As String

      Dim tmpSource As Byte()
      Dim tmpHash As Byte()

      tmpSource = ASCIIEncoding.ASCII.GetBytes(s)
      tmpHash = New Cryptography.MD5CryptoServiceProvider.ComputeHash(tmpSource)
     
      Return BitConverter.ToString(tmpHash)

    End Function

    The result of this function is a 47 character string which consists of the hexadecimal representation of the MD5 hash of our password.  Each hexadecimal number is separated by dashes.

    More Database Articles
    More By Clay Dowling


       · Hello!Great article here. SQLite really is a simple, fast and powerful database...
       · Its a very helpful article...I have been looking for database designed with...
     

    DATABASE ARTICLES

    - Manage Projects with SQL Server Management S...
    - Query Editing and Regular Expressions with S...
    - Using SQL Server Management Studio Tools
    - SQL Server Management Studio
    - Exporting a MySQL Database to Excel Using OD...
    - Controlling Databases with SQL Server 2005 D...
    - Using Recovery Models with SQL Server 2005 D...
    - Handling Database Properties for the SQL Ser...
    - Managing Permissions with the SQL Server 200...
    - SQL Server 2005 Database Engine Security
    - Administering SQL Server 2005 Database Engine
    - Building Applications with Anonymous Types
    - A Closer Look at Anonymous Types
    - Programming with Anonymous Types
    - Converting Your Excel Worksheet into a Worki...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek