简单的数据库应用程序不会检索值

时间:2016-06-30 15:23:17

标签: c# android xamarin

我正在学习SQLite,想要创建自己非常简单的概念证明应用程序,我只需从数据库中存储和检索值。我使用Krueger的NuGet包SQLite-net。

我的问题是一切似乎都工作,除了它没有检索值,它崩溃在“字符串a = db.GetAsync(0).Result.storedvalue.ToString();”,只说多个错误发生。这一行是否有问题,或者我首先如何存储值有什么问题?

Image of app running in emulator

以下是该应用的完整C#代码:

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using SQLite;
using System.IO;
using System.Reflection;

namespace SimpleDatabaseTest
{
    [Activity(Label = "SimpleDatabaseTest", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        SQLiteConnection db;
        int i;
        int k;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // create database
            db = new SQLiteConnection(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"test.sqlite"));
            db.CreateTable<somevalues>();

            // set up enter_value and show_value
            EditText enter_value = FindViewById<EditText>(Resource.Id.enter_value);
            TextView show_value = FindViewById<TextView>(Resource.Id.show_value);

            // store value button
            Button store_value = FindViewById<Button>(Resource.Id.store_value);

            store_value.Click += delegate
            {
                somevalues newTable = new somevalues();
                i = int.Parse(enter_value.Text);

                newTable.storedvalue = i;
                k = newTable.id;
                db.Insert(newTable);

                Toast toast = Toast.MakeText(ApplicationContext, k.ToString(), ToastLength.Long); //enter_value.Text
                toast.Show();
            };

            // retrieve value button
            Button retrieve_value = FindViewById<Button>(Resource.Id.retrieve_value);

            retrieve_value.Click += delegate
            {
                somevalues some = db.Get<somevalues>("id");
                //string a = some.storedvalue.ToString();

                //Toast toast = Toast.MakeText(ApplicationContext, a, ToastLength.Long);
                //toast.Show();

                //show_value.Text = a;
            };
        }
    }

}

0 个答案:

没有答案