我如何在Xamarin表单上使用SQLite创建数据库?

axzmvihb  于 2023-03-03  发布在  SQLite
关注(0)|答案(1)|浏览(127)

我一直在使用SQLite-net-pcl添加数据库到我的应用程序,但当我尝试创建数据库时,它抛出了此异常SQLite。'无法打开数据库文件:mydatabase.db3(误用)“。

public class MySQLDatabase
{
    private static SQLiteAsyncConnection db;

    public void MyDatabase()
    {
        if (db != null)
        {
            return;
        }

        string databasepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "mydatbase.db3");
        db = new SQLiteAsyncConnection(databasepath);
        db.CreateTableAsync<MyTable>();
    }

    public void InsertData(int gameid, string preorder,string rating,string standards,string overpriced,string agerating,string gameplay,string recommend,string impact)
    {
        MyTable myNewTable = new MyTable
        {
            GameId = gameid,
            PreOrder = preorder,
            Rating = rating,
            Standards = standards,
            OverPriced = overpriced,
            AgeRating = agerating,
            Gameplay = gameplay,
            Recommend = recommend,
            Impact = impact
        };
        conn.Insert(myNewTable);
    }

    public void CloseConnection()
    {
        conn.Close();
    }
}

我已经看到了使用SQLiteConnection.CreateFile()解决这个问题的其他方法,但是由于某种原因,这个方法没有出现。我使用的是SQLite-net-pcl 1.8.116版本。

iovurdzv

iovurdzv1#

这是我在Xamarin中创建和迁移Sqlite DB时使用方法。

var rootFolder = new LocalRootFolder();
var connection = new SQLiteAsyncConnection(Path.Combine(rootFolder.Path, "my_db.sqlite"));
await connection.CreateTableAsync<My_Table>();

相关问题