最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C/C++通過SQLite SDK實現(xiàn)數(shù)據(jù)庫增刪改查操作

 更新時間:2023年11月27日 09:19:20   作者:微軟技術(shù)分享  
SQLite,作為一款嵌入式關(guān)系型數(shù)據(jù)庫管理系統(tǒng),一直以其輕量級、零配置以及跨平臺等特性而備受青睞,本文主要介紹了C++如何通過SQLite SDK實現(xiàn)數(shù)據(jù)庫增刪改查操作,感興趣的可以了解下

SQLite,作為一款嵌入式關(guān)系型數(shù)據(jù)庫管理系統(tǒng),一直以其輕量級、零配置以及跨平臺等特性而備受青睞。不同于傳統(tǒng)的數(shù)據(jù)庫系統(tǒng),SQLite是一個庫,直接與應(yīng)用程序一同編譯和鏈接,無需單獨的數(shù)據(jù)庫服務(wù)器進程,實現(xiàn)了數(shù)據(jù)庫的零配置管理。這種設(shè)計理念使得SQLite成為許多嵌入式系統(tǒng)、移動應(yīng)用和小型項目中的首選數(shù)據(jù)庫引擎。

SQLite的特點包括:

  • 嵌入式數(shù)據(jù)庫引擎: SQLite 是一個嵌入式數(shù)據(jù)庫引擎,意味著它是一個庫,與應(yīng)用程序一同編譯和鏈接,而不是作為獨立的進程運行。
  • 零配置: 無需配置或管理。SQLite 不需要一個獨立的數(shù)據(jù)庫服務(wù)器進程,所有的操作都是直接在存儲在文件中的數(shù)據(jù)庫上執(zhí)行。
  • 輕量級: SQLite 是一個輕量級的數(shù)據(jù)庫,相對于一些其他數(shù)據(jù)庫管理系統(tǒng)來說,它的內(nèi)存占用和資源消耗相對較小。
  • 支持 SQL: SQLite 支持大部分標準的 SQL 語法,并提供了事務(wù)支持,包括隔離級別和回滾。
  • 跨平臺: SQLite 可以在各種操作系統(tǒng)上運行,包括 Windows、Linux、macOS 和其他嵌入式系統(tǒng)。
  • 自給自足: SQLite 數(shù)據(jù)庫是一個單一的磁盤文件,整個數(shù)據(jù)庫被存儲在一個文件中,這使得備份、復(fù)制或傳輸數(shù)據(jù)庫變得非常容易。
  • 開源: SQLite 是一個開源項目,采用公共領(lǐng)域授權(quán)(Public Domain License),可以在商業(yè)和非商業(yè)項目中免費使用。

SQLite 數(shù)據(jù)庫以其獨特的自給自足特性脫穎而出,整個數(shù)據(jù)庫被存儲在一個單一的磁盤文件中,使得備份、復(fù)制或傳輸數(shù)據(jù)庫變得異常簡單。而作為一款開源項目,SQLite采用了公共領(lǐng)域授權(quán),可以在商業(yè)和非商業(yè)項目中免費使用。

由于該數(shù)據(jù)庫的小巧和簡潔所以在使用上也非常容易,當讀者下載好附件以后會看到如下圖所示的文件;

使用時只需要將sqlite3.hsqlite3.c文件導(dǎo)入到項目中并使用#include "sqlite3.h"即可,無需做其他配置,圖中的sqlite3.dll是動態(tài)庫,sqlite3.exe則是一個命令行版本的數(shù)據(jù)庫可在測試時使用它。

打開與關(guān)閉庫

sqlite3_open 用于打開或創(chuàng)建一個 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)建一個新的數(shù)據(jù)庫文件。
  • ppDb: 用于存儲 SQLite 數(shù)據(jù)庫句柄(handle)的指針。SQLite 數(shù)據(jù)庫句柄是與一個打開的數(shù)據(jù)庫關(guān)聯(lián)的結(jié)構(gòu),它在后續(xù)的 SQLite 操作中用作標識。

該函數(shù)返回一個整數(shù)值,代表函數(shù)的執(zhí)行狀態(tài)。如果函數(shù)成功執(zhí)行,返回 SQLITE_OK。如果有錯誤發(fā)生,返回一個表示錯誤代碼的整數(shù)值。可以通過 sqlite3_errmsg 函數(shù)獲取更詳細的錯誤信息。

sqlite3_close 用于關(guān)閉數(shù)據(jù)庫連接的函數(shù)。其原型如下:

