SQLite指南(3) - 5分钟了解熟悉SQLite
博客分类:
DB2|PostgreSQL|SQLite|Others Database General
在没有大量阅读SQLite在线文档并且不了解相关配置之前,几分钟的时间可以让你快速了解SQLite.
1. 下载源代码,你总能从http://www.sqlite.org/download.html 这里下载到最新的SQLite发行版本对应的源码。
2. 创建数据库
你可以从http://www.sqlite.org/download.html这里下载到sqlite3.exe,也可以直接依据下文:
http://iihero.iteye.com/blog/1175595,自己动手编译出该可执行文件。 进到cmd窗口,进到sqlite3所在目录,执行sqlite3 1. D:\\shared>sqlite3 d:\\shared\\demo\est.db 2. SQLite version 3.7.6 3. Enter \".help\" for instructions 4. Enter SQL statements terminated with a \";\" 5. sqlite> create table t(id int primary key, col2 varchar(32)); 6. sqlite> insert into t values(1, 'iihero'); 7. sqlite> insert into t values(2, '中国'); 8. sqlite> select * from t; 9. 1|iihero 10.2|中国 11.sqlite> 3.编写简单的使用SQLite的应用程序 使用C-API来访问SQLite数据库: Java代码 1. #include 4. static int callback(void *NotUsed, int argc, char **argv, char **azColName){ 5. int i; 6. for(i=0; i 9. printf(\"\\n\"); 10. return 0; 11.} 12. 13.int main(int argc, char **argv){ 14. sqlite3 *db; 15. char *zErrMsg = 0; 16. int rc; 17. 18. if( argc!=3 ){ 19. fprintf(stderr, \"Usage: %s DATABASE SQL-STATEMENT\\n\0]); 20. exit(1); 21. } 22. rc = sqlite3_open(argv[1], &db); 23. if( rc ){ 24. fprintf(stderr, \"Can't open database: %s\\n\(db)); 25. sqlite3_close(db); 26. exit(1); 27. } 28. rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg); 29. if( rc!=SQLITE_OK ){ 30. fprintf(stderr, \"SQL error: %s\\n\31. sqlite3_free(zErrMsg); 32. } 33. sqlite3_close(db); 34. return 0; 35.} 逻辑很简单,就带两个参数,每一个参数为db文件的路径,第2个参数为要执行的sql语句。 设该文件为demo.c. 编译,需要sqlite3.h, sqlite3.c以及这个demo.c 下边看看我的整个编译及测试过程: Java代码 1. E:\\learn\\db_research\\sqlite\\sqlite_auto_build>cd sqlite-amalgamation-3070800 2. 3. E:\\learn\\db_research\\sqlite\\sqlite_auto_build\\sqlite-amalgamation-3070800>cl -Gs -GX -D_WIN32 -nologo -Zi -DOS_WIN=1 -DSQLITE_DEBUG=1 -DWIN32=1 -DTHREADSAFE=1 -DSQLITE_OS_WIN=1 -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_SOUNDEX=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -I. demo.c sqlite3.c -o demo.exe 4. cl : Command line warning D9035 : option 'GX' has been deprecated and will be removed in a future release 5. cl : Command line warning D9036 : use 'EHsc' instead of 'GX' 6. cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release 7. demo.c 8. sqlite3.c 9. Generating Code... 10. 11.E:\\learn\\db_research\\sqlite\\sqlite_auto_build\\sqlite-amalgamation-3070800>dir demo.exe 12. 驱动器 E 中的卷没有标签。 13. 卷的序列号是 04EC-044E 14. 15. E:\\learn\\db_research\\sqlite\\sqlite_auto_build\\sqlite-amalgamation-3070800 的目录 16. 17.2011-09-27 21:32 1,221,120 demo.exe 18. 1 个文件 1,221,120 字节 19. 0 个目录 8,856,236,032 可用字节 20. 21.E:\\learn\\db_research\\sqlite\\sqlite_auto_build\\sqlite-amalgamation-3070800>demo.exe d:\\shared\\demo\est.db \"create table t123(id int primary key, col2 varchar(32)); insert into t123 values(1, 'iihero')\" 22. 23.E:\\learn\\db_research\\sqlite\\sqlite_auto_build\\sqlite-amalgamation-3070800>d:\\shared\\sqlite3.exe d:\\shared\\demo\est.db 24.SQLite version 3.7.6 25.Enter \".help\" for instructions 26.Enter SQL statements terminated with a \";\" 27.sqlite> select * from t123; 28.1|iihero 29.sqlite> 上边的例子,我并没有创建一个独立的VC工程,因为不过是几个源文件,加一条编译命令。没必要弄得那么烦琐。如果您有兴趣,也可以根据上边的编译命令,建立一个Win32 console工程,应该很简单。 至于tcl编译并绑定的示例,有兴趣的,可以自行下载 http://www.sqlite.org/sqlite-autoconf-3070800.tar.gz, 进行实验。 并搭建tcl环境 因篇幅问题不能全部显示,请点此查看更多更全内容