Help Please. I am trying to learn xamarin c-sharp. To that end I have learned just enough of the basics that I felt confident trying to make a simple SQLite Db app.
I am using sqlite-net-pcl. I have tried Xamarin.Android as well as Xamarin.Forms. I have run the app on two different win10 computers. I have run the app on an emulator as well as on my phone. All with the same results . . . all returned data is 0 or null.
I have an existing non-empty db (textdb.db ):
I have searched the web and found a few people complaining of the same results but I can not find a solution.
I hope that someone out there will see something and tell me what is wrong.
Structure: table: testtable ................... id, INT, primary key data, BIGINT
Data id = 1 data = 2 ...................
My Code:
using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.Widget;
using AndroidX.AppCompat.App;
using Google.Android.Material.FloatingActionButton;
using Google.Android.Material.Snackbar;
using Android.Widget;
using System.IO;
using SQLite;
using System.Linq;
namespace android_db_app
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
. . .
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
. . .
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
. . .
}
private void FabOnClick(object sender, EventArgs eventArgs)
{
. . .
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
. . .
}
public void Exit_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
public void Connect_Click(object sender, EventArgs e)
{
object o,o1;
string dbname = "test.db";
string s = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, @"DailyReminder/DB/" + dbname);
if (!File.Exists(s))
s = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, dbname);
SQLiteAsyncConnection dbConnection = new SQLiteAsyncConnection(s);//SQLiteConnection.Table
string sql = "SELECT * FROM testtable;";
string sql1 = "SELECT ID FROM testtable;";
o = dbConnection.Table<testtable>();
o1 = dbConnection.QueryAsync<int>(sql1,0);
}
}
public class testtable
{
public Int32 id;
public Int64 data;
}
}
All results in the objects are value 0 instead of 1 and 2.
For my sqlite.net, this is my setting:
0 I'm creating a class in Models folder
1 Inside MainActivity.cs (Android)
2 Inside App.xaml.cs:
3 Calling the database, for example you want to call the database when a page appear: