sqlite入门基础(一):sqlite3_open,sqlite3_exec,slite3_close
打开数据库链接sqlite3_open用法 原型: int sqlite3_open( const char *filename,/* Database filename (UTF-8) */ sqlite3 **ppDb OUT: SQLite db handle */ ); 用这个函数开始数据库操作。需要传入两个参数,一是数据库文件名,比如:E:/test.db。文件名不需要一定存在,如果此文件不存在,sqlite会自动建立它。如果它存在,就尝试把它当数据库文件来打开。二是sqlite3**,即前面提到的关键数据结构。这个结构底层细节如何,你不要管它。 关闭数据库链接sqlite3_close用法 int sqlite3_close(sqlite3 *ppDb); ppDb为刚才使用sqlite3_open打开的数据库链接 执行sql操作sqlite3_exec用法 int sqlite3_exec(
sqlite3* ppDb,0); line-height:1.5!important"> An open database */
char *sql,0); line-height:1.5!important"> SQL to be evaluated int (*callback)(void*,255); line-height:1.5!important">int,255); line-height:1.5!important">char**,255); line-height:1.5!important">char**),0); line-height:1.5!important"> Callback function void *,0); line-height:1.5!important"> 1st argument to callback char **errmsg Error msg written here 这就是执行一条sql 语句的函数。 exec 的回调 typedef int(*sqlite3_callback)(void*,int,char**,char**); //para是你在sqlite3_exec 里传入的void*参数通过para参数,你可以传入一些特殊的指针(比如类指针、结构指针), 实例: #include <iostream> using namespace std; #include "sqlite/sqlite3.h" int callback(char**); int main() { sqlite3* db; int nResult = sqlite3_open(test.db",&db); if (nResult != SQLITE_OK) { cout<<打开数据库失败:"<<sqlite3_errmsg(db)<<endl; return 0; } else { cout<<数据库打开成功"<<endl; } char* errmsg; nResult = sqlite3_exec(db,create table fuck(id integer primary key autoincrement,name varchar(100))errmsg); if (nResult != SQLITE_OK) { sqlite3_close(db); cout<<errmsg; sqlite3_free(errmsg); string strSql; strSql+=begin;n"; for (int i=0;i<100;i++) { strSql+=insert into fuck values(null,'heh');n"; } strSql+=commit;//cout<<strSql<<endl; nResult = sqlite3_exec(db,strSql.c_str(),&errmsg); if (nResult != SQLITE_OK) { sqlite3_close(db); cout<<errmsg<<endl; sqlite3_free(errmsg); 0; } strSql = select * from fuck"; nResult = sqlite3_exec(db,callback,&errmsg); 0; } sqlite3_close(db); 0; } int nCount,255); line-height:1.5!important">char** pValue,255); line-height:1.5!important">char** pName) { string s; for(0;i<nCount;i++) { s+=pName[i]; s+=:"; s+=pValue[i]; s+=n"; } cout<<s<<endl; 0; } 修改自董淳光 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |