gpt4 book ai didi

c - 在C中将Sqlite表格式化为字符串(char)?

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

我对 Sqlite 很陌生,我想使用 C 自动从 sqlite 数据库表写入一个文本文件(实际上是 xml)。我该怎么做?我用这些从数据库中选择一些数据并在终端中打印:

int callback(void * ptr, int resultados, char ** STR1, char **STR2) {
int i;

for(i = 0; STR1[i] != NULL; i++) {
printf("%s = %s\n", STR2[i], STR1[i]);
}
return 0;
}

以及 main 内部:

/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}

/* Create SQL statement */
sql = "SELECT * from COMPANY";

/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Operation done successfully\n");
}
sqlite3_close(db);

然后我尝试更改回调,让 STR1 和 STR2 写入文本文件,如下所示:

int callback(void * ptr, int resultados, char ** STR1, char **STR2) {
int i;
char teste[1024];
FILE *pFile;

pFile = fopen ("test.txt", "a+");
for(i = 0; STR1[i] != NULL; i++) {
printf("%s = %s\n", STR2[i], STR1[i]);
snprintf (teste, sizeof(teste), "%s %s\n", STR2[i], STR1[i]);
fwrite (teste, 1, sizeof(teste), pFile);
fflush(pFile);

}
return 0;
}

但是当我尝试打开 test.txt 时,gedit 说“打开文件/home/kdan/test/test.txt 时出现问题。”您打开的文件包含一些无效字符,并且文本包含一堆随机字符、数字和符号。因此,我无法获取所选数据并转换为字符串 D:我是否应该尝试使用诸如 sqlite_mprintfsqlite_vmprintfsqlite_exec_printf 之类的东西> 或sqlite_get_table_printf?这样做对吗?或者是否有另一种方法来选择数据并将其格式化为变量char?抱歉我的英语生疏。

编辑:它现在正在工作,只需要使用 fputs 而不是 fwrite,这是工作代码:

int callback(void * ptr, int resultados, char ** STR1, char **STR2) {
int i;
char teste[1024];
FILE *pFile;
pFile = fopen ("test.txt", "a");
for(i = 0; STR1[i] != NULL; i++) {
printf("%s = %s\n", STR2[i], STR1[i]);
snprintf (teste, sizeof(teste), "%s %s\n", STR2[i], STR1[i]);
fputs (teste, pFile);
fflush(pFile);
}
return 0;
}

最佳答案

尚未测试您的代码,但我认为问题出在 fwrite() 调用上。

来自 Linux 手册页:

NAME
fread, fwrite - binary stream input/output

SYNOPSIS
[...]

size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);

DESCRIPTION
[...]

The function fwrite() writes nmemb elements of data, each size bytes
long, to the stream pointed to by stream, obtaining them from the loca‐
tion given by ptr.

STR2[i]STR1[i] 的总长度可能小于 sizeof teste。出于说明目的,假设 STR2[i]"foo"STR1[i]"bar" >。在 snprintf() 调用之后,teste 将具有以下内容:

teste[0] 'f'
teste[1] 'o'
teste[2] 'o'
teste[3] ' '
teste[4] 'b'
teste[5] 'a'
teste[6] 'r'
teste[7] '\n'
teste[8] '\0'
teste[9] onwards uninitialised

由于fwrite用于写入二进制流,因此在到达'\0'字节后不会停止写入。相反,它会继续在 teste 中写入所有未初始化的字符。在本例中,它将写入 1015 个未初始化的字符。

写入文本流的正确方法是使用 fputs() (在这种情况下,您应该删除字符串中的 '\n')或fprintf()。或者,如果您确实想使用 fwrite(),请让它打印 strlen(teste) 字节(在本例中为 8)而不是 sizeof teste 是 1024。

最后,虽然与您的问题无关,但如果您没有从文件中读取内容,则应该使用 "a" 而不是 "fopen() 来读取文件a+”

关于c - 在C中将Sqlite表格式化为字符串(char)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517880/

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