用c语言操作sqlite3
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include "sqlite3.h"
void sqlite_test();
char query(sqlite3 *db, const char *sql);
int main() {
sqlite_test();
}
void sqlite_test() {
sqlite3 *db;
int rc;
rc = sqlite3_open("sqlite.sqlite3", &db);
if(rc) {
fprintf(stderr, "打开失败%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
}else {
printf("成功\n");
char *sql = "CREATE TABLE IF NOT EXISTS test(\
id INTEGER PRIMARY KEY,\
uid INTEGER,\
nickname VARCHAR(12),\
latitude FLOAT\
)";
query(db, sql);
sql = "INSERT INTO test(uid,nickname,latitude)VALUES(1,'abcdefg',1.234)";
query(db, sql);
sql = "SELECT * FROM test";
query(db, sql);
}
}
char query(sqlite3 *db, const char *sql) {
printf("%s\n", sql);
char *select_str = "SELECT";
char *errorMsg;
char *str_str = strstr(sql, select_str);
if(str_str) {
printf("in it, %s\n", str_str);
int row = 0, column = 0, i = 0;
char **result;
sqlite3_get_table(db, sql, &result, &row, &column, &errorMsg);
printf("row:%d, column:%d\n", row, column);
for(; i < column * (row + 1); i++) {
printf("result[%d]=%s\n", i, result[i]);
}
}else{
sqlite3_exec(db, sql, 0, 0, &errorMsg);
}
if(errorMsg){
printf("%s\n", errorMsg);
}else{
printf("success!\n");
}
}