sqlite-net-pcl queries are returning correct row count but all data is null or 0

857 views Asked by At

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.

2

There are 2 answers

5
Godya Aditya On

For my sqlite.net, this is my setting:

0 I'm creating a class in Models folder

namespace MyApp.Models
{
   public class testtable
     {
      public int id { get; set; }
      public int data {get; set; }
     }
}

1 Inside MainActivity.cs (Android)

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        
        string dbName = "your_table_name.db3";
        string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string dbPath = Path.Combine(folderPath, dbName);
        LoadApplication(new App(dbPath));
    }

2 Inside App.xaml.cs:

    public static string DatabasePath = string.Empty; //sqlite

    public App(string databasePath)
    {
        InitializeComponent();

        MainPage = new NavigationPage(new View.MainPage());

        DatabasePath = databasePath; //sqlite
    }

3 Calling the database, for example you want to call the database when a page appear:

    using MyApp.Models;
    ...
    protected override async void OnAppearing()
    {
         using SQLiteConnection conn = new SQLiteConnection(App.DatabasePath);
         conn.CreateTable<testtable>();
         List<testtable> notes = conn.Table<testtable>().ToList();
    }
0
RFex On

Try creating a class with your ID, like this

class IdResult
{
   public int ID { get; set; }
}

then

o1 =  dbConnection.QueryAsync<IdResult>(sql1,0);

You will see your ID in IdResult.ID.