UWP使用SQLite创建数据库

时间:2017-04-22 23:09:44

标签: c# sqlite uwp

我试图在我的UWP应用程序中使用SQLite创建数据库及其表。我的代码没有给我任何错误,但我无法找到我的数据库文件。

SqliteEngine.UseWinSqlite3(); //Configuring library to use SDK version of SQLite
        using (SqliteConnection db = new SqliteConnection("Filename=sqliteSample.db"))
        {
            db.Open();
            // Resume Table
            String resumeTable = "CREATE TABLE IF NOT EXISTS Resume (resumeId INTEGER PRIMARY KEY AUTOINCREMENT, educationInfoId INTEGER NULL, personalInfoId INTEGER NULL, userId INTEGER, certInfoId INTEGER NULL, workInfoId INTEGER NULL)";
            SqliteCommand createResumeTable = new SqliteCommand(resumeTable, db);

            // PersonalInfo Table
            String personalInfoTable = "CREATE TABLE IF NOT EXISTS PersonalInfo (personalInfoId INTEGER PRIMARY KEY AUTOINCREMENT, userId INTEGER, city VARCHAR(100) NULL, email VARCHAR(50) NULL, name VARCHAR(100) NULL, state VARCHAR(10) NULL)";
            SqliteCommand createPersonalInfoTable = new SqliteCommand(personalInfoTable, db);
            try
            {
                createResumeTable.ExecuteReader();
                createPersonalInfoTable.ExecuteReader();
            }
            catch (SqliteException e)
            {
                Console.WriteLine(e.ToString());
            }
        }

代码中有问题吗?数据库文件应该在哪里?

由于

3 个答案:

答案 0 :(得分:1)

您可以在nuget中使用SqlitePCL。 http://sqlite.org/download.html

下载页面具有UAP软件,您应该下载它。

下载文件是VSIX,你应该安装它。

打开工具 - >扩展程序和更新... - >已安装 - > SDK可以看到

enter image description here

重新启动visual studio并右键单击您的项目References –> Add Reference 并添加sqlite

enter image description here

打开nuget搜索SQLitePCL。

enter image description here

有一些sql字符串

    private static String DB_NAME = "SQLiteSample.db";
    private static String TABLE_NAME = "SampleTable";
    private static String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (Key TEXT,Value TEXT);";
    private static String SQL_QUERY_VALUE = "SELECT Value FROM " + TABLE_NAME + " WHERE Key = (?);";
    private static String SQL_INSERT = "INSERT INTO " + TABLE_NAME + " VALUES(?,?);";


    private static String SQL_UPDATE = "UPDATE " + TABLE_NAME + " SET Value = ? WHERE Key = ?";
    private static String SQL_DELETE = "DELETE FROM " + TABLE_NAME + " WHERE Key = ?"

创建表

_connection = new SQLiteConnection(DB_NAME);
using (var statement = _connection.Prepare(SQL_CREATE_TABLE))
{
    statement.Step();
}

INSERT

using (var statement = _connection.Prepare(SQL_INSERT))
{
    statement.Bind(1, key);
    statement.Bind(2, value);
    statement.Step();
}

DELETE

using (var statement = _connection.Prepare(SQL_DELETE))
{
    statement.Bind(1, key);
    statement.Step();
}

更新

using(var statement = _connection.Prepare(SQL_UPDATE))
{
    statement.Bind(1, value);
    statement.Bind(2, key);
    statement.Step();
}

请参阅http://www.cnblogs.com/ms-uap/p/4798269.html

答案 1 :(得分:0)

看起来代码是正确的。我刚刚验证过。

除非你做了一些自定义的事情,否则默认位置应为:

C:\ Users \“您的用户名”\ AppData \ Local \ Packages \“您的应用包”\ LocalState \ sqliteSample.db

答案 2 :(得分:0)

  1. 下载适用于通用应用平台扩展的SQLite - >添加参考。
  2. 安装nuget pack - SQLite.Net-PCL。
  3. >

    private static string dbPath = string.Empty;
        private static string DBPath(string fileName)
        {
            if (string.IsNullOrEmpty(dbPath))
            {
                dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, fileName);
            }
            return dbPath;
        }
        /// <summary>
        /// Get fileName From LocalFolder. Please Create first a dataBase.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static SQLiteConnection DbConnection(string fileName)
        {
            return new SQLiteConnection(new SQLitePlatformWinRT(), DBPath(fileName));
        }
    
相关问题