Sqlite,数据库连接和windows phone 8.1(silverlight)

问题描述 投票:1回答:1

我最近开始了一个新的工作,作为软件开发人员/专家,我目前正在升级维护一个针对Windows ce的应用程序(我知道,我知道)

我目前主要处理我的数据库连接,我找到了一个N​​uget包(sqlite-net),但是我找不到关于如何使用它的文档。在sqlite3类下有一个名为sqlite_open(string,ByRef db)的函数,这只是让我疯狂。如果您有任何人成功使用此命名空间,我将非常感谢您的帮助。 (通过电话发布,如果你们需要更多细节,可能需要一段时间)

先感谢您!

windows vb.net silverlight windows-phone-8 windows-phone-8.1
1个回答
1
投票

我不确定你的研究是如何进行的,但我曾经用Windows Phone 8,8.1和UWP开发一些应用程序,我知道你正在谈论的库,大部分文档都在诺基亚Wiki上(现已死了)但是,我能够找到一些:

Windows Phone: How to use SQLite in Windows Phone

在为Windows Phone开发时,您需要考虑这个库的一些非常重要的事情:

在标准的Windows Phone 8或Windows Phone 8.1(Silverlight)项目中,您必须添加以下内容:

SILVERLIGHT; WINDOWS_PHONE; USE_WP8_NATIVE_SQLITE

如果您的WP项目中未添加上一行根本不起作用,则会在您的应用程序配置中添加。

这里有一些额外的文档:

Working with SQLite in Windows Phone 8: a sqlite-net version for mobile

Scribd上有官方维基的备份,也许你可以在Wayback Machine找到另一个。

但是让我们关注重要的代码,请记住这是从C#到VB.NET的原始翻译,我不经常使用它并且它没有经过全面测试。我强烈建议您切换到C#,因为有更广泛的文档和示例。

DBManagement类:

Public Class DBManagement
    'DB location
    Public Shared DB_PATH As String = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "sample.sqlite"))

    'DB Connection
    Private dbConn As SQLiteConnection

    'Example of Opening or Closing the DB
    Private Sub OpenOrCreateDB()
        ''' Create the database connection.
        dbConn = new SQLiteConnection(DB_PATH)
        ''' Create the table Task, if it doesn't exist.
        dbConn.CreateTable(Of Task)()

    End Sub

    'Example of Closing the DB
    Private Sub CloseDB()
        If dbConn <> Nothing Then 
            ' Close the database connection.
            dbConn.Close()
        End If
    End Sub

    'Example of a Select Operation
    Private Function List(Of Task) ExecQuery(Query as String)
        Dim sqlCommand = new SQLiteCommand(dbConn)
        '"select * from task where title = 'filter_test'"
        sqlCommand.CommandText = Query
        Return sqlComm.ExecuteQuery(Of Task)()
    End Sub

    'Example of an Insert Operation
    Private Sub Insert(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim task As Task = New Task() With {.Title = TitleField.Text, .Text = TextField.Text, .CreationDate = DateTime.Now}
        dbConn.Insert(task)
    End Sub
End Class

实体表类任务:

Public Class Task

    <PrimaryKey, AutoIncrement>
    Public Property Id As Integer

    Public Property Title As String

    Public Property Text As String

    Public Property CreationDate As DateTime

    Public Overrides Function ToString() As String
        Return Title & ":" & Text & " < " + CreationDate.ToShortDateString() & " " + CreationDate.ToShortTimeString()
    End Function
End Class

希望它可能会给你一些启发。

© www.soinside.com 2019 - 2024. All rights reserved.