标签:
a
#include <stdio.h> #include <sqlite3.h> static int callback(void *NotUsed, int argc, char **argv, char **azColName){ int i; for(i=0;i<argc;i++){ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0 ; } int main(int argc, char **argv){ sqlite3 *db; char *message = 0; int rc ; if(argc!=3){ fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]); return 1; } rc = sqlite3_open(argv[1], &db); if(rc){ fprintf(stderr, "Can‘t open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; } rc = sqlite3_exec(db , argv[2], callback,0 ,&message); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", message); sqlite3_free(message); } sqlite3_close(db); return 0; }
运行命令:
./example test_db "create table test_table (int val);"
在目录下出现一个test_db文件 就是test_db这个数据库了 :)
参考文献:
[0] An Introduction To The SQLite C/C++ Interface http://www.sqlite.org/cintro.html
[1] SQLite3 C语言API入门 http://www.blogjava.net/xylz/archive/2012/09/25/388519.html
标签:
原文地址:http://my.oschina.net/darionyaphet/blog/393082