本文介绍一下SQLite C/C++接口。
早期的SQLite C/C++接口只有5个接口函数, 很容易学习。 新版本的SQLite新增了很多功能, 目前大概有185个API接口。本文介绍一些核心的API接口和对象。
1. 核心对象
数据库连接对象: sqlite3prepared_statement对象: sqlite3_stmt严格的讲 sqlite3_stmt对象也不是必须的, 另外两个封装了qlite3_stmt的接口sqlite3_exec和sqlite3_get_table可以代替sqlite3_stmt对象完成数据存取工作。 但是理解sqlite3_stmt对于充分使用SQLite有很大帮助。
以下是一些操作 sqlite3和sqlite3_stmt对象的接口。下面这些名字只是提供一个概念,实际的函数可能会有很多版本比如sqlite3_open() 有,sqlite3_open_v16(), sqlite3_open_v2()
sqlite3_open()sqlite3_prepare()sqlite3_step()sqlite3_column()sqlite3_finalize()sqlite3_close() 函数说明sqlite3_open() 打开一个数据库连接, 返回sqlite3对象sqlite3_prepare() 此函数将SQL转换成sqlite3_stmt对象, 通常使用sqlite3_prepare_v2()sqlite3_step() 此函数单步执行sqlite3_stmtsqlite3_column() 返回 sqlite3_stmt所在行的指定column的值, 有如下具体函数 * sqlite3_column_blob() * sqlite3_column_bytes() * sqlite3_column_bytes16() * sqlite3_column_count() * sqlite3_column_double() * sqlite3_column_int() * sqlite3_column_int64() * sqlite3_column_text() * sqlite3_column_text16() * sqlite3_column_type() * sqlite3_column_value()sqlite3_finalize() 销毁 sqlite3_stmt对象, 所有sqlite3_stmt对象都应该销毁以防止内存泄漏
sqlite3_close() 关闭数据库连接, 销毁sqlite3对象, 所有与这个sqlite3对象相关的sqlite3_stmt对象都应该在调用这个函数之前销毁。2. 绑定和重新执行sqlite3_stmt
sqlite3_reset(): 此函数使得执行过sqlite3_step()的sqlite3_stmt重新执行, 相当于将游标返回到开始位置重新读取数据, sqlite3_reset()的效率比重新创建一个sqlite3_stmt搞很多。 sqlite3_bind(): 此函数用于INSERT SQL, 当同一个INSERT SQL要插入一系列数据时使用, 每次sqlite3_step()后重新bind数据。3. 一般用法和步骤
(1) Create a prepared statement using sqlite3_prepare().(2) Evaluate the prepared statement by calling sqlite3_step() one or more times.(3) For queries, extract results by calling sqlite3_column() in between two calls to sqlite3_step().(4) Destroy the prepared statement using sqlite3_finalize(). 4. 简易接口 sqlite3_get_table()sqlite3_exec()5. 例子
view plaincopy to clipboardprint?
01.void basic_interface() 02.{ 03. sqlite3 *db; 04. char *zErrMsg = 0; 05. int rc; 06. char sql[]="select * from addr;"; 07. sqlite3_stmt *stmt; 08. int id; 09. unsigned char *street, *city; 10. rc = sqlite3_open("D:/VCWork/test.db", &db); 11. if( rc ){ 12. fprintf(stderr, "Can't open database: %s/n", sqlite3_errmsg(db)); 13. sqlite3_close(db); 14. return; 15. } 16. rc= sqlite3_prepare_v2(db,sql, strlen(sql), &stmt,0); 17. if( rc ){ 18. fprintf(stderr, "Can't open statement: %s/n", sqlite3_errmsg(db)); 19. sqlite3_close(db); 20. return; 21. } 22. //right align output format 23. printf("%10s %10s %10s/n", "id", "street", "city"); 24. printf("%10s %10s %10s/n", "---", "---", "---"); 25. while(sqlite3_step(stmt)==SQLITE_ROW ) { 26. id = sqlite3_column_int(stmt, 0); 27. street = (unsigned char*)sqlite3_column_text(stmt,1); 28. city = (unsigned char*)sqlite3_column_text(stmt, 2); 29. printf("%10d %10s %10s/n", id, street, city); 30. } 31. sqlite3_finalize(stmt); 32. sqlite3_close(db); 33.} void basic_interface(){ sqlite3 *db; char *zErrMsg = 0; int rc; char sql[]="select * from addr;"; sqlite3_stmt *stmt; int id; unsigned char *street, *city; rc = sqlite3_open("D:/VCWork/test.db", &db); if( rc ){ fprintf(stderr, "Can't open database: %s/n", sqlite3_errmsg(db)); sqlite3_close(db); return; } rc= sqlite3_prepare_v2(db,sql, strlen(sql), &stmt,0); if( rc ){ fprintf(stderr, "Can't open statement: %s/n", sqlite3_errmsg(db)); sqlite3_close(db); return; } //right align output format printf("%10s %10s %10s/n", "id", "street", "city"); printf("%10s %10s %10s/n", "---", "---", "---"); while(sqlite3_step(stmt)==SQLITE_ROW ) { id = sqlite3_column_int(stmt, 0); street = (unsigned char*)sqlite3_column_text(stmt,1); city = (unsigned char*)sqlite3_column_text(stmt, 2); printf("%10d %10s %10s/n", id, street, city); } sqlite3_finalize(stmt); sqlite3_close(db);}参考:
An Introduction To The SQLite C/C++ Interface