网站加首页,青青网站怎么做,天津做填料的公司,国际货代做网站SQLite#xff0c;作为一款嵌入式关系型数据库管理系统#xff0c;一直以其轻量级、零配置以及跨平台等特性而备受青睐。不同于传统的数据库系统#xff0c;SQLite是一个库#xff0c;直接与应用程序一同编译和链接#xff0c;无需单独的数据库服务器进程#xff0c;实现…SQLite作为一款嵌入式关系型数据库管理系统一直以其轻量级、零配置以及跨平台等特性而备受青睐。不同于传统的数据库系统SQLite是一个库直接与应用程序一同编译和链接无需单独的数据库服务器进程实现了数据库的零配置管理。这种设计理念使得SQLite成为许多嵌入式系统、移动应用和小型项目中的首选数据库引擎。
SQLite的特点包括
嵌入式数据库引擎 SQLite 是一个嵌入式数据库引擎意味着它是一个库与应用程序一同编译和链接而不是作为独立的进程运行。零配置 无需配置或管理。SQLite 不需要一个独立的数据库服务器进程所有的操作都是直接在存储在文件中的数据库上执行。轻量级 SQLite 是一个轻量级的数据库相对于一些其他数据库管理系统来说它的内存占用和资源消耗相对较小。支持 SQL SQLite 支持大部分标准的 SQL 语法并提供了事务支持包括隔离级别和回滚。跨平台 SQLite 可以在各种操作系统上运行包括 Windows、Linux、macOS 和其他嵌入式系统。自给自足 SQLite 数据库是一个单一的磁盘文件整个数据库被存储在一个文件中这使得备份、复制或传输数据库变得非常容易。开源 SQLite 是一个开源项目采用公共领域授权Public Domain License可以在商业和非商业项目中免费使用。
SQLite 数据库以其独特的自给自足特性脱颖而出整个数据库被存储在一个单一的磁盘文件中使得备份、复制或传输数据库变得异常简单。而作为一款开源项目SQLite采用了公共领域授权可以在商业和非商业项目中免费使用。
SQLiteSDK下载https://download.csdn.net/download/lyshark_csdn/88568197
由于该数据库的小巧和简洁所以在使用上也非常容易当读者下载好附件以后会看到如下图所示的文件 使用时只需要将sqlite3.h与sqlite3.c文件导入到项目中并使用#include sqlite3.h即可无需做其他配置图中的sqlite3.dll是动态库sqlite3.exe则是一个命令行版本的数据库可在测试时使用它。
打开与关闭库
sqlite3_open 用于打开或创建一个 SQLite 数据库文件。该函数的原型如下
int sqlite3_open(const char *filename, /* Database filename (UTF-8) */sqlite3 **ppDb /* OUT: SQLite db handle */
);filename: 要打开或创建的 SQLite 数据库文件的路径。如果文件不存在将会创建一个新的数据库文件。ppDb: 用于存储 SQLite 数据库句柄handle的指针。SQLite 数据库句柄是与一个打开的数据库关联的结构它在后续的 SQLite 操作中用作标识。
该函数返回一个整数值代表函数的执行状态。如果函数成功执行返回 SQLITE_OK。如果有错误发生返回一个表示错误代码的整数值。可以通过 sqlite3_errmsg 函数获取更详细的错误信息。
sqlite3_close 用于关闭数据库连接的函数。其原型如下
int sqlite3_close(sqlite3*);sqlite3: 要关闭的 SQLite 数据库连接的句柄。
该函数返回一个整数值用于表示函数的执行状态。如果函数成功执行返回 SQLITE_OK。如果有错误发生返回一个表示错误代码的整数值。
使用 sqlite3_close 函数可以释放与数据库连接相关的资源并确保数据库文件被正确关闭。在关闭数据库连接之前应该确保已经完成了所有需要执行的 SQL 语句并在需要的情况下检查执行结果。
// 打开数据库并返回句柄
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;
}// 关闭数据库
bool close_database(sqlite3 *db)
{int ref sqlite3_close(db);if (ref SQLITE_OK)return true;return false;
}执行查询语句
sqlite3_exec 用于执行 SQL 语句的高级接口函数。它的原型如下
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 数据库连接的句柄。sql: 要执行的 SQL 语句以 UTF-8 编码。callback: 回调函数用于处理查询结果的每一行数据。callback_param: 传递给回调函数的参数。errmsg: 用于存储错误消息的指针。
sqlite3_exec 函数执行一个或多个 SQL 语句并对每一条语句的执行结果调用指定的回调函数。回调函数的原型如下
int callback(void* callback_param, /* 参数由 sqlite3_exec 传递给回调函数 */int num_columns, /* 结果集中的列数 */char** column_values, /* 指向结果集中当前行的列值的数组 */char** column_names /* 指向结果集中列名的数组 */
);callback_param: 回调函数的参数由 sqlite3_exec 传递给回调函数。num_columns: 结果集中的列数。column_values: 指向结果集中当前行的列值的数组。column_names: 指向结果集中列名的数组。
回调函数返回一个整数用于指示是否继续执行后续的 SQL 语句。如果回调函数返回非零值sqlite3_exec 将停止执行 SQL并立即返回。
sqlite3_prepare_v2 用于准备 SQL 语句的接口函数。它的原型如下
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 数据库连接的句柄。sql: 要准备的 SQL 语句以 UTF-8 编码。sql_len: SQL 语句的长度如果为 -1则表示 SQL 语句以 null 结尾。stmt: 用于存储准备好的语句句柄的指针。tail: 用于存储未使用的 SQL 语句的指针。
sqlite3_prepare_v2 函数用于将 SQL 语句编译成一个 SQLite 语句对象prepared statement。这个对象可以被多次执行每次执行时可以绑定不同的参数。stmt 参数将用于存储编译后的语句的句柄以供后续的操作。
sqlite3_step 执行预编译 SQL 语句的接口函数。它的原型如下
int sqlite3_step(sqlite3_stmt*);sqlite3_stmt*: 由 sqlite3_prepare_v2 预编译的 SQL 语句的句柄。
sqlite3_step 函数用于执行由 sqlite3_prepare_v2 预编译的 SQL 语句。在执行过程中可以通过不断调用 sqlite3_step 来逐行获取查询结果直到结果集结束。对于非查询语句如 INSERT、UPDATE、DELETEsqlite3_step 函数执行一次即可完成操作。
该函数的返回值表示执行的结果可能的返回值包括
SQLITE_ROW: 成功获取一行数据。SQLITE_DONE: 执行完成没有更多的数据可用用于非查询语句。其他错误码表示执行过程中出现了错误。
sqlite3_column_text 用于获取查询结果集中某一列的文本值。其原型为
const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);sqlite3_stmt*: 由 sqlite3_prepare_v2 预编译的 SQL 语句的句柄。int iCol: 列的索引从0开始。
该函数返回指向字符串值的指针该字符串值是查询结果集中指定列的文本表示。需要注意的是返回的指针指向 SQLite 内部的存储区应该在使用完之后尽早释放资源。
sqlite3_column_int 用于获取查询结果集中某一列的整数值。其原型为
int sqlite3_column_int(sqlite3_stmt*, int iCol);sqlite3_stmt*: 由 sqlite3_prepare_v2 预编译的 SQL 语句的句柄。int iCol: 列的索引从0开始。
该函数返回查询结果集中指定列的整数表示。需要注意的是如果该列不是整数类型或者包含的数据无法转换为整数那么返回的结果可能不是有效的整数值。
sqlite3_finalize 用于释放一个预备语句对象prepared statement。在使用 sqlite3_prepare_v2 函数准备 SQL 语句后需要使用 sqlite3_finalize 来释放相应的语句对象。
该函数的原型为
int sqlite3_finalize(sqlite3_stmt *pStmt);sqlite3_stmt *pStmt: 指向要释放的语句对象的指针。
该函数返回 SQLITE_OK 表示成功返回其他错误码表示失败。
// 执行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;
}// 插入数据
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); // 执行语句sqlite3_finalize(stmt); // 清理语句句柄return true;}sqlite3_finalize(stmt);return false;
}// 查询数据集
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){// 每调一次sqlite3_step()函数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;
}调用查询语句
创建数据库
首先打开了名为 database.db 的 SQLite 数据库并创建了一个名为 LySharkDB 的表该表格包含了id、name、age 和 msg四个字段。随后通过执行 SQL 语句创建了这个表格。最后关闭了数据库连接。这段代码主要用于数据库初始化操作确保了数据库中包含了指定的表格结构。
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);// 运行创建表操作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;
}上述代码运行后则可以创建一个数据库database.db表名为LySharkDB读者可以使用数据库工具打开该表其结构如下所示 插入数据测试
创建数据库后接着就是插入数据测试插入时可以使用insert_data如下代码项数据库中插入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;
}插入后打开数据库管理软件可看到插入后的记录 查询与删除数据
而查询删除与增加记录我们这里直接使用exec_sql()函数通过传入不同的SQL语句实现。
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 namelyshark where id 4;);if (update_ref true){std::cout 更新完成. std::endl;}// 查询数据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;
}执行更新后的表记录如下所示 查询区间数据
首先创建一些数据集这里通过循环生成并插入数据如下代码中新建一个TimeDB数据表其中有三个字段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.hppusing 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;
}// 初始化创建表结构
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);
}// 批量生成时间字符串并插入数据表
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 执行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;
}如下是五分钟的模拟数据 当有了数据则再查询代码中Select_Time_List函数演示了如何通过时间查询一个区间的数据并返回一个容器列表给被调用者使用查询代码如下所示
#include iostream
#include string
#include map
#include vector
#include time.h
#include sqlite3.h#include boost/lexical_cast.hpp
#include boost/format.hppusing namespace std;
using namespace boost;// 打开数据库并返回句柄
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;
}// 关闭数据库
bool close_database(sqlite3 *db)
{int ref sqlite3_close(db);if (ref SQLITE_OK)return true;return false;
}// 执行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;
}// 插入数据
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); // 执行语句sqlite3_finalize(stmt); // 清理语句句柄return true;}sqlite3_finalize(stmt);return false;
}// 查询数据集
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){// 每调一次sqlite3_step()函数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;
}// 初始化创建表结构
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);
}// 批量生成时间字符串并插入数据表
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 执行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);
}// 查询时间区间并返回 传入开始时间与结束时间,过滤出特定的记录
bool Select_Time_List(sqlite3 *db, std::vectorstd::mapstd::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_caststd::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 区间内的所有的负载情况std::vectorstd::mapstd::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){// 输出该区间内的数据std::map std::string, int::iterator ptr;for (ptr db_time[x].begin(); ptr ! db_time[x].end(); ptr){std::cout 时间区间: ptr-first CPU利用率: ptr-second std::endl;}}}std::system(pause);return 0;
}例如代码中我们查询2023-11-25 19:52:31 - 2023-11-25 19:53:35这个区间内的数据信息并返回一个map容器给被调用者运行效果如下所示