值得注意的是,sqlite2和sqlite3的数据格式不兼容。我用的sqlite是版本2的,导致次程序一直调不通。后来发现才该过来。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <iconv.h>
int main()
{
sqlite3* pdb = 0;
sqlite3_stmt* pstmt = 0;
int l1, l2, ret;
char *p1, *p2;
char* file1 = "test.db";
char file2[32];
char* sql_sel1 = "select * from test";
char sql_sel2[64];
const char* ptr = 0;
iconv_t cd;
cd = iconv_open("UTF8", "ASCII");
if (cd == (iconv_t)-1)
{
printf("iconv_open error!/n");
exit(0);
}
bzero(file2, 32);
l1 = strlen(file1);
l2 = 32;
p1 = file1;
p2 = file2;
while (l1 > 0)
{
if (iconv(cd, &p1, &l1, &p2, &l2) == -1)
break;
}
bzero(sql_sel2, 64);
l1 = strlen(sql_sel1);
l2 = 64;
p1 = sql_sel1;
p2 = sql_sel2;
while (l1 > 0)
{
if (iconv(cd, &p1, &l1, &p2, &l2) == -1)
break;
}
iconv_close(cd);
if (sqlite3_open(file2, &pdb) != SQLITE_OK)
{
printf("sqlite3_open:%s/n", sqlite3_errmsg(pdb));
exit(0);
}
if (sqlite3_prepare(pdb, sql_sel2, strlen((char*)sql_sel2), &pstmt, &ptr) != SQLITE_OK)
{
printf("sqlite3_prepare:%s/n", sqlite3_errmsg(pdb));
exit(0);
}
printf("id/tname/n");
while ((ret = sqlite3_step(pstmt)) != SQLITE_DONE)
{
if (ret == SQLITE_BUSY)
continue;
if (ret == SQLITE_ERROR)
break;
printf("%d/t%s/n", sqlite3_column_int(pstmt, 0), sqlite3_column_text(pstmt, 1));
}
sqlite3_finalize(pstmt);
sqlite3_exec(pdb, "insert into test values(NULL, 'god')", 0, 0, 0);
sqlite3_close(pdb);
printf("ok!/n");
}
#compile
gcc -o test -lsqlite3 test.c
sqlite api使用代码实例
最新推荐文章于 2024-03-16 11:24:09 发布