在之前的文章中,给出了一个数据库知识的概览:《数据库概览》,本文介绍一些基础的、常用的
SQLite
数据库知识。
SQLite
基础
SQL
基础语法
- 插入
INSERT INTO 表名 (列名1,...)
VALUES (列1值,...);
- 修改
UPDATE 表名
SET 列名1= 列1值, ....
WHERE [条件表达式];
- 删除
DELETE FROM 表名
WHERE [条件表达式];
- 查询
c++\
SELECT 列名1, .... FROM 表名;
WHERE [条件表达式];</p>
<pre><code>* 创建表
++
CREATE TABLE 库名.表名(
列1名 类型 特征(主键、唯一、非空、自增、注释),
…
);
- 删除表
DROP TABLE 表名;
SQLite
接口函数
// 打开文件:如果文件存在,则尝试打开;如果文件不存在,则创建
// 注意:sqlite中这个文件就是一个库
// 不支持多线程访问,不支持多实例访问
SQLITE_API int sqlite3_open(
const char*filename, /* Database filename (UTF-8)*/
sqlite3**ppDb /* OUT: SQLite db handle*/
);
SQLITE_API int sqlite3_open16(
const void*filename, /* Database filename (UTF-16)*/
sqlite3**ppDb /* OUT: SQLite db handle*/
);
// 关闭文件, Open之后一定要close
SQLITE_API int sqlite3_close(sqlite3*);
// 执行操作
SQLITE_API int sqlite3_exec(
sqlite3*, /* An open database*/
const char*sql, /* SQL to be evaluated*/
int (*callback)(void*,int,char**,char**), /* Callback function*/
void*, /* 1st argument to callback*/
char**errmsg /* Error msg written here*/
);
SQLITE_API int sqlite3_prepare(
sqlite3*db, /* Database handle*/
const char*zSql, /* SQL statement, UTF-8 encoded*/
int nByte, /* Maximum length of zSql in bytes.*/
sqlite3_stmt**ppStmt, /* OUT: Statement handle*/
const char**pzTail /* OUT: Pointer to unused portion of zSql*/
);
SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
SQLITE_API int sqlite3_step(sqlite3_stmt*);
SQLITE_API int sqlite3_finalize(sqlite3_stmt*pStmt);
SQLITE_API const void*sqlite3_column_blob(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
SQLITE_API const unsigned char*sqlite3_column_text(sqlite3_stmt*, int iCol);
SQLITE_API const void*sqlite3_column_text16(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
SQLITE_API sqlite3_value*sqlite3_column_value(sqlite3_stmt*, int iCol);
SQLite
安装
SQLite
官网:https://www.sqlite.org/index.htmlSQLite
官网下载地址:https://www.sqlite.org/download.html
在c++项目中使用 SQLite
操作数据库有两种方式,接下来分别讲两种方式:
源文件操作数据库
C++项目中,通过 调用 SQLite
源文件代码,实现操作 SQLite
数据库。 实现基本功能,包括创建数据库,打开连接数据库,建表,添加数据,关闭数据库的功能。
- 官网下载
sqlite
源文件,创建sqlite
的文件夹,并将解压后的文件放入sqlite
的文件夹:
- 用到以下几个文件,特别是
sqlite3.c
sqlite3.h
这两个文件。
- 用
vs2019
新建VC++
解决方案,新增项目。 - 把
sqlite
的文件夹,内含头文件和源文件放到项目里。 - 将项目设置为多字节字符集,避免字符集带来的问题。
- 多字节字符集使用
sqlite3_open
- 宽字符集使用
sqlite3_open16
- C/C++混合编程,编译报头文件错误,
sqlite3.c
设置不使用预编译头,或者编译成动态库载入
- 引用头文件,创建
sqlite3
对象,后续操作数据库都要用到 - 新建数据库, 此时可以看到项目目录下生成数据库文件。 注意:打开(连接)数据库,和新建是同一个函数,内部会判断是否存在,不存在会新建再打开,存在直接打开。
#include "sqlite3.h"
#include
int callback(void*, int argc, char* argv[], char* names[])
{
for (int i = 0; i < argc; i++)
{
std::cout << names[i] << "= " << argv[i] << std::endl;
}
return 0;
}
int main()
{
sqlite3* pdb = NULL;
char* errMsg = NULL;
int ret = sqlite3_open("edyun. db", &pdb);
if (ret){
std::cout << sqlite3_errmsg(pdb) << std::endl;
return -1;
else {
std::cout << "open edyun.db success! \r\n";
}
const char* sql =
"CREATE TABLE EDoYun(" \
"ID INT PRIMARY KEY NOT NULL,"\
"NAME TEXT NOT NULL);";
do {
ret = sqlite3_exec(pdb, sql, NULL, NULL, &errMsg);
if (ret != SQLITE_OK){
std::cout<< errMsg << " return " << ret << std::endl;
sqlite3_free(errMsg);
break;
} else {
std::cout<< "create table EDoYun success!" << std::endl;
break;
}
sql = "INSERT INTO EDoYun (ID,NAME)VALUES(1,\"jueding\");";
ret = sqlite3_exec(pdb, sq, NULL, NULL, &errMsg);
if (ret != SQLITE_OK){
std::cout<< errMsg << " return " << ret << std::endl;
sqlite3_free(errMsg);
break;
} else {
std::cout << "insert table EDoYun success!" << std::endl;
}
sql = "SELECT * FROM EDoYun;";
ret = sqlite3_exec(pdb, sql, callback, NULL, &errMsg);
if (ret != SQLITE_OK){
std::cout<< errMsg << " return " << ret << std::endl;
sqlite3_free(errMsg);
break;
} else {
std:: cout<< "insert table EDoYun success!" << std::endl;
}
sql = "DROP TABLE EDoYun;" ;
ret = sqlite3_exec(pdb, sql, NULL, NULL, &errMsg);
if (ret != SQLITE_OK){
std::cout << errMsg << " return " << ret << std::endl;
sqlite3_free(errMsg);
break;
} else {
std::cout << " drop table EDoYun success!" << std::endl;
}
} while (false);
sqlite3_close(pdb);
}
动态库操作数据库
通过 调用 sqlite
动态库,实现操作 sqlite
数据库,实现 基本功能,包括创建数据库,打开连接数据库,建表,添加数据,关闭数据库的功能。由 vs2019
创建的演示工程,在release文件夹下可以执行 .exe
文件演示
Original: https://blog.csdn.net/weixin_45004203/article/details/122893252
Author: 源世界yu
Title: SQLite安装与使用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/816170/
转载文章受原作者版权保护。转载请注明原作者出处!