首页 > 解决方案 > 使用 Xamarn android 和 SQLite 浏览器

问题描述

我是 Xamarn 和 C# 的新手,我尝试使用数据库制作应用程序。我尝试选择表格中的每一行并且它起作用了。但是当我尝试插入时,没有出现任何错误,但我看不到我在 SQLite browser 上添加的新行。此外,当我尝试选择所有行时,新的确实出现了:)。

我正在尝试创建一个数据库,如果有人添加了每个人都可以看到的项目,那么任何人都可以在其中添加他们可以看到的东西更像是商店。

还有一件事,当我尝试选择表时,我总是会收到此错误未处理的异常:Android.Database.Sqlite.SQLiteException:发生 在此处输入图像描述

这是我的联系

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Database.Sqlite;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Java.Interop;
using Java.Lang;
using SQLite;
using Boolean = System.Boolean;
using Byte = System.Byte;
using SQLiteException = SQLite.SQLiteException;

namespace AppDatabase.Resources
{
    public class DBdiagnoseX : SQLiteOpenHelper
    {
        private static readonly string DB_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        private static string DB_name = "Diagnose.db";
        private static int VERSION = 1;
        public Context context;

        public DBdiagnoseX(Context context) :base(context, DB_name, null, VERSION)
        {
            this.context = context;
        }


        public string GetSqlPath()
        {
            return Path.Combine(DB_path, DB_name);
        }

        public override SQLiteDatabase WritableDatabase
        {
            get
            {
                return CreateSQLiteDB();
            }
        }

        private SQLiteDatabase CreateSQLiteDB()
        {
            SQLiteDatabase sqliteDB=null;
            string path = GetSqlPath();
            Stream streamSQLite = null;
            FileStream streamWriter = null;
            Boolean IsSQLiteInit = false;
            try
            {
                if (File.Exists(path))
                    IsSQLiteInit = true;
                else
                {
                    streamSQLite = context.Resources.OpenRawResource(Resource.Raw.Diagnose);
                    streamWriter = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                    if(streamSQLite!=null && streamWriter != null)
                    {
                        if(CopySQLiteDB(streamSQLite,streamWriter))
                            IsSQLiteInit = true;
                    }
                }

                if (IsSQLiteInit)
                    sqliteDB =SQLiteDatabase.OpenDatabase(path, null, DatabaseOpenFlags.OpenReadonly);
            }
            catch{ }
            return sqliteDB;
        }

标签: c#androidsqlitexamarin

解决方案


推荐阅读