win10 QT failure attempt to write a readonly database

err: insert LINK failure attempt to write a readonly database

err: insert SCAN failure attempt to write a readonly database

在网上找了许多文章都说原因是用户权限不足,无法访问数据库文件。但都没针对window提供很好的解决办法。我在QT开发过程中也遇到类似的问题,后来发现QT可以访问它自己创建的文件。因此,这里的解决办法就是不用现成的db文件,重新调用sqlite3_open函数来创建新的db文件。这样操作后,qt就可以向db文件写入数据了。

    /* 打开DB */
    sqlite3 *db;
    m_handle = nullptr;
    const char* path = "./png/sim.db";
    int ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK) {
        printf("err: open DB\n");
        fflush(stdout);
        return;
    }

    /* 保存句柄 */
    m_handle = db;


    /* 删除表格 */
    char* errMsg = nullptr;
    const char* tlist[] = {"SCAN", "LINK"};
    char* sql1 = sqlite3_mprintf("drop table if exists %s", tlist[tab]);
    int rc = sqlite3_exec(m_handle, sql1, 0, 0, &errMsg);
    if (rc != SQLITE_OK) {
        printf("err: drop %s failure %s\n", tlist[tab], errMsg);
        sqlite3_free(errMsg);
        sqlite3_free(sql1);
        fflush(stdout);
        return;
    }
    sqlite3_free(sql1);

    /* 重建表格 */
    char *sql2 = sqlite3_mprintf("create table if not exists %s(year INTEGER, month INTEGER, day INTEGER, hour INTEGER,"
                                 "min INTEGER, sec INTEGER, valid INTEGER, glbChId INTEGER, snr INTEGER, noise INTEGER,"
                                 "UNIQUE(year, month, day, hour, min, sec, glbChId) ON CONFLICT REPLACE)", tlist[tab]);
    rc = sqlite3_exec(m_handle, sql2, 0, 0, &errMsg);
    if (rc != SQLITE_OK) {
        printf("err: create SCAN failure %s\n", errMsg);
        sqlite3_free(errMsg);
        sqlite3_free(sql2);
        return;
    }
    sqlite3_free(sql2);

你可能感兴趣的:(数据库,数据库无法访问)