gpt4 book ai didi

c - 包含两个数据库的sqlite3 C API,在一个数据库中创建表影响另一个数据库产生的结果

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:18 28 4
gpt4 key购买 nike

我正在尝试为我的项目使用两个数据库。已经创建了一个称为 EPG 数据库的数据库,其中包含许多表和条目。另一个是我自己的数据库,称为 REC 数据库。当我打开自己的数据库时,我想在其中创建一个表。所以一共有三个函数:

   re_rec_db_init();
create_table();
re_epg_db_init();

re_db_db_init() 的代码是这样的:

   void re_epg_db_init(void)
{
int status = sqlite3_initialize();

s_dbepg_path=re_dbepg_get_path(); //get the path of the database

//openrecDB

status=sqlite3_open_v2(s_dbepg_path,&s_re_dbepg,SQLITE_OPEN_READONLY,NULL);
//check status
if(status!=SQLITE_OK){
printf("\n\nerror occured in opening epg database \n");
exit(0);
}

status=sqlite3_extended_result_codes(s_re_dbepg,1);
printf("\n\nstatus for opening the epg db is = %d \n",status);

}

re_rec_init 的唯一区别是路径、句柄名称和模式不同 (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE)

接下来,create_table() fn 将帮助我在 rec 数据库中创建一个不同的表。它看起来像这样:

void  create_table(void)
{
int status;
char *sql_new;
char *zErr;
sql_new="create table if not exists test(Eventid VARCHAR(128) primary key,Title VARCHAR(128))";
status=sqlite3_exec(s_re_dbrec,sql_new,NULL,NULL,&zErr);
if(status==SQLITE_OK || status==SQLITE_DONE)
{
printf("\n\n table ready!! \n");
}
else
{
printf("\n\n table status: %d",status);
}

}

我希望程序在 epg 数据库中查询给定的标题并返回 Eventid。但是由于这个 create_tables 方法出现了问题。当我让它执行时,返回的 Eventid 为空。而当我将其注释掉时,Eventid 完美返回。这两种情况的输出是

case1:创建表时

    status for opening the rec db is = 0 

table ready!!

enter the title to be queried:Looney Tunes

the entered title is:Looney Tunes

status for opening the epg db is = 0

success in reading the query!

Event id of the title is:(null) //wrong!

case2:当create_table被注释掉时

  status for opening the rec db is = 0 

enter the title to be queried:Looney Tunes

the entered title is:Looney Tunes

status for opening the epg db is = 0

success in reading the query!

Event id of the title is:00010006014124ed //correct result!

我真的不知道这是怎么回事。在读取查询时,我使用标准的 sqlite3_prepare_v2,sqlite3_step 命令来获取事件 ID。请帮帮我

谢谢

最佳答案

您的 sqlite 数据库指针似乎正在引用您新创建的数据库,并且不会在下一次初始化时更新。你的代码也没有反射(reflect)你到底在做什么。给出一些简单的精确代码。

sqlite3 *dbfile, *dbfile2;

您可以尝试在打开的连接中插入/更新/删除/创建。必须将连接指针更改为当前正在使用的数据库。

bool ConnectDB (sqlite3 dbfile)
{
if ( sqlite3_open(DB, &dbfile) == SQLITE_OK )
{
isOpenDB = true;
return true;
}

return false;
}

void DisonnectDB ()
{
if ( isOpenDB == true )
{
sqlite3_close(dbfile)
}
}

关于c - 包含两个数据库的sqlite3 C API,在一个数据库中创建表影响另一个数据库产生的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9408946/

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