int sqlite3_close(sqlite3*);

sqlite3: 要關(guān)閉的 SQLite 數(shù)據(jù)庫連接的句柄。

該函數(shù)返回一個整數(shù)值,用于表示函數(shù)的執(zhí)行狀態(tài)。如果函數(shù)成功執(zhí)行,返回 SQLITE_OK。如果有錯誤發(fā)生,返回一個表示錯誤代碼的整數(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: 用于存儲錯誤消息的指針。

sqlite3_exec 函數(shù)執(zhí)行一個或多個 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é)果集中當前行的列值的數(shù)組 */
  char** column_names    /* 指向結(jié)果集中列名的數(shù)組 */
);
  • callback_param: 回調(diào)函數(shù)的參數(shù),由 sqlite3_exec 傳遞給回調(diào)函數(shù)。
  • num_columns: 結(jié)果集中的列數(shù)。
  • column_values: 指向結(jié)果集中當前行的列值的數(shù)組。
  • column_names: 指向結(jié)果集中列名的數(shù)組。

回調(diào)函數(shù)返回一個整數(shù),用于指示是否繼續(xù)執(zhí)行后續(xù)的 SQL 語句。如果回調(diào)函數(shù)返回非零值,sqlite3_exec 將停止執(zhí)行 SQL,并立即返回。

sqlite3_prepare_v2 用于準備 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: 要準備的 SQL 語句,以 UTF-8 編碼。
  • sql_len: SQL 語句的長度,如果為 -1,則表示 SQL 語句以 null 結(jié)尾。
  • stmt: 用于存儲準備好的語句句柄的指針。
  • tail: 用于存儲未使用的 SQL 語句的指針。

sqlite3_prepare_v2 函數(shù)用于將 SQL 語句編譯成一個 SQLite 語句對象(prepared statement)。這個對象可以被多次執(zhí)行,每次執(zhí)行時可以綁定不同的參數(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ù)可用(用于非查詢語句)。
  • 其他錯誤碼,表示執(zhí)行過程中出現(xiàn)了錯誤。

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 用于釋放一個預(yù)備語句對象(prepared statement)。在使用 sqlite3_prepare_v2 函數(shù)準備 SQL 語句后,需要使用 sqlite3_finalize 來釋放相應(yīng)的語句對象。

該函數(shù)的原型為:

int sqlite3_finalize(sqlite3_stmt *pStmt);

sqlite3_stmt *pStmt: 指向要釋放的語句對象的指針。

該函數(shù)返回 SQLITE_OK 表示成功,返回其他錯誤碼表示失敗。

// 執(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自動計算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)建了一個名為 "LySharkDB" 的表,該表格包含了id、name、age 和 msg四個字段。隨后,通過執(zhí)行 SQL 語句創(chuàng)建了這個表格。最后,關(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"
      ")";

    // 運行創(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;
}

上述代碼運行后則可以創(chuàng)建一個數(shù)據(jù)庫database.db表名為LySharkDB讀者可以使用數(shù)據(jù)庫工具打開該表,其結(jié)構(gòu)如下所示;

插入數(shù)據(jù)測試

創(chuàng)建數(shù)據(jù)庫后,接著就是插入數(shù)據(jù)測試,插入時可以使用insert_data,如下代碼項數(shù)據(jù)庫中插入5條記錄;

