2017-04-22 123 views
1

我想在我的UWP應用程序中使用SQLite創建數據庫及其表格。我的代碼不給我任何錯誤,但我找不到我的數據庫文件。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()); 
      } 
     } 

代碼中出現錯誤嗎?數據庫文件應該在哪裏?

謝謝

+0

數據庫位於您在連接字符串中指定的目錄中。 –

回答

1

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

下載頁面有UAP軟件,您應該下載它。

下載文件是VSIX,您應該安裝它。

打開工具 - >擴展和更新 - >安裝 - >軟件開發工具包可以看到

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(); 
} 

UPDATE

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

0

貌似代碼是正確的。我剛剛證實。

除非你已經做了定製,默認位置應該是:

C:\用戶\ 「您的用戶名 」\應用程序數據\本地\包\「 您的應用程序包」 \ LocalState \ sqliteSample.db