简体   繁体   English

sqlite-net-pcl 查询返回正确的行数,但所有数据都是 null 或 0

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

Help Please.请帮忙。 I am trying to learn xamarin c-sharp.我正在尝试学习 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.为此,我学到了足够多的基础知识,我对尝试制作一个简单的 SQLite Db 应用程序充满信心。

I am using sqlite-net-pcl.我正在使用 sqlite-net-pcl。 I have tried Xamarin.Android as well as Xamarin.Forms.我已经尝试过 Xamarin.Android 以及 Xamarin.Z64502425319129281C3683CAE8AZ8。 I have run the app on two different win10 computers.我已经在两台不同的 win10 计算机上运行了该应用程序。 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.所有返回的数据都是 0 或 null。

I have an existing non-empty db (textdb.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.我搜索了 web 并发现一些人抱怨相同的结果,但我找不到解决方案。

I hope that someone out there will see something and tell me what is wrong.我希望有人在那里看到一些东西并告诉我出了什么问题。

Structure: table: testtable................... id, INT, primary key data, BIGINT结构:表:testtable............ id,INT,主键数据,BIGINT

Data id = 1 data = 2...................数据 id = 1 数据 = 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.对象中的所有结果都是值 0 而不是 1 和 2。

For my sqlite.net, this is my setting:对于我的 sqlite.net,这是我的设置:

0 I'm creating a class in Models folder 0 我正在模型文件夹中创建一个 class

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

1 Inside MainActivity.cs (Android) 1 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: 2 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: 3 调用数据库,比如你想在出现页面时调用数据库:

    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();
    }

Try creating a class with your ID, like this尝试使用您的 ID 创建 class,如下所示

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

then然后

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

You will see your ID in IdResult.ID.您将在 IdResult.ID 中看到您的 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM