[英]sqlite-net-pcl queries are returning correct row count but all data is null or 0
請幫忙。 我正在嘗試學習 xamarin c-sharp。 為此,我學到了足夠多的基礎知識,我對嘗試制作一個簡單的 SQLite Db 應用程序充滿信心。
我正在使用 sqlite-net-pcl。 我已經嘗試過 Xamarin.Android 以及 Xamarin.Z64502425319129281C3683CAE8AZ8。 我已經在兩台不同的 win10 計算機上運行了該應用程序。 我已經在模擬器和手機上運行了該應用程序。 所有結果都相同。 . . 所有返回的數據都是 0 或 null。
我有一個現有的非空數據庫(textdb.db):
我搜索了 web 並發現一些人抱怨相同的結果,但我找不到解決方案。
我希望有人在那里看到一些東西並告訴我出了什么問題。
結構:表:testtable............ id,INT,主鍵數據,BIGINT
數據 id = 1 數據 = 2.......
我的代碼:
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;
}
}
對象中的所有結果都是值 0 而不是 1 和 2。
對於我的 sqlite.net,這是我的設置:
0 我正在模型文件夾中創建一個 class
namespace MyApp.Models
{
public class testtable
{
public int id { get; set; }
public int data {get; set; }
}
}
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 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 調用數據庫,比如你想在出現頁面時調用數據庫:
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();
}
嘗試使用您的 ID 創建 class,如下所示
class IdResult
{
public int ID { get; set; }
}
然后
o1 = dbConnection.QueryAsync<IdResult>(sql1,0);
您將在 IdResult.ID 中看到您的 ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.