[英]How to use an existing sqlite database on windows phone 8 with cordova
[英]How to use SQLite database with the windows phone 7?
我正在開發windows phone 7應用程序。 我是窗口手機7應用程序的新手。 我是Application.GetResourceStream()方法的新手。 我在我的應用程序中使用以下鏈接。
http://wirebear.com/blog/2010/11/12/using-sqlite-in-your-wp7-app
我按照上面鏈接中描述的步驟操作。 我在Application.GetResourceStream()方法面臨問題。 在上面的方法中,我使用以下代碼
Stream src = Application.GetResourceStream(
new Uri(@"/SQLiteConnectivity;component/" + dbName,
UriKind.Relative)).Stream;
我右鍵單擊了我的項目名稱並選擇了屬性,並將程序集名稱設置為“SQLiteConnectivity”。 運行應用程序后,我在Application.GetResourceStream()中收到空引用異常錯誤。 我也不知道應該在Application.GetResourceStream()方法中使用什么而不是'component'。 能否請您提供任何代碼或鏈接或解決上述問題的解決方案?
是的,我得到了答案部分所述的答案。 我通過將數據庫屬性中的“構建操作”設置為“資源”和“復制到輸出目錄”作為“始終復制”來解決上述問題。 但我現在面臨新問題
我在app.xaml.cs中聲明了以下代碼
public partial class App : Application
{
public PhoneApplicationFrame RootFrame { get; private set; }
private DBHelper _db;
public DBHelper db
{
get
{
if (_db == null)
_db = new DBHelper("ExpenseManager.db");
return _db;
}
}
}
並在我的mainpage.xaml.cs中調用它,如下所示
private void button1_Click(object sender, RoutedEventArgs e)
{
// Code runs "for real"
_customerEntries = (Application.Current as App).db.SelectObservableCollection<Category>("SELECT Category_Name FROM Category WHERE Category_ID=1");
textBox1.Text = _customerEntries.ToString();
}
我在mainpage類下面添加了一個類,如下所示
public class Category
{
public int Category_ID { get; set; }
public string Category_Name { get; set; }
}
現在,當我運行我的應用程序時,我在BindAll(ppStmt)的SQLClient.cs的以下函數中點擊按鈕點擊錯誤,因為“文件已加密或不在數據庫中”;
Sqlite3.Vdbe Prepare()
{
Sqlite3.Vdbe ppStmt=new Sqlite3.Vdbe();
if (Sqlite3.sqlite3_prepare_v2(_db, CommandText, CommandText.Length, ref ppStmt, 0) != Sqlite3.SQLITE_OK)
throw new SQLiteException(Sqlite3.sqlite3_errmsg(_db));
BindAll(ppStmt);
return ppStmt;
}
能否請您提供我可以解決上述問題的任何代碼或鏈接? 如果我做錯了什么,請指導我。
您需要驗證數據庫文件的構建屬性是否設置為資源,並且您需要檢查dbName屬性的值以確保它是完整文件的名稱,包括任何文件夾結構。
應用程序的db屬性需要以不同方式設置。 在App.xaml.cs文件中,請更改此行:
public DBHelper db = new DBHelper("DATABASE.s3db");
對此:
private DBHelper _db;
public DBHelper db
{
get
{
if (_db == null)
_db = new DBHelper("DATABASE.s3db");
return _db;
}
}
請務必將DATABASE.s3db替換為包含文件夾結構的數據庫文件的名稱。
對於錯誤:
'file is encrypted or is not in a database'
您在哪個版本的SQLite中創建了此數據庫? 您必須使用SQLite版本3.如果您使用的是SQLite3,那么您是否在此數據庫上設置了密碼?
我們可以這樣使用......
private void button1_Click(object sender, RoutedEventArgs e)
{
// Code runs "for real"
_customerEntries = (Application.Current as App).db.SelectObservableCollection<Category>("SELECT Category_Name FROM Category WHERE Category_ID=1");
foreach (Category me in _customerEntries )
{
fielddiscription.Add(me.field_description);//add to list
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.