I have SQLite database generated in Sqlite Browser with imported Excel files. It has few tables. Each table has same structure.
Columns are:
All I need is to open this database in my Xamarin.Forms Shared project and execute 1 item per time.
Each cell includes 1 double number. I need to execute this number and send it to Label on screen.
I don't need to write anything to this database. Only read.
Best way I can imagine is to Execute SQL like:
Select 2000m From Bravo Where Age = 20.
Can someone help me with that?
So the problem was with Windows Phone. Somehow it can't start SQLiteConnection. But Android does, so iOS must too.
First of all i moved my DB to local path of application:
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;
}
Then i opened connection:
public SQLiteConnection DBClass_conn()
{
database = new SQLiteConnection (DatabasePath);
return database;
}
Then i added SQLiteCommand:
SQLiteCommand cmd = new SQLiteCommand(App.Database.DBClass_conn());
And query:
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;
}
Then i just change CommandText and ExecuteScalar:
cmd.CommandText = querytext("Symphony");
string count = (string)cmd.ExecuteScalar<string>();
Right now it execute only 1 query, but it's not hard to make it more universal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.