[英]Xamarin.Forms SQLite.net data execution
我有使用導入的Excel文件在Sqlite瀏覽器中生成的SQLite數據庫。 它只有幾張桌子。 每個表具有相同的結構。
列為:
我所需要的只是在Xamarin.Forms Shared項目中打開此數據庫,每次執行1個項目。
每個單元格包含1個雙數。 我需要執行此號碼並將其發送到屏幕上的標簽。
我不需要向該數據庫寫任何東西。 只看。
我能想象的最好方法是執行SQL,例如:
Select 2000m From Bravo Where Age = 20.
有人可以幫我嗎?
因此問題出在Windows Phone。 不知何故它無法啟動SQLiteConnection。 但是Android確實如此,iOS也必須如此。
首先,我將數據庫移至應用程序的本地路徑:
public static string GetLocalFilePath(string filename)
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string dbPath = Path.Combine(path, filename);
CopyDatabaseIfNotExists(dbPath);
return dbPath;
}
private static void CopyDatabaseIfNotExists(string dbPath)
{
if (!File.Exists(dbPath))
{
using (var br = new BinaryReader(Application.Context.Assets.Open("KDLife_mobileDB.db3")))
{
using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int length = 0;
while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, length);
}
}
}
}
}
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
global::Xamarin.FormsMaps.Init(this, bundle);
string dbPath = GetLocalFilePath("KDLife_mobileDB.db3");
LoadApplication (new KDLife_mobile.App ());
App.ScreenWidth = (int)Resources.DisplayMetrics.WidthPixels;
App.ScreenHeight = (int)Resources.DisplayMetrics.HeightPixels;
}
然后我打開連接:
public SQLiteConnection DBClass_conn()
{
database = new SQLiteConnection (DatabasePath);
return database;
}
然后我添加了SQLiteCommand:
SQLiteCommand cmd = new SQLiteCommand(App.Database.DBClass_conn());
並查詢:
public string querytext(string q_table)
{
string sql = "SELECT \"2000f\" FROM Bravo WHERE Age = \"20\" AND Period = \"15\"";//, App.amount_gender, q_table, App.age;
return sql;
}
然后我只是更改CommandText和ExecuteScalar:
cmd.CommandText = querytext("Symphony");
string count = (string)cmd.ExecuteScalar<string>();
現在,它僅執行1個查詢,但使其更通用並不困難。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.