gpt4 book ai didi

用于存储sqlite db的C代码不将数据存储在物理内存中

转载 作者:行者123 更新时间:2023-11-30 18:04:46 25 4
gpt4 key购买 nike

虽然这个程序在我的dev-C++编译器上执行期间工作得很好,但接收值并再次显示它,下次执行时它不会打开以前的数据库,我必须再次输入数据。数据库数据输入的内容未物理存储在内存中。请帮忙!

void fill(sqlite3* db, char* tbname);
void CreateDatabase(sqlite3** db);
void CreateTable(sqlite3* db, char* tbname);
void display(sqlite3* db, char* tbname);

int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for(i=0; i<argc; i++){
printf("%s = ",azColName[i]);
if( argv[i] )
printf(argv[i]);
else
printf("NULL");
printf("\n");
}

return 0;
}

void dsperr(char**db_err)
{
if( *db_err )
{
printf("db_err\n");
sqlite3_free(*db_err); // release (free) memory
*db_err = 0; // reinitialize the caller's pointer
}
}

void GetTablename(char* tbname)
{
printf("Enter the table name\n");
scanf("%s", tbname);
}

int main()
{
while(1)
{
sqlite3* db = 0; // database connection
char tbname[DBNAMESIZE]; // table name
int done = 0;
while(!done)
{
char answer[8] = {0};
printf("\n\nPlease select one of the folowing options\n");
printf("1. Create a new or open an existing database\n");
printf("2. Create a new table\n");
printf("3. Add some data\n");
printf("4. List all data\n");
printf("5. Quit\n");
scanf("%1s",answer);

switch(answer[0])
{
case '1':
CreateDatabase(&db);
break;
case '2':
CreateTable(db, tbname);
break;
case '3':
fill(db, tbname);
break;
case '4':
display(db, tbname);
break;
case '5':
done = 1;
break;
}
}

sqlite3_close(db);
}
}

void CreateDatabase(sqlite3** db)
{
char dbname[DBNAMESIZE];
int n;
printf("Please enter the name of the database\n");
scanf("%s",dbname);
fgets(dbname,sizeof(dbname), stdin);

if( dbname[strlen(dbname)-1] == '\n')
dbname[strlen(dbname)-1] = '\0';
n = sqlite3_open(dbname, db);
if( n != SQLITE_OK )
{
printf("Error opening database.\n");
}
else
printf("Database open ok\n");
}

void CreateTable(sqlite3* db, char* tbname)
{
char* db_err = 0;
int n = 0;
char statement[255] = {0};
GetTablename(tbname);

if( tbname[0] == '\0' )
return;
sprintf(statement, "CREATE TABLE %s (id integer);", tbname);

n = sqlite3_exec(db, statement, NULL, 0, &db_err);
dsperr(&db_err);
if( n != SQLITE_OK )
{
printf("Error executing \"%s\"\n",statement);
}
else
printf("Table created successfully\n");
}

void fill(sqlite3* db, char* tbname)
{
char* db_err = 0;
int id;
int n = 0;
char buf[80];

if( strlen(tbname) == 0)
{
GetTablename(tbname);
if( tbname[0] == '\0')
return;
}

printf("Enter the data into the table\n");
scanf("%d",&id);
sprintf(buf,"insert into %s values(%d);", tbname, id);
n = sqlite3_exec(db, buf, NULL, 0, &db_err);
dsperr(&db_err);

if( n != SQLITE_OK )
{
printf("Error inserting value %d\n", id);
return;
}
}

void display(sqlite3* db, char* tbname)
{
char* db_err = 0;
char select[255] = {0};
if( strlen(tbname) == 0)
{
GetTablename(tbname);
if( tbname[0] == '\0' )
return;
}

if( strlen(tbname) > 0)
{
sprintf(select, "select * from %s;", tbname);
sqlite3_exec(db, select, callback, 0, &db_err);
dsperr(&db_err);
}
}

最佳答案

我已经很多年没有使用过 C 语言了,但是根据 this page 上的 sqlite3_open() 文档:

If the filename is an empty string, then a private, temporary on-disk database will be created. This private database will be automatically deleted as soon as the database connection is closed.

您可能将空字符串传递给sqlite3_open()?看起来您正在读取 dbname 两次,一次使用 scanf,一次使用 fgets

关于用于存储sqlite db的C代码不将数据存储在物理内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7396688/

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