C++使用SQLite步骤及示例
生活随笔
收集整理的這篇文章主要介紹了
C++使用SQLite步骤及示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
開發環境:Windows XP+VS2005。
SQLite版本為SQLite 3.7.13,相關文件如下。
sqlite-dll-win32-x86-3071300.zip:包含sqlite3.def、sqlite3.dll文件。
sqlite-amalgamation-3071300.zip:包含sqlite3.h 文件。
sqlite-shell-win32-x86-3071300.zip:包含sqlite3.exe 文件。
? 運行Visual Studio 2005 Command Prompt命令行程序。
啟動位置:開始程序->Microsoft Visual Studio 2005->Visual Studio Tools->Visual Studio 2005 Command Prompt。
? 依次執行控制臺命令。
[plain]?view plaincopy cd?D:\sqlite\sqlite-dll-win32-x86-3071300?? D:?? lib?/def:sqlite3.def?/machine:ix86??
? 啟動命令行,進入D:\ sqlite目錄。
命令依次為:
[plain]?view plaincopy cd?D:\sqlite?? d:?? ? 創建test.db測試文件。
創建user表。
創建命令依次如下。
[plain]?view plaincopy D:\sqlite>sqlite3.exe?test.db?? SQLite?version?3.7.13?2012-06-11?02:05:22?? Enter?".help"?for?instructions?? Enter?SQL?statements?terminated?with?a?";"?? sqlite>?create?table?user?? ???...>?(?? ???...>?id?integer?primary?key?autoincrement,?? ???...>?name?varchar(64),?? ???...>?age?integer?? ???...>?);?? sqlite>?.quit??
? sqlite3.h(在sqlite-amalgamation-3071300.zip壓縮包中)添加到工程。
? sqlite3.lib復制到工程文件夾下。
? 工程屬性中添加sqlite3.lib庫依賴。
Configuration Properties->Linker->Input->Additional Dependencies添加sqlite3.lib。
? 程序代碼為:
[cpp]?view plaincopy /*? @brief?本程序測試sqlite數據庫的增刪改查? @date?2012-09-03? */?? //?SQLiteTest.cpp?:?Defines?the?entry?point?for?the?console?application.?? //??? ?? #include?"stdafx.h"?? #include?"sqlite3.h"?? #include?<iostream>?? using?namespace?std;?? ?? sqlite3?*?pDB?=?NULL;?? ?? //增加用戶?? bool?AddUser(const?string&?sName,?const?string&?sAge);?? //刪除用戶?? bool?DeleteUser(const?string&?sName);?? //修改用戶?? bool?ModifyUser(const?string&?sName,?const?string&?sAge);?? //查找用戶?? bool?SelectUser();?? ?? int?_tmain(int?argc,?_TCHAR*?argv[])?? {?? ????//打開路徑采用utf-8編碼?? ????//如果路徑中包含中文,需要進行編碼轉換?? ????int?nRes?=?sqlite3_open("D:\\sqlite\\test.db",?&pDB);?? ????if?(nRes?!=?SQLITE_OK)?? ????{?? ????????cout<<"Open?database?fail:?"<<sqlite3_errmsg(pDB);?? ????????goto?QUIT;?? ????}?? ?? ????//添加“趙錢孫李”?? ????if?(????!AddUser("zhao",?"18")?? ????????||?!AddUser("qian",?"19")?? ????????||?!AddUser("sun",?"20")?? ????????||?!AddUser("li",?"21"))?? ????{?? ????????goto?QUIT;?? ????}?? ?? ????//刪除“趙”?? ????if?(!DeleteUser("zhao"))?? ????{?? ????????goto?QUIT;?? ????}?? ?? ????//修改“孫”?? ????if?(!ModifyUser("sun",?"15"))?? ????{?? ????????goto?QUIT;?? ????}?? ?? ????//查找用戶?? ????if?(!SelectUser())?? ????{?? ????????goto?QUIT;?? ????}?? ?? QUIT:?? ????sqlite3_close(pDB);?? ?? ????return?0;?? }?? ?? bool?AddUser(const?string&?sName,?const?string&?sAge)?? {?? ????string?strSql?=?"";?? ????strSql?+=?"insert?into?user(name,age)";?? ????strSql?+=?"values('";?? ????strSql?+=?sName;?? ????strSql?+=?"',";?? ????strSql?+=?sAge;?? ????strSql?+=?");";?? ?? ????char*?cErrMsg;?? ????int?nRes?=?sqlite3_exec(pDB?,?strSql.c_str()?,0?,0,?&cErrMsg);?? ????if?(nRes?!=?SQLITE_OK)???? ????{?? ????????cout<<"add?user?fail:?"<<cErrMsg<<endl;?? ????????return?false;?? ????}?? ????else?? ????{?? ????????cout<<"add?user?success:?"<<sName.c_str()<<"\t"<<sAge.c_str()<<endl;?? ????}?? ?? ????return?true;?? }?? ?? bool?DeleteUser(const?string&?sName)?? {?? ????string?strSql?=?"";?? ????strSql?+=?"delete?from?user?where?name='";?? ????strSql?+=?sName;?? ????strSql?+=?"';";?? ?? ????char*?cErrMsg;?? ????int?nRes?=?sqlite3_exec(pDB?,?strSql.c_str()?,0?,0,?&cErrMsg);?? ????if?(nRes?!=?SQLITE_OK)???? ????{?? ????????cout<<"delete?user?fail:?"<<cErrMsg<<endl;?? ????????return?false;?? ????}?? ????else?? ????{?? ????????cout<<"delete?user?success:?"<<sName.c_str()<<endl;?? ????}?? ?? ????return?true;?? }?? ?? bool?ModifyUser(const?string&?sName,?const?string&?sAge)?? {?? ????string?strSql?=?"";?? ????strSql?+=?"update?user?set?age?=";?? ????strSql?+=?sAge;?? ????strSql?+=?"?where?name='";?? ????strSql?+=?sName;?? ????strSql?+=?"';";?? ?? ????char*?cErrMsg;?? ????int?nRes?=?sqlite3_exec(pDB?,?strSql.c_str()?,0?,0,?&cErrMsg);?? ????if?(nRes?!=?SQLITE_OK)???? ????{?? ????????cout<<"modify?user?fail:?"<<cErrMsg<<endl;?? ????????return?false;?? ????}?? ????else?? ????{?? ????????cout<<"modify?user?success:?"<<sName.c_str()<<"\t"<<sAge.c_str()<<endl;?? ????}?? ?? ????return?true;?? }?? ?? static?int?UserResult(void?*NotUsed,?int?argc,?char?**argv,?char?**azColName)?? {?? ????for(int?i?=?0?;?i?<?argc?;?i++)?? ????{?? ????????cout<<azColName[i]<<"?=?"<<(argv[i]???argv[i]?:?"NULL")<<",?";?? ????}?? ????cout<<endl;?? ?? ????return?0;?? }?? ?? bool?SelectUser()?? {?? ????char*?cErrMsg;?? ????int?res?=?sqlite3_exec(pDB,?"select?*?from?user;",?UserResult?,?0?,?&cErrMsg);???? ?? ????if?(res?!=?SQLITE_OK)?? ????{?? ????????cout<<"select?fail:?"<<cErrMsg<<endl;?? ????????return?false;?? ????}?? ?? ????return?true;?? }??
? 編譯成功后,將sqlite3.dll復制到SQLiteTest.exe同一目錄下,運行SQLiteTest.exe。
運行結果:
[plain]?view plaincopy add?user?success:?zhao??18?? add?user?success:?qian??19?? add?user?success:?sun???20?? add?user?success:?li????21?? delete?user?success:?zhao?? modify?user?success:?sun????????15?? id?=?2,?name?=?qian,?age?=?19,?? id?=?3,?name?=?sun,?age?=?15,?? id?=?4,?name?=?li,?age?=?21,??
開發語言:C++。
1、 下載sqlite文件。
下載網址:http://www.sqlite.org/download.html。SQLite版本為SQLite 3.7.13,相關文件如下。
sqlite-dll-win32-x86-3071300.zip:包含sqlite3.def、sqlite3.dll文件。
sqlite-amalgamation-3071300.zip:包含sqlite3.h 文件。
sqlite-shell-win32-x86-3071300.zip:包含sqlite3.exe 文件。
2、 生成sqlite3.lib。
? sqlite-dll-win32-x86-3071300.zip文件解壓到D:\ sqlite。? 運行Visual Studio 2005 Command Prompt命令行程序。
啟動位置:開始程序->Microsoft Visual Studio 2005->Visual Studio Tools->Visual Studio 2005 Command Prompt。
? 依次執行控制臺命令。
[plain]?view plaincopy
即可生成sqlite3.lib文件。
3、 創建測試數據。
? sqlite-shell-win32-x86-3071300.zip文件解壓到D:\ sqlite。? 啟動命令行,進入D:\ sqlite目錄。
命令依次為:
[plain]?view plaincopy
創建user表。
| 字段Code | 字段類型 | 字段描述 |
| id | integer | 主鍵,自增 |
| name | varchar(64) | 用戶名 |
| age | integer | 年齡 |
[plain]?view plaincopy
4、 創建示例工程
? 創建win32控制臺工程SQLiteTest。? sqlite3.h(在sqlite-amalgamation-3071300.zip壓縮包中)添加到工程。
? sqlite3.lib復制到工程文件夾下。
? 工程屬性中添加sqlite3.lib庫依賴。
Configuration Properties->Linker->Input->Additional Dependencies添加sqlite3.lib。
? 程序代碼為:
[cpp]?view plaincopy
? 編譯成功后,將sqlite3.dll復制到SQLiteTest.exe同一目錄下,運行SQLiteTest.exe。
運行結果:
[plain]?view plaincopy
5、 SQLite管理工具
可視化管理工具,推薦使用:SQLite Expert,見:http://www.sqliteexpert.com/。
總結
以上是生活随笔為你收集整理的C++使用SQLite步骤及示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ODBC学习(一)基本理论
- 下一篇: C++操作SQLite简明教程