int main(int argc, char *argv[])
{
  sqlite3* open_db = open_database("./database.db");
  if (open_db != false)
  {
    bool create_table_ref;

    // 運行插入記錄
    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語句實現(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ù),如下代碼中新建一個TimeDB數(shù)據(jù)表,其中有三個字段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;

// 獲取本地時間日期
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ù)據(jù)表
void Insert_Test()
{
	sqlite3* open_db = open_database("./database.db");
	for (int x = 0; x < 1000; x++)
	{
		// 獲取本地日期時間
		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ù);

當有了數(shù)據(jù)則再查詢,代碼中Select_Time_List函數(shù)演示了如何通過時間查詢一個區(qū)間的數(shù)據(jù),并返回一個容器列表給被調(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自動計算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;
}

// 獲取本地時間日期
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ù)據(jù)表
void Insert_Test()
{
	sqlite3* open_db = open_database("./database.db");
	for (int x = 0; x < 1000; x++)
	{
		// 獲取本地日期時間
		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);
}

// 查詢時間區(qū)間并返回 傳入開始時間與結(jié)束時間,過濾出特定的記錄
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;

			// 取出第一個和第二個字段
			const unsigned char *time_text = sqlite3_column_text(stmt, 1);
			const int cpu_value = sqlite3_column_int(stmt, 2);

			// 放入一個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)的所有的負載情況
	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 << "時間區(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這個區(qū)間內(nèi)的數(shù)據(jù)信息,并返回一個map容器給被調(diào)用者,運行效果如下所示;

以上就是C/C++通過SQLite SDK實現(xiàn)數(shù)據(jù)庫增刪改查操作的詳細內(nèi)容,更多關(guān)于C++ SQLite數(shù)據(jù)庫增刪改查的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語言指針詳解及用法示例

    C語言指針詳解及用法示例

    這篇文章主要介紹了C語言指針詳解及用法示例,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價值。需要的朋友可以了解下。
    2017-11-11
  • C++之WSAAsyncSelect模型實例

    C++之WSAAsyncSelect模型實例

    這篇文章主要介紹了C++的WSAAsyncSelect模型,實例講述了socket與Windows消息機制的用法,需要的朋友可以參考下
    2014-10-10
  • 指向變量的常指針與指向常變量的指針詳細解析

    指向變量的常指針與指向常變量的指針詳細解析

    剛接觸到指針時,關(guān)于指向變量的常指針和指向常變量的指針容易混淆,所以整理下,希望能夠幫助自己也幫助到大家
    2013-10-10
  • C++簡明分析講解布爾類型及引用

    C++簡明分析講解布爾類型及引用

    布爾型(Boolean),是一種類型變量類型,引用(reference)就是C++對C語言的重要擴充。引用就是某一變量(目標)的一個別名,對引用的操作與對變量直接操作完全一樣
    2022-04-04
  • Qt音視頻開發(fā)之利用ffmpeg實現(xiàn)解碼本地攝像頭

    Qt音視頻開發(fā)之利用ffmpeg實現(xiàn)解碼本地攝像頭

    一開始用ffmpeg做的是視頻流的解析,后面增加了本地視頻文件的支持,到后面發(fā)現(xiàn)ffmpeg也是支持本地攝像頭設(shè)備的,所以本文就來用ffmpeg實現(xiàn)解碼本地攝像頭功能吧
    2023-03-03
  • C語言實現(xiàn)三子棋簡單小游戲

    C語言實現(xiàn)三子棋簡單小游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)三子棋簡單小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 詳解C語言中二分查找的運用技巧

    詳解C語言中二分查找的運用技巧

    本文主要介紹了二分查找在實際中的應(yīng)用,通過分析幾個應(yīng)用二分查找的實例,總結(jié)下能使用二分查找算法的一些共同點,感興趣的可以了解一下
    2022-03-03
  • C++算法之海量數(shù)據(jù)處理方法的總結(jié)分析

    C++算法之海量數(shù)據(jù)處理方法的總結(jié)分析

    本篇文章是對海量數(shù)據(jù)處理方法進行了詳細的總結(jié)與分析,需要的朋友參考下
    2013-05-05
  • C++?內(nèi)存泄漏調(diào)試方式

    C++?內(nèi)存泄漏調(diào)試方式

    這篇文章主要介紹了C++?內(nèi)存泄漏調(diào)試方式,C++和其他高級語言不同,需要自行管理內(nèi)存,項目大調(diào)用多,下文我們就來看看C++?內(nèi)存泄漏調(diào)試方式分享,需要的小伙伴可以參考一下
    2022-04-04
  • linux系統(tǒng)中c++寫日志文件功能分享

    linux系統(tǒng)中c++寫日志文件功能分享

    這篇文章主要介紹了linux系統(tǒng)中c++寫日志文件功能,簡化了glog,只保留了寫日志文件的功能,只是改寫了linux版本,需要的朋友可以參考下
    2014-03-03

最新評論

从化市| 汝南县| 安康市| 江安县| 秦皇岛市| 贞丰县| 革吉县| 湖口县| 许昌县| 景宁| 满洲里市| 郯城县| 游戏| 彩票| 玉山县| 天台县| 鄂伦春自治旗| 边坝县| 容城县| 南木林县| 沧州市| 米林县| 永平县| 长丰县| 房山区| 元谋县| 乐山市| 平陆县| 拉萨市| 吉林省| 丰宁| 扶绥县| 陆丰市| 滦南县| 仁化县| 定襄县| 湘阴县| 合江县| 丰原市| 徐汇区| 日喀则市|