C/C++通過SQLite SDK實(shí)現(xiàn)數(shù)據(jù)庫增刪改查操作
SQLite,作為一款嵌入式關(guān)系型數(shù)據(jù)庫管理系統(tǒng),一直以其輕量級、零配置以及跨平臺等特性而備受青睞。不同于傳統(tǒng)的數(shù)據(jù)庫系統(tǒng),SQLite是一個(gè)庫,直接與應(yīng)用程序一同編譯和鏈接,無需單獨(dú)的數(shù)據(jù)庫服務(wù)器進(jìn)程,實(shí)現(xiàn)了數(shù)據(jù)庫的零配置管理。這種設(shè)計(jì)理念使得SQLite成為許多嵌入式系統(tǒng)、移動(dòng)應(yīng)用和小型項(xiàng)目中的首選數(shù)據(jù)庫引擎。
SQLite的特點(diǎn)包括:
- 嵌入式數(shù)據(jù)庫引擎: SQLite 是一個(gè)嵌入式數(shù)據(jù)庫引擎,意味著它是一個(gè)庫,與應(yīng)用程序一同編譯和鏈接,而不是作為獨(dú)立的進(jìn)程運(yùn)行。
- 零配置: 無需配置或管理。SQLite 不需要一個(gè)獨(dú)立的數(shù)據(jù)庫服務(wù)器進(jìn)程,所有的操作都是直接在存儲在文件中的數(shù)據(jù)庫上執(zhí)行。
- 輕量級: SQLite 是一個(gè)輕量級的數(shù)據(jù)庫,相對于一些其他數(shù)據(jù)庫管理系統(tǒng)來說,它的內(nèi)存占用和資源消耗相對較小。
- 支持 SQL: SQLite 支持大部分標(biāo)準(zhǔn)的 SQL 語法,并提供了事務(wù)支持,包括隔離級別和回滾。
- 跨平臺: SQLite 可以在各種操作系統(tǒng)上運(yùn)行,包括 Windows、Linux、macOS 和其他嵌入式系統(tǒng)。
- 自給自足: SQLite 數(shù)據(jù)庫是一個(gè)單一的磁盤文件,整個(gè)數(shù)據(jù)庫被存儲在一個(gè)文件中,這使得備份、復(fù)制或傳輸數(shù)據(jù)庫變得非常容易。
- 開源: SQLite 是一個(gè)開源項(xiàng)目,采用公共領(lǐng)域授權(quán)(Public Domain License),可以在商業(yè)和非商業(yè)項(xiàng)目中免費(fèi)使用。
SQLite 數(shù)據(jù)庫以其獨(dú)特的自給自足特性脫穎而出,整個(gè)數(shù)據(jù)庫被存儲在一個(gè)單一的磁盤文件中,使得備份、復(fù)制或傳輸數(shù)據(jù)庫變得異常簡單。而作為一款開源項(xiàng)目,SQLite采用了公共領(lǐng)域授權(quán),可以在商業(yè)和非商業(yè)項(xiàng)目中免費(fèi)使用。
由于該數(shù)據(jù)庫的小巧和簡潔所以在使用上也非常容易,當(dāng)讀者下載好附件以后會看到如下圖所示的文件;

