2012年5月14日 星期一

SQLite for PC , for mobile

首先到
下載ADO.net  for sqlite
SQLiteBrowser

在PC 上面操作是沒有什麼問題!


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim filename As String = Application.StartupPath + "\Test.db3"
        If Not File.Exists(filename) Then
            SQLite.SQLiteConnection.CreateFile(filename)
        End If


        Dim oConn As New SQLiteConnection("Data Source=" + filename)
        Try
            oConn.Open()
            '執行SQL指令
            Dim zSQL As String = vbNullString
            Dim zWord As String = vbNullString
            Dim zDeno As String = vbNullString
            Dim oCmd As SQLiteCommand = Nothing
            zSQL = "CREATE TABLE  IF NOT EXISTS coffees (  F1 TEXT PRIMARY KEY,  F2 TEXT );"
            oCmd = New SQLiteCommand(zSQL, oConn)
            oCmd.ExecuteNonQuery()




            For i = 0 To 4
                zWord = "1234" + CStr(i)
                zDeno = "5678"
                zSQL = "INSERT INTO coffees (F1,F2) VALUES ('" & zWord & "','" & zDeno & "')"
                oCmd = New SQLiteCommand(zSQL, oConn)
                oCmd.ExecuteNonQuery()
            Next


            Label1.Text = "Data inserted ready!!!"
            oCmd.Dispose()
            oConn.Close()
        Catch ex As Exception
            Label1.Text = ex.Message
        End Try
    End Sub

在mobile 
要參考C:\Program Files (x86)\SQLite.NET\bin\CompactFramework\SQLite.Interop.066.DLL
不過在這邊卻踢到鐵板,看了一下錯誤訊息,就自己複製了一個DLL把檔名改成
System.Data.SQLite.dll結果就可以了

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mypath As String = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase.ToString)
        mypath += "\Test.db3"


        If Not File.Exists(mypath) Then
            SQLiteConnection.CreateFile(mypath)
        End If


        Dim oConn As New SQLiteConnection("Data Source=" + mypath)
        Try
            oConn.Open()
            '執行SQL指令
            Dim zSQL As String = vbNullString
            Dim zWord As String = vbNullString
            Dim zDeno As String = vbNullString
            Dim oCmd As SQLiteCommand = Nothing
            zSQL = "CREATE TABLE  IF NOT EXISTS coffees (  F1 TEXT PRIMARY KEY,  F2 TEXT );"
            oCmd = New SQLiteCommand(zSQL, oConn)
            oCmd.ExecuteNonQuery()


            For i = 0 To 1000
                zWord = "1234" + CStr(i)
                zDeno = "5678"
                zSQL = "INSERT INTO coffees (F1,F2) VALUES ('" & zWord & "','" & zDeno & "')"
                oCmd = New SQLiteCommand(zSQL, oConn)
                oCmd.ExecuteNonQuery()
            Next


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try


以上兩個code是參考別人網站的,在mobile操作時,要把SQLite.Interop.dll直接複製到mobile裡面。
小歐參考網站