没太多时间写正文,就看一个简单的例子程序吧,:)
#include
#include
#include
int main (void)
{
int ret = -1;
sqlite3 *db = NULL;
// 创建数据库和表
ret = sqlite3_open ("testonconf.db", &db);
char *c = "CREATE TABLE test (id INTEGER PRIMARY KEY, data INTEGER)";
ret = sqlite3_exec (db, c, NULL, NULL, NULL);
// 插入三行数据
char *i1 = "INSERT INTO test VALUES (1, 2222)";
ret = sqlite3_exec (db, i1, NULL, NULL, NULL);
char *i2 = "INSERT INTO test VALUES (5, 5555)";
ret = sqlite3_exec (db, i2, NULL, NULL, NULL);
char *i3 = "INSERT INTO test VALUES (9, 1129)";
ret = sqlite3_exec (db, i2, NULL, NULL, NULL);
// 试验1: 没什么悬念,id=5的原来data为5555的被替换成333333
//char *i4 = "INSERT OR REPLACE INTO test (id, data) VALUES (5, 333333)";
//ret = sqlite3_exec (db, i4, NULL, NULL, NULL);
// 试验2: 错误19(SQLITE_CONSTRAINT),约束冲突 己有id=1,且id是unique值
//char *i5 = "UPDATE test SET id=5, data=545454 WHERE id=1";
//ret = sqlite3_exec (db, i5, NULL, NULL, NULL);
//printf ("ret: %d\n", ret); // 返回19, SQLITE_CONSTRAINT // Abort due to constraint violation
// 试验3: 没什么悬念,id=5的原来data为5555的被替换成333333
//char *i5 = "UPDATE test SET id=5, data=545454 WHERE id=5";
//ret = sqlite3_exec (db, i5, NULL, NULL, NULL);
// 试验4: 先删除 unique 冲突类型的id=1和id=5的数据,再执行update
//char *i6 = "UPDATE OR REPLACE test SET id=5, data=545454 WHERE id=1";
//ret = sqlite3_exec (db, i6, NULL, NULL, NULL);
// 试验5: 没有 where 语句约束, 表示所有数据?
char *i7 = "UPDATE OR REPLACE test SET id=5, data=666666";
ret = sqlite3_exec (db, i7, NULL, NULL, NULL);
system ("sqlite3 testonconf.db \"SELECT * FROM test;\"");
char *d = "DELECT FROM test";
ret = sqlite3_exec (db, d, NULL, NULL, NULL);
ret = sqlite3_close (db);
return 0;
}
阅读(5595) | 评论(0) | 转发(0) |