gpt4 book ai didi

c++ - 如何从 SQLite 数据库中读取数据?

转载 作者:IT老高 更新时间:2023-10-28 22:03:40 24 4
gpt4 key购买 nike

我决定使用 SQLite,因为它允许将数据库存储到单个文件中。我想我已经设法用 SQLite Database Browser 做一个数据库.

如何在 C/C++ 程序中读取该数据?

最佳答案

使用 sqlite 读取的示例:

#include <stdio.h>
#include <sqlite3.h>
#include <string.h>


int main(int argc, char** argv)
{
const char* username = "satyam";
char q[999];
sqlite3* db;
sqlite3_stmt* stmt;
int row = 0;
int bytes;
const unsigned char* text;

if (2 == argc) {
username = argv[1];
}

q[sizeof q - 1] = '\0';
snprintf(
q,
sizeof q - 1,
"SELECT ipaddr FROM items WHERE username = '%s'",
username
);

if (sqlite3_open ("test.db", &db) != SQLITE_OK) {
fprintf(stderr, "Error opening database.\n");
return 2;
}

printf("Query: %s\n", q);

sqlite3_prepare(db, q, sizeof q, &stmt, NULL);

bool done = false;
while (!done) {
printf("In select while\n");
switch (sqlite3_step (stmt)) {
case SQLITE_ROW:
bytes = sqlite3_column_bytes(stmt, 0);
text = sqlite3_column_text(stmt, 1);
printf ("count %d: %s (%d bytes)\n", row, text, bytes);
row++;
break;

case SQLITE_DONE:
done = true;
break;

default:
fprintf(stderr, "Failed.\n");
return 1;
}
}

sqlite3_finalize(stmt);

return 0;
}

关于c++ - 如何从 SQLite 数据库中读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3957343/

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