sqlite 轻量级数据库
在Linux上apt install sqlite 即可
在空白文件夹输入 sqlite test.db 创建并进入Test.db的编辑
.show 显示当前配置
.tables 显示当前已有数据库
select * from company 显示company库中的数据
创建company表
sqlite>
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
向表中插入信息
INSERT INTO COMPANY VALUES (2, 'Allen', 25, 'Texas', 15000.00 );
INSERT INTO COMPANY VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );
INSERT INTO COMPANY VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );
INSERT INTO COMPANY VALUES (5, 'David', 27, 'Texas', 85000.00 );
INSERT INTO COMPANY VALUES (6, 'Kim', 22, 'South-Hall', 45000.00 );
INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 );
修改显示配置项以便更清楚的查看数据库表格
sqlite> .header on //输出表头
.mode column //制表输出
.width 10, 20, 10 //设置第一列的宽度为 10,第二列的宽度为 20,第三列的宽度为 10
select * from company
select id,name,salary from company
select * from company where age>=25 and salary>=10000
查看表单详细信息
sqlite>.schema COMPANY
删除表单
sqlite>DROP TABLE COMPANY;
使用 SQLite .dump 点命令来导出完整的数据库在一个文本文件中
$sqlite3 test.db .dump > test.sql
抑或反向操作
$sqlite3 testDB.db < testDB.sql
// 24+16+13+13+1+2+5+8 =82 *0.78 =60
// 26+27+22+14+18+9+4+13 =124*0.69 =87
C++环境配置
从这里下载 "https://www.sqlite.org/download.html" 三个文件
"https://www.sqlite.org/2019/sqlite-amalgamation-3270200.zip" //源码
shell.c sqlite3.c sqlite3.h sqlite3ext.h
"https://www.sqlite.org/2019/sqlite-dll-win64-x64-3270200.zip" //dll、def 用于生成lib文件
sqlite3.dll sqlite3.def
"https://www.sqlite.org/2019/sqlite-tools-win32-x86-3270200.zip" //Windows可执行程序,添加环境用
sqlite3.exe sqldiff.exe sqlite3_analyzer.exe
![](/qrcode.jpg)
全部解压出来的文件如上
1、首先在Windows环境变量 path 中添加 sqlite3.exe 的路径,让cmd和powershell 都能执行sqlite3指令
2、在vs工程中新建filter 添加源码包中的四个.c .h文件,并设置不进行预编译
3、在vs安装目录中找到lib.exe 来将 sqlite3.def 编译成 sqlite3.lib ,打开cmd或者powershell
32位系统用指令 lib.exe /def:sqlite3.def /machine:ix86
64位系统用指令 lib.exe /def:sqlite3.def /machine:x64 或者省略/machine:x64字段
*如果因为路径问题非常难搞,就将vs中的lib.exe link.exe link.exe.config mspdbcore.dll 复制到sqlite3.def 所在文件夹再执行以上命令
生成的sqlite3.lib 需要在vs工程 linker -> input -> additional dependencies 中引用
//-----------------------------------------------
C++工程代码样例
#include <sqlite3.h>
// callback:回调函数,每成功执行一次sql语句就执行一次callback函数
static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
int i;
for (i = 0; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main()
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test2.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}
else {
fprintf(stderr, "Opened database successfully\n");
}
const char *sql;
sql = "CREATE TABLE COMPANY(" \
"ID INT PRIMARY KEY NOT NULL," \
"NAME TEXT NOT NULL," \
"AGE INT NOT NULL," \
"ADDRESS CHAR(50)," \
"SALARY REAL );";
const char * insert = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \
"VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \
"VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); " \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
"VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
"VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else {
fprintf(stdout, "Table created successfully\n");
}
rc = sqlite3_exec(db, insert, callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else {
fprintf(stdout, "Records created successfully\n");
}
sqlite3_close(db);
}