代码之家  ›  专栏  ›  技术社区  ›  B. Clay Shannon-B. Crow Raven

为什么我的SQLite数据库被认为是只读的?

  •  0
  • B. Clay Shannon-B. Crow Raven  · 技术社区  · 4 年前

    enter image description here

    …当我尝试这样写入SQLite数据库时:

    private async Task InsertMapRecord(string mapName, string mapNotes)
    {
        path = folder.Path;
        connStr = string.Format(connStrBase, path);
        try
        {
            SqliteConnection conn = new SqliteConnection(connStr);
            String query = "INSERT INTO CartographerMain " +
                "(MapName, MapNotes) " +
                "VALUES (@MapName, @MapNotes)";
    
            using (SqliteCommand cmd = new SqliteCommand(query, conn))
            {
                cmd.Parameters.AddWithValue("@MapName", mapName);
                cmd.Parameters.AddWithValue("@MapNotes", mapNotes);
                await conn.OpenAsync();
                int result = await cmd.ExecuteNonQueryAsync();
    
                if (result < 0)
                {
                    MessageDialog dialog = new MessageDialog("Error inserting data into CartographerMain");
                    await dialog.ShowAsync();
                }
            }
        }
        catch (SqliteException sqlex)
        {
            string sqlExcMsg = string.Format("{0} Inner Exception: {1} Stack Trace: {2}", sqlex.Message,    sqlex.InnerException, sqlex.StackTrace);
            MessageDialog dialog = new MessageDialog(sqlExcMsg, "InsertMapRecord");
            await dialog.ShowAsync();
        }
    }
    

    所以,根据一些信息 here ,我检查了应用程序运行的文件夹。它被标记为只读,如下所示:

    enter image description here

    enter image description here

    更新

    在下面的评论中回答Selvin的问题,我存储db文件如下:

    StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    string connStrBase = @"Data source={0}\Cartographer.db";
    string connStr = string.Empty;
    string path = string.Empty;
    
    . . .
    
    private async Task InsertMapRecord(string mapName, string mapNotes)
    {
        path = folder.Path;
        connStr = string.Format(connStrBase, path);
        try
        {
            SqliteConnection conn = new SqliteConnection(connStr);
        . . .
    

    我将数据库添加到项目中,将其Copy to Output Directory属性设置为“Copy if newer”

    数据库文件的路径是C:\Users\bclay\source\repos\magratoryou\magratoryou\magratoryer.db。我手动复制到那里。

    0 回复  |  直到 4 年前