使用時(shí)只需要將sqlite3.h與sqlite3.c文件導(dǎo)入到項(xiàng)目中并使用#include "sqlite3.h"即可,無需做其他配置,圖中的sqlite3.dll是動(dòng)態(tài)庫,sqlite3.exe則是一個(gè)命令行版本的數(shù)據(jù)庫可在測試時(shí)使用它。
打開與關(guān)閉庫
sqlite3_open 用于打開或創(chuàng)建一個(gè) SQLite 數(shù)據(jù)庫文件。該函數(shù)的原型如下:
int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ );
filename: 要打開或創(chuàng)建的 SQLite 數(shù)據(jù)庫文件的路徑。如果文件不存在,將會創(chuàng)建一個(gè)新的數(shù)據(jù)庫文件。ppDb: 用于存儲 SQLite 數(shù)據(jù)庫句柄(handle)的指針。SQLite 數(shù)據(jù)庫句柄是與一個(gè)打開的數(shù)據(jù)庫關(guān)聯(lián)的結(jié)構(gòu),它在后續(xù)的 SQLite 操作中用作標(biāo)識。
該函數(shù)返回一個(gè)整數(shù)值,代表函數(shù)的執(zhí)行狀態(tài)。如果函數(shù)成功執(zhí)行,返回 SQLITE_OK。如果有錯(cuò)誤發(fā)生,返回一個(gè)表示錯(cuò)誤代碼的整數(shù)值??梢酝ㄟ^ sqlite3_errmsg 函數(shù)獲取更詳細(xì)的錯(cuò)誤信息。
sqlite3_close 用于關(guān)閉數(shù)據(jù)庫連接的函數(shù)。其原型如下:
int sqlite3_close(sqlite3*);
sqlite3: 要關(guān)閉的 SQLite 數(shù)據(jù)庫連接的句柄。
該函數(shù)返回一個(gè)整數(shù)值,用于表示函數(shù)的執(zhí)行狀態(tài)。如果函數(shù)成功執(zhí)行,返回 SQLITE_OK。如果有錯(cuò)誤發(fā)生,返回一個(gè)表示錯(cuò)誤代碼的整數(shù)值。
使用 sqlite3_close 函數(shù)可以釋放與數(shù)據(jù)庫連接相關(guān)的資源,并確保數(shù)據(jù)庫文件被正確關(guān)閉。在關(guān)閉數(shù)據(jù)庫連接之前,應(yīng)該確保已經(jīng)完成了所有需要執(zhí)行的 SQL 語句,并在需要的情況下檢查執(zhí)行結(jié)果。
// 打開數(shù)據(jù)庫并返回句柄
sqlite3* open_database(std::string database_name)
{
int ref =-1;
sqlite3 *db = 0;
ref = sqlite3_open(database_name.c_str(), &db);
if (ref == SQLITE_OK)
return db;
return false;
}
// 關(guān)閉數(shù)據(jù)庫
bool close_database(sqlite3 *db)
{
int ref = sqlite3_close(db);
if (ref == SQLITE_OK)
return true;
return false;
}
執(zhí)行查詢語句
sqlite3_exec 用于執(zhí)行 SQL 語句的高級接口函數(shù)。它的原型如下:
int sqlite3_exec(
sqlite3* db, /* Database handle */
const char* sql, /* SQL statement, UTF-8 encoded */
int (*callback)( /* Callback function */
void*, /* Callback parameter */
int, /* Number of columns in the result set */
char**, /* Array of column values */
char** /* Array of column names */
),
void* callback_param, /* 1st argument to callback function */
char** errmsg /* Error msg written here */
);
db: SQLite 數(shù)據(jù)庫連接的句柄。sql: 要執(zhí)行的 SQL 語句,以 UTF-8 編碼。callback: 回調(diào)函數(shù),用于處理查詢結(jié)果的每一行數(shù)據(jù)。callback_param: 傳遞給回調(diào)函數(shù)的參數(shù)。errmsg: 用于存儲錯(cuò)誤消息的指針。
sqlite3_exec 函數(shù)執(zhí)行一個(gè)或多個(gè) SQL 語句,并對每一條語句的執(zhí)行結(jié)果調(diào)用指定的回調(diào)函數(shù)?;卣{(diào)函數(shù)的原型如下:
int callback( void* callback_param, /* 參數(shù),由 sqlite3_exec 傳遞給回調(diào)函數(shù) */ int num_columns, /* 結(jié)果集中的列數(shù) */ char** column_values, /* 指向結(jié)果集中當(dāng)前行的列值的數(shù)組 */ char** column_names /* 指向結(jié)果集中列名的數(shù)組 */ );
callback_param: 回調(diào)函數(shù)的參數(shù),由sqlite3_exec傳遞給回調(diào)函數(shù)。num_columns: 結(jié)果集中的列數(shù)。column_values: 指向結(jié)果集中當(dāng)前行的列值的數(shù)組。column_names: 指向結(jié)果集中列名的數(shù)組。
回調(diào)函數(shù)返回一個(gè)整數(shù),用于指示是否繼續(xù)執(zhí)行后續(xù)的 SQL 語句。如果回調(diào)函數(shù)返回非零值,sqlite3_exec 將停止執(zhí)行 SQL,并立即返回。
sqlite3_prepare_v2 用于準(zhǔn)備 SQL 語句的接口函數(shù)。它的原型如下:
int sqlite3_prepare_v2( sqlite3* db, /* Database handle */ const char* sql, /* SQL statement, UTF-8 encoded */ int sql_len, /* Length of SQL statement in bytes, or -1 for zero-terminated */ sqlite3_stmt** stmt, /* OUT: Statement handle */ const char** tail /* OUT: Pointer to unused portion of SQL statement */ );
db: SQLite 數(shù)據(jù)庫連接的句柄。sql: 要準(zhǔn)備的 SQL 語句,以 UTF-8 編碼。sql_len: SQL 語句的長度,如果為 -1,則表示 SQL 語句以 null 結(jié)尾。stmt: 用于存儲準(zhǔn)備好的語句句柄的指針。tail: 用于存儲未使用的 SQL 語句的指針。
sqlite3_prepare_v2 函數(shù)用于將 SQL 語句編譯成一個(gè) SQLite 語句對象(prepared statement)。這個(gè)對象可以被多次執(zhí)行,每次執(zhí)行時(shí)可以綁定不同的參數(shù)。stmt 參數(shù)將用于存儲編譯后的語句的句柄,以供后續(xù)的操作。
sqlite3_step 執(zhí)行預(yù)編譯 SQL 語句的接口函數(shù)。它的原型如下:
int sqlite3_step(sqlite3_stmt*);
sqlite3_stmt*: 由 sqlite3_prepare_v2 預(yù)編譯的 SQL 語句的句柄。
sqlite3_step 函數(shù)用于執(zhí)行由 sqlite3_prepare_v2 預(yù)編譯的 SQL 語句。在執(zhí)行過程中,可以通過不斷調(diào)用 sqlite3_step 來逐行獲取查詢結(jié)果,直到結(jié)果集結(jié)束。對于非查詢語句(如 INSERT、UPDATE、DELETE),sqlite3_step 函數(shù)執(zhí)行一次即可完成操作。
該函數(shù)的返回值表示執(zhí)行的結(jié)果,可能的返回值包括:
SQLITE_ROW: 成功獲取一行數(shù)據(jù)。SQLITE_DONE: 執(zhí)行完成,沒有更多的數(shù)據(jù)可用(用于非查詢語句)。- 其他錯(cuò)誤碼,表示執(zhí)行過程中出現(xiàn)了錯(cuò)誤。
sqlite3_column_text 用于獲取查詢結(jié)果集中某一列的文本值。其原型為:
const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
sqlite3_stmt*: 由sqlite3_prepare_v2預(yù)編譯的 SQL 語句的句柄。int iCol: 列的索引,從0開始。
該函數(shù)返回指向字符串值的指針,該字符串值是查詢結(jié)果集中指定列的文本表示。需要注意的是,返回的指針指向 SQLite 內(nèi)部的存儲區(qū),應(yīng)該在使用完之后盡早釋放資源。
sqlite3_column_int 用于獲取查詢結(jié)果集中某一列的整數(shù)值。其原型為:
int sqlite3_column_int(sqlite3_stmt*, int iCol);
sqlite3_stmt*: 由sqlite3_prepare_v2預(yù)編譯的 SQL 語句的句柄。int iCol: 列的索引,從0開始。
該函數(shù)返回查詢結(jié)果集中指定列的整數(shù)表示。需要注意的是,如果該列不是整數(shù)類型,或者包含的數(shù)據(jù)無法轉(zhuǎn)換為整數(shù),那么返回的結(jié)果可能不是有效的整數(shù)值。
sqlite3_finalize 用于釋放一個(gè)預(yù)備語句對象(prepared statement)。在使用 sqlite3_prepare_v2 函數(shù)準(zhǔn)備 SQL 語句后,需要使用 sqlite3_finalize 來釋放相應(yīng)的語句對象。
該函數(shù)的原型為:
int sqlite3_finalize(sqlite3_stmt *pStmt);
sqlite3_stmt *pStmt: 指向要釋放的語句對象的指針。
該函數(shù)返回 SQLITE_OK 表示成功,返回其他錯(cuò)誤碼表示失敗。
// 執(zhí)行SQL語句
bool exec_sql(sqlite3 *db, char *sql)
{
char *error_code = 0;
int ref = sqlite3_exec(db, sql, 0, 0, &error_code);
if (ref == SQLITE_OK)
{
return true;
}
return false;
}
// 插入數(shù)據(jù)
bool insert_data(sqlite3 *db, char *sql)
{
sqlite3_stmt *stmt = 0;
// 插入前檢查語句合法性, -1自動(dòng)計(jì)算SQL長度
int ref = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (ref == SQLITE_OK)
{
sqlite3_step(stmt); // 執(zhí)行語句
sqlite3_finalize(stmt); // 清理語句句柄
return true;
}
sqlite3_finalize(stmt);
return false;
}
// 查詢數(shù)據(jù)集
bool select_data(sqlite3 *db, char *sql)
{
sqlite3_stmt *stmt = 0;
int ref = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (ref == SQLITE_OK)
{
// 每調(diào)一次sqlite3_step()函數(shù),stmt就會指向下一條記錄
while (sqlite3_step(stmt) == SQLITE_ROW)
{
// 取出第1列字段的值
const unsigned char *name = sqlite3_column_text(stmt, 1);
// 取出第2列字段的值
int age = sqlite3_column_int(stmt, 2);
std::cout << "姓名: " << name << " 年齡: " << age << std::endl;
}
}
else
{
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
調(diào)用查詢語句
創(chuàng)建數(shù)據(jù)庫
首先打開了名為 "database.db" 的 SQLite 數(shù)據(jù)庫,并創(chuàng)建了一個(gè)名為 "LySharkDB" 的表,該表格包含了id、name、age 和 msg四個(gè)字段。隨后,通過執(zhí)行 SQL 語句創(chuàng)建了這個(gè)表格。最后,關(guān)閉了數(shù)據(jù)庫連接。這段代碼主要用于數(shù)據(jù)庫初始化操作,確保了數(shù)據(jù)庫中包含了指定的表格結(jié)構(gòu)。
int main(int argc, char *argv[])
{
sqlite3* open_db = open_database("database.db");
if (open_db != false)
{
bool create_table_ref;
std::string sql =
"create table LySharkDB("
"id int auto_increment primary key,"
"name char(30) not null,"
"age int not null,"
"msg text default null"
")";
// 運(yùn)行創(chuàng)建表操作
char run_sql[1024] = { 0 };
strcpy(run_sql, sql.c_str());
create_table_ref = exec_sql(open_db, run_sql);
}
close_database(open_db);
std::system("pause");
return 0;
}
上述代碼運(yùn)行后則可以創(chuàng)建一個(gè)數(shù)據(jù)庫database.db表名為LySharkDB讀者可以使用數(shù)據(jù)庫工具打開該表,其結(jié)構(gòu)如下所示;

插入數(shù)據(jù)測試
創(chuàng)建數(shù)據(jù)庫后,接著就是插入數(shù)據(jù)測試,插入時(shí)可以使用insert_data,如下代碼項(xiàng)數(shù)據(jù)庫中插入5條記錄;
int main(int argc, char *argv[])
{
sqlite3* open_db = open_database("./database.db");
if (open_db != false)
{
bool create_table_ref;
// 運(yùn)行插入記錄
if (create_table_ref == true)
{
bool insert_ref = 0;
insert_ref = insert_data(open_db, "insert into LySharkDB(id,name,age,msg) values(1,'lyshark',1,'hello lyshark');");
insert_ref = insert_data(open_db, "insert into LySharkDB(id,name,age,msg) values(2,'guest',2,'hello guest');");
insert_ref = insert_data(open_db, "insert into LySharkDB(id,name,age,msg) values(3,'admin',3,'hello admin');");
insert_ref = insert_data(open_db, "insert into LySharkDB(id,name,age,msg) values(4,'wang',4,'hello wang');");
insert_ref = insert_data(open_db, "insert into LySharkDB(id,name,age,msg) values(5,'sqlite',5,'hello sql');");
if (insert_ref == true)
{
std::cout << "插入完成" << std::endl;
}
}
}
close_database(open_db);
std::system("pause");
return 0;
}
插入后,打開數(shù)據(jù)庫管理軟件,可看到插入后的記錄;

查詢與刪除數(shù)據(jù)
而查詢刪除與增加記錄,我們這里直接使用exec_sql()函數(shù),通過傳入不同的SQL語句實(shí)現(xiàn)。
int main(int argc, char *argv[])
{
sqlite3* open_db = open_database("./database.db");
if (open_db != false)
{
// 刪除記錄
bool delete_ref = exec_sql(open_db, "delete from LySharkDB where id = 5;");
if (delete_ref == true)
{
std::cout << "刪除完成." << std::endl;
}
// 更新記錄
bool update_ref = exec_sql(open_db, "update LySharkDB set name='lyshark' where id = 4;");
if (update_ref == true)
{
std::cout << "更新完成." << std::endl;
}
// 查詢數(shù)據(jù)
bool select_ref = select_data(open_db, "select * from LySharkDB;");
if (select_ref == true)
{
std::cout << "查詢完畢." << std::endl;
}
}
close_database(open_db);
std::system("pause");
return 0;
}
執(zhí)行更新后的表記錄如下所示;

查詢區(qū)間數(shù)據(jù)
首先創(chuàng)建一些數(shù)據(jù)集,這里通過循環(huán)生成并插入數(shù)據(jù),如下代碼中新建一個(gè)TimeDB數(shù)據(jù)表,其中有三個(gè)字段uid,host_time,cpu_value;
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <time.h>
#include "sqlite3.h"
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
using namespace std;
using namespace boost;
// 獲取本地時(shí)間日期
std::string get_local_datetime()
{
char ct[80];
time_t tt;
struct tm *tblock;
tt = time(NULL);
tblock = localtime(&tt);
strftime(ct, sizeof(ct), "%Y-%m-%d %H:%M:%S", tblock);
return ct;
}
// 初始化創(chuàng)建表結(jié)構(gòu)
void Init_Database()
{
sqlite3* open_db = open_database("./database.db");
if (open_db != false)
{
bool create_table_ref;
std::string sql =
"create table TimeDB("
"uid primary key,"
"host_time char(128) not null,"
"cpu_value int not null"
");";
char run_sql[1024] = { 0 };
strcpy(run_sql, sql.c_str());
exec_sql(open_db, run_sql);
}
close_database(open_db);
}
// 批量生成時(shí)間字符串并插入數(shù)據(jù)表
void Insert_Test()
{
sqlite3* open_db = open_database("./database.db");
for (int x = 0; x < 1000; x++)
{
// 獲取本地日期時(shí)間
std::string local_times = get_local_datetime();
std::string format_string = boost::str(boost::format("insert into TimeDB(uid,host_time,cpu_value) values(%d,'%s',%d);") % x %local_times %x);
std::cout << "執(zhí)行SQL: " << format_string << std::endl;
char run_sql[1024] = { 0 };
strcpy(run_sql, format_string.c_str());
insert_data(open_db, run_sql);
_sleep(1000);
}
close_database(open_db);
}
int main(int argc, char *argv[])
{
sqlite3* open_db = open_database("./database.db");
Init_Database();
Insert_Test();
std::system("pause");
return 0;
}
如下是五分鐘的模擬數(shù)據(jù);

當(dāng)有了數(shù)據(jù)則再查詢,代碼中Select_Time_List函數(shù)演示了如何通過時(shí)間查詢一個(gè)區(qū)間的數(shù)據(jù),并返回一個(gè)容器列表給被調(diào)用者使用,查詢代碼如下所示;
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <time.h>
#include "sqlite3.h"
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
using namespace std;
using namespace boost;
// 打開數(shù)據(jù)庫并返回句柄
sqlite3* open_database(std::string database_name)
{
int ref = -1;
sqlite3 *db = 0;
ref = sqlite3_open(database_name.c_str(), &db);
if (ref == SQLITE_OK)
return db;
return false;
}
// 關(guān)閉數(shù)據(jù)庫
bool close_database(sqlite3 *db)
{
int ref = sqlite3_close(db);
if (ref == SQLITE_OK)
return true;
return false;
}
// 執(zhí)行SQL語句
bool exec_sql(sqlite3 *db, char *sql)
{
char *error_code = 0;
int ref = sqlite3_exec(db, sql, 0, 0, &error_code);
if (ref == SQLITE_OK)
{
return true;
}
return false;
}
// 插入數(shù)據(jù)
bool insert_data(sqlite3 *db, char *sql)
{
sqlite3_stmt *stmt = 0;
// 插入前檢查語句合法性, -1自動(dòng)計(jì)算SQL長度
int ref = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (ref == SQLITE_OK)
{
sqlite3_step(stmt); // 執(zhí)行語句
sqlite3_finalize(stmt); // 清理語句句柄
return true;
}
sqlite3_finalize(stmt);
return false;
}
// 查詢數(shù)據(jù)集
bool select_data(sqlite3 *db, char *sql)
{
sqlite3_stmt *stmt = 0;
int ref = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (ref == SQLITE_OK)
{
// 每調(diào)一次sqlite3_step()函數(shù),stmt就會指向下一條記錄
while (sqlite3_step(stmt) == SQLITE_ROW)
{
// 取出第1列字段的值
const unsigned char *name = sqlite3_column_text(stmt, 1);
// 取出第2列字段的值
int age = sqlite3_column_int(stmt, 2);
std::cout << "姓名: " << name << " 年齡: " << age << std::endl;
}
}
else
{
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
// 獲取本地時(shí)間日期
std::string get_local_datetime()
{
char ct[80];
time_t tt;
struct tm *tblock;
tt = time(NULL);
tblock = localtime(&tt);
strftime(ct, sizeof(ct), "%Y-%m-%d %H:%M:%S", tblock);
return ct;
}
// 初始化創(chuàng)建表結(jié)構(gòu)
void Init_Database()
{
sqlite3* open_db = open_database("./database.db");
if (open_db != false)
{
bool create_table_ref;
std::string sql =
"create table TimeDB("
"uid primary key,"
"host_time char(128) not null,"
"cpu_value int not null"
");";
char run_sql[1024] = { 0 };
strcpy(run_sql, sql.c_str());
exec_sql(open_db, run_sql);
}
close_database(open_db);
}
// 批量生成時(shí)間字符串并插入數(shù)據(jù)表
void Insert_Test()
{
sqlite3* open_db = open_database("./database.db");
for (int x = 0; x < 1000; x++)
{
// 獲取本地日期時(shí)間
std::string local_times = get_local_datetime();
std::string format_string = boost::str(boost::format("insert into TimeDB(uid,host_time,cpu_value) values(%d,'%s',%d);") % x %local_times %x);
std::cout << "執(zhí)行SQL: " << format_string << std::endl;
char run_sql[1024] = { 0 };
strcpy(run_sql, format_string.c_str());
insert_data(open_db, run_sql);
_sleep(1000);
}
close_database(open_db);
}
// 查詢時(shí)間區(qū)間并返回 傳入開始時(shí)間與結(jié)束時(shí)間,過濾出特定的記錄
bool Select_Time_List(sqlite3 *db, std::vector<std::map<std::string, int>> &time_ref, std::string start_time, std::string end_time)
{
sqlite3_stmt *stmt = 0;
std::string format_string = boost::str(boost::format("select * from TimeDB where host_time >= '%s' and host_time <= '%s';") % start_time %end_time);
char run_sql[1024] = { 0 };
strcpy(run_sql, format_string.c_str());
int ref = sqlite3_prepare_v2(db, run_sql, -1, &stmt, 0);
if (ref == SQLITE_OK)
{
while (sqlite3_step(stmt) == SQLITE_ROW)
{
std::map < std::string, int > ptr;
// 取出第一個(gè)和第二個(gè)字段
const unsigned char *time_text = sqlite3_column_text(stmt, 1);
const int cpu_value = sqlite3_column_int(stmt, 2);
// 放入一個(gè)map容器中
ptr[boost::lexical_cast<std::string>(time_text)] = cpu_value;
time_ref.push_back(ptr);
}
sqlite3_finalize(stmt);
return true;
}
sqlite3_finalize(stmt);
return false;
}
int main(int argc, char *argv[])
{
sqlite3* open_db = open_database("./database.db");
//Init_Database();
//Insert_Test();
// 查詢 2023-11-25 19:52:31 - 2023-11-25 19:53:35 區(qū)間內(nèi)的所有的負(fù)載情況
std::vector<std::map<std::string, int>> db_time;
bool is_true = Select_Time_List(open_db, db_time, "2023-11-25 19:52:31", "2023-11-25 19:53:35");
if (is_true == true)
{
for (int x = 0; x < db_time.size(); x++)
{
// 輸出該區(qū)間內(nèi)的數(shù)據(jù)
std::map < std::string, int>::iterator ptr;
for (ptr = db_time[x].begin(); ptr != db_time[x].end(); ptr++)
{
std::cout << "時(shí)間區(qū)間: " << ptr->first << " CPU利用率: " << ptr->second << std::endl;
}
}
}
std::system("pause");
return 0;
}
例如代碼中我們查詢2023-11-25 19:52:31 - 2023-11-25 19:53:35這個(gè)區(qū)間內(nèi)的數(shù)據(jù)信息,并返回一個(gè)map容器給被調(diào)用者,運(yùn)行效果如下所示;

以上就是C/C++通過SQLite SDK實(shí)現(xiàn)數(shù)據(jù)庫增刪改查操作的詳細(xì)內(nèi)容,更多關(guān)于C++ SQLite數(shù)據(jù)庫增刪改查的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++實(shí)現(xiàn)圖像目標(biāo)區(qū)裁剪ImageCropping
本文主要介紹了C++實(shí)現(xiàn)圖像目標(biāo)區(qū)裁剪ImageCropping,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C++11標(biāo)準(zhǔn)庫 互斥鎖 <mutex> 詳解
這篇文章主要介紹了C++11標(biāo)準(zhǔn)庫互斥鎖 <mutex> 的相關(guān)知識,使用call_once()的時(shí)候,需要一個(gè)once_flag作為call_once()的傳入?yún)?shù),本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-07-07
關(guān)于C語言 const 和 define 區(qū)別
這篇文章主要介紹了關(guān)于C語言 const 和 define 區(qū)別 的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09
教你在VS2022?MFC程序中調(diào)用CUDA代碼的方法
這篇文章主要介紹了在VS2022?MFC程序中調(diào)用CUDA代碼,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
解析C++編程中virtual聲明的虛函數(shù)以及單個(gè)繼承
這篇文章主要介紹了C++編程中virtual聲明的虛函數(shù)以及單個(gè)繼承,剖析虛函數(shù)和單個(gè)基類所能夠繼承的成員,要的朋友可以參考下2016-01-01

