生活随笔
收集整理的這篇文章主要介紹了
C++ :sqlite3使用:
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
sqlite3使用:
- 資源下載
- demo
- 調試
- 裸機中安裝Sqlite,exe執行程序調用
更詳細的sqlite3使用和MFC應用請轉到
鏈接:
資源下載
1、下載sqlite3源碼:http://www.sqlite.org/download.html
下載下圖所示需要的四份所需文件(X86和X64都要下載)
2.文件解壓:
f:盤新建sqlite文件夾并完成解壓
3.這里需要使用到vs2017安裝目錄下的lib.exe和一些其他的依賴庫;
直接將vs2017安裝目錄下的HostX64/x64文件夾拷貝(C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\Hostx64)到新文件夾(若x86則要拷貝x86對應的文件夾);
再將步驟2中的sqlite3.dll和sqlite3.def拷貝到這個新文件夾;
4.生成lib文件
在新文件夾中打開powershell,輸入:
.\lib
.exe
/def
:sqlite3
.def
/machine
:X64
(如果是x86則最后的參數要換成X86)
ps:如果powershell打開路徑不是lib.exe所在路徑會發生如下報錯
修改后:
5.生成文件
新生成文件:sqlite3.lib (75kb)
demo
創建數據表
1.進入到“F:\sqlite\sqlite-tools-win32-x86-3350500>”
2.開始執行創建語句如下圖:
3.新建DEMO 添加運行文件
在該目錄下找到剛剛生成的sqlite3.lib;
在sqlite-amalgamation-3180000目錄下找到sqlite3.h;
在sqlite-dll-win64-x64-3180000 目錄下找到sqlite3.dll;
將上述三個文件拷貝到vs2017的項目源碼目錄下:
參考鏈接1:https://blog.csdn.net/jqsad/article/details/72773264
參考鏈接2:https://blog.csdn.net/xionglifei2014/article/details/80665636
F
:\sqlite\sqlite
-tools
-win32
-x86
-3350500>sqlite3
.exe test
.db
SQLite version
3.35.5 2021-04-19 18:32:05
Enter
".help" for usage hints
.
sqlite
> create table user
...> (...> id integer primary key autoincrement
,...> name
varchar(64),...> age integer
...> );
sqlite
> .quit
#include <iostream>
#include "sqlite3.h"#pragma comment(lib, "sqlite3.lib")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 main() {int res
= sqlite3_open("test.db", &pDB
);if (res
!= SQLITE_OK
) {cout
<< "Open database fail!" << endl
;return 0;}else {cout
<< "Open database success" << endl
;}AddUser("zhao", "18");QUIT
:sqlite3_close(pDB
);system("pause");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;
}
調試
error LNK2019: 無法解析的外部符號 sqlite3_XXX,
裸機中安裝Sqlite,exe執行程序調用
Sqlite3文件下載:
https://www.sqlite.org/download.html
1.下載 sqlite-dll-win32-x86-3140100
2.下載 sqlite-tools-win32-x86-3140100
安裝:
先在C盤建一個文件夾 sqlite,
1.把 sqlite-dll-win32-x86-3140100 中解壓出來的兩個文件(sqlite3.def 和 sqlite3.dll)復制到剛才新建的目錄(C:\sqlite)中
2.把 sqlite-tools-win32-x86-3140100 中解壓出來的文件 sqlite3.exe 復制到C:\sqlite 目錄中
3.添加系統環境變量, 在Path變量值后面添加 C:\sqlite(分號不要忘記了),如下圖所示:
接下來測試一下是否安裝完成
運行cmd,切換到C盤根目錄 C:\sqlite,輸入sqlite3,然后回車,如果安裝成功的話,會顯示sqlite版本號
總結
以上是生活随笔為你收集整理的C++ :sqlite3使用:的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。