gpt4 book ai didi

c++ - sqlite库无法更新已安装的数据库文件

转载 作者:行者123 更新时间:2023-12-02 10:34:50 25 4
gpt4 key购买 nike

我可以使用sqlite3命令行工具在安装程序已放置的文件中打开,读取和更新数据库,如下所示:

C:\ProgramData\spectronix\oximeter>C:\Users\James\code\bin\sqlite3 spectrumalpha.dat
SQLite version 3.16.2 2017-01-06 16:32:41
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE run ( run INTEGER PRIMARY KEY, timestamp, desc );
CREATE TABLE distance ( hwid, distance );
CREATE TABLE spectrum ( run, timestamp, hwid, distance, data );
CREATE TABLE waveDescription ( run, timestamp, hwid );
CREATE TABLE wave ( wave, idx, data );
sqlite> select desc from run where run is 5;
update from sqlite3 tool
sqlite> UPDATE run SET desc='update #2 from sqlite3 tool' WHERE run IS 5;
sqlite> select desc from run where run is 5;
update #2 from sqlite3 tool

但是当我运行这段代码
#include <iostream>
#include "sqlite3.h"

int main()
{
sqlite3 * db;
sqlite3_stmt *st = 0;
int ret;
char const * tail = 0;

ret = sqlite3_open("C:/ProgramData/spectronix/oximeter/spectrumalpha.dat",&db);

std::cout << "working with db in "
<< sqlite3_db_filename( db, "main" ) << "\n";

// read initial value
ret = sqlite3_prepare_v2(
db,
"SELECT desc FROM run WHERE run IS 5;"
, -1, &st, &tail );
if( ret != SQLITE_OK )
throw std::runtime_error("sqlite first read");
sqlite3_step( st );
std:: cout << "first read is " << sqlite3_column_text(st, 0) << "\n";
sqlite3_finalize( st );

return 0;
}

输出显示数据的先前版本
working with db in C:\ProgramData\spectronix\oximeter\spectrumalpha.dat
first read is test4

另一个奇怪的是库代码拒绝更新数据
#include <iostream>
#include "sqlite3.h"

int main()
{
sqlite3 * db;
sqlite3_stmt *st = 0;
int ret;
char const * tail = 0;

ret = sqlite3_open("C:/ProgramData/spectronix/oximeter/spectrumalpha.dat",&db);

std::cout << "working with db in "
<< sqlite3_db_filename( db, "main" ) << "\n";

// read initial value
ret = sqlite3_prepare_v2(
db,
"SELECT desc FROM run WHERE run IS 5;"
, -1, &st, &tail );
if( ret != SQLITE_OK )
throw std::runtime_error("sqlite first read");
sqlite3_step( st );
std:: cout << "first read is " << sqlite3_column_text(st, 0) << "\n";
sqlite3_finalize( st );

// update value
ret = sqlite3_prepare_v2(
db,
"UPDATE run SET desc = 'new_value' WHERE run IS 5;"
, -1, &st, &tail );
if( ret != SQLITE_OK )
throw std::runtime_error("sqlite update error");
sqlite3_finalize( st );
std::cout << sqlite3_changes( db ) << " rows updated\n";

return 0;
}

输出
working with db in C:\ProgramData\spectronix\oximeter\spectrumalpha.dat
first read is test4
0 rows updated

我尝试将数据库移到应用程序的工作目录中,将打开数据库的调用更改为
ret = sqlite3_open("spectrumalpha.dat",&db);

但是会出现同样的问题。

因此,最后,我删除了数据库,然后从头开始重新创建它。无论是什么问题,问题都消失了!任何人都知道它可能是什么-万一它再次发生?

在我使用 安装程序写入数据库文件之后,第二天问题又回来了。

最佳答案

安装程序正在创建对某些用户具有写许可权而对其他用户具有只读权限的文件。一个典型的unix障碍,但是我不习惯在windows上处理。

关于c++ - sqlite库无法更新已安装的数据库文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60762651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com