1.1. SQL语句执行函数
int sqlite3_exec(
sqlite3*,
const char *sql,
int (*callback)(void*,int,char**,char**),
void *,
char **errmsg
);
1.2. 释放错误信息所占的内存空间
void sqlite3_free(void*);
#include
#include
using namespace std;
//sqlite3头文件
#include "sqlite3.h"
//sqlite3库文件
#pragma comment(lib,"sqlite3.lib")
//函数功能:将utf8字符转gb2312字符
//参数: const char* utf8[IN] -- UTF8字符
//返回值: char* -- gb2312字符
char* U2G(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
if (wstr) delete[] wstr;
return str;
}
//unicode字符转utf8
//函数功能:将gb2312字符转换为utf8字符
//参数: const char* gb2312[IN] -- gb2312字符
//返回值: char* -- UTF8字符
char* G2U(const char* gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr) delete[] wstr;
return str;
}
//函数功能:新建数据表Student
//参数: sqlite3 *pDataBase[IN] -- 数据库指针
//返回值:无
void CreateStudent(sqlite3 *pDataBase)
{
if (NULL == pDataBase)
{
return;
}
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "CREATE TABLE 'Student'( \
Sno char(7) PRIMARY KEY,\
Sname char(10) NOT NULL,\
Ssex CHAR(2));");
int iRet = sqlite3_exec(pDataBase, sql, NULL, 0, &pErrMsg);
if (iRet != SQLITE_OK)
{
cout << "新建表Student操作执行失败,错误原因:" << pErrMsg << endl;
//注意,执行失败,需要清理错误码的内存空间
sqlite3_free(pErrMsg);
}
else
{
cout << "数据表Student新建成功" << endl;
}
}
//函数功能:新建数据表Student1
//参数: sqlite3 *pDataBase[IN] -- 数据库指针
//返回值:无
void CreateStudent1(sqlite3 *pDataBase)
{
if (NULL == pDataBase)
{
return;
}
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "CREATE TABLE 'Student1'( \
Sno char(7) PRIMARY KEY,\
Sname char(10) NOT NULL,\
Ssex CHAR(2));");
int iRet = sqlite3_exec(pDataBase, sql, NULL, 0, &pErrMsg);
if (iRet != SQLITE_OK)
{
cout << "新建表Student1操作执行失败,错误原因:" << pErrMsg << endl;
//注意,执行失败,需要清理错误码的内存空间
sqlite3_free(pErrMsg);
}
else
{
cout << "数据表Student1新建成功" << endl;
}
}
//函数功能:修改表Student,新增年龄字段
//参数: sqlite3 *pDataBase[IN] -- 数据库指针
//返回值:无
void AlterStudent(sqlite3 *pDataBase)
{
if (NULL == pDataBase)
{
return;
}
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "ALTER TABLE 'Student' ADD Sage INT NULL;");
int iRet = sqlite3_exec(pDataBase, sql, NULL, 0, &pErrMsg);
if (iRet != SQLITE_OK)
{
cout << "修改Student1操作执行失败,错误原因:" << pErrMsg << endl;
//注意,执行失败,需要清理错误码的内存空间
sqlite3_free(pErrMsg);
}
else
{
cout << "修改表Student成功" << endl;
}
}
//函数功能:删除表Student1
//参数: sqlite3 *pDataBase[IN] -- 数据库指针
//返回值: 无
void DropStudent(sqlite3 *pDataBase)
{
if (NULL == pDataBase)
{
return;
}
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "DROP TABLE 'Student1';");
int iRet = sqlite3_exec(pDataBase, sql, NULL, 0, &pErrMsg);
if (iRet != SQLITE_OK)
{
cout << "删除Student1操作执行失败,错误原因:" << pErrMsg << endl;
//注意,执行失败,需要清理错误码的内存空间
sqlite3_free(pErrMsg);
}
else
{
cout << "表Student1成功删除" << endl;
}
}
int main()
{
sqlite3 *pDataBase = NULL;
//打开数据库
//如果路径不含中文,可以不用转码。不过保险起见,建议全部转码。
int iRet = sqlite3_open(G2U("E:\\sqlite数据库\\testSQLite.db"), &pDataBase);
if (iRet)
{
cout << "数据库打开失败,失败原因:" << sqlite3_errmsg(pDataBase) << endl;
}
else
{
cout << "数据库打开成功!" << endl;
//新建Student表
CreateStudent(pDataBase);
//新建表Student1,留演示删除用
CreateStudent1(pDataBase);
//修改Student表,新增年龄字段
AlterStudent(pDataBase);
//删除数据表Student1
DropStudent(pDataBase);
//关闭数据库
iRet= sqlite3_close(pDataBase);
if (0 == iRet)
{
cout << "数据库关闭成功!" << endl;
}
}
getchar();
return 0;
}
栏目导航
上一篇:SQLite学习笔记(三)– 打开和关闭数据库(C++实现)
下一篇:SQLite学习笔记(五)– 数据查询功能两种实现方式(上)_回调方式(C++实现)