体积小
支持window和Linux(包括Android),跨平台性很好
采用的一种库函数的形式来提供数据库的服务
文件性的数据库
缺点,不能使用网络,单纯就是本地数据库
加密性能不是很好,安全性不足
插入
INSERT INTO 表名 (列名1,…)
VALUES (列1值,…);
UPDATE 表名
SET 列名1= 列1值, …
WHERE [条件表达式];
DELETE FROM 表名
WHERE [条件表达式];
//如果表名有空格或者特殊符号 使用 ’ " `包起来
SELECT 列名1, … FROM 表名;
WHERE [条件表达式];
` “ ‘
CREATE TABLE 库名.表名(
列1名类型 特征(主键、唯一、非空、自增、注释),…);
删除表 清楚所有数据
DROP TABLE 表名;
接口函数
如果文件存在,则尝试打开
如果文件不存在,则创建
这个文件就是一个库
不支持多线程访问,不支持多实例访问
Open 记得Close 是加载到内存 然后从内存到磁盘
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*/
);
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);
#include
#include "sqlite3.h"
using namespace std;
int callback(void*, int argc, char* argv[], char* name[])
{
for (int i = 0; i < argc; i++)
{
cout << name[i] << "==" << argv[i] << endl;
}
return 0;
}
int main()
{
sqlite3* pdb = NULL;
char* errMsg=NULL;
int ret = sqlite3_open("tianchen.db", &pdb);
if (ret)
{
cout << sqlite3_errmsg(pdb) << endl;
return -1;
}
else
{
cout << "open tianchen.db success\r\n" << endl;
}
//ID int 类型 主键约束 非空约束
//NAME 文本类型 非空约束
const char*sql="CREATE TABLE TianChen(" \
"ID INT PRIMARY KEY NOT NULL," \
"NAME TEXT NOT NULL );";
do
{
ret = sqlite3_exec(pdb, sql, NULL, NULL, &errMsg);
if (ret != SQLITE_OK)
{
cout << errMsg<<"return" <<ret<< endl;
sqlite3_free(errMsg);
break;
}
else
{
cout << "Create Tianchen Table" << endl;
}
sql ="INSERT INTO TianChen (ID,NAME)VALUES(1,\"tianchen\")";
ret = sqlite3_exec(pdb, sql, NULL, NULL, &errMsg);
//错误处理
sql = "SELECT * FROM TianChen;";
ret = sqlite3_exec(pdb, sql, callback, NULL, &errMsg);
//错误处理
sql = "DROP TABLE TianChen;";
ret = sqlite3_exec(pdb, sql, callback, NULL, &errMsg);//并不会直接清空 会有保留
} while (0);
sqlite3_close(pdb);
}