测试数据说明
测试表为Student表,其基本结构和建立方法参考上一篇: SQLite学习笔记(四)– 数据表的定义、修改与删除(C++实现)
测试表的内容如下:
具体代码
#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 *db[IN] --数据库操作句柄
//返回值: bool --函数执行成功,则返回true;否则返回false
bool DeleteStudent(sqlite3 *db)
{
bool bRet = false;
int rc;
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "DELETE FROM Student WHERE Sno='%s';","1802");
rc = sqlite3_exec(db, sql, NULL, 0, &pErrMsg);
if (rc != SQLITE_OK)
{
cout << "操作发生失败,失败原因:" << pErrMsg << endl;;
sqlite3_free(pErrMsg);
bRet = false;
}
else
{
bRet = true;
}
return bRet;
}
//函数功能:向Student表中更新数据
//参数: sqlite3 *db[IN] --数据库操作句柄
//返回值: bool --函数执行成功,则返回true;否则返回false
bool UpateStudent(sqlite3 *db)
{
bool bRet = false;
int rc;
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "UPDATE Student Set Sage=%d WHERE Sno='%s';",18, "1804");
rc = sqlite3_exec(db, sql, NULL, 0, &pErrMsg);
if (rc != SQLITE_OK)
{
cout << "操作发生失败,失败原因:" << pErrMsg << endl;;
sqlite3_free(pErrMsg);
bRet = false;
}
else
{
bRet = true;
}
return bRet;
return true;
}
//函数功能:向Student表中插入数据
//参数: sqlite3 *db[IN] --数据库操作句柄
//返回值: bool --函数执行成功,则返回true;否则返回false
bool InsertStudent(sqlite3 *db)
{
bool bRet = false;
int rc;
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "INSERT INTO Student VALUES( \
'%s','%s','%s',%d);","1804",G2U("赵六"),G2U("男"),19);
rc = sqlite3_exec(db, sql, NULL, 0, &pErrMsg);
if (rc != SQLITE_OK)
{
cout << "操作发生失败,失败原因:" << pErrMsg << endl;;
sqlite3_free(pErrMsg);
bRet = false;
}
else
{
bRet = true;
}
return bRet;
}
//函数功能:使用查询表方式查询学生数据
//参数: sqlite3 *db[IN] --数据库操作句柄
//返回值: bool --函数执行成功,则返回true;否则返回false
bool SelectStudent_QueryTable(sqlite3 *db)
{
bool bRet = false;
int rc;
char sql[3000];
char *pErrMsg = 0;
sprintf_s(sql, "SELECT * FROM 'Student';");
string m_SqlCommand(sql);
char** pResult;
int nRow;
int nCol;
int nResult = sqlite3_get_table(db,
m_SqlCommand.c_str(),
&pResult, &nRow, &nCol, &pErrMsg);
if (nResult != SQLITE_OK)
{
//注意,执行失败,需要清理错误码的内存空间
sqlite3_free(pErrMsg);
return false;
}
int nIndex = nCol;
int cnt = 0;
for (int i = 0; ifor (int j = 0; jcout << U2G(pResult[j]) << ":" << U2G(pResult[nIndex]) << " ";
++nIndex;
}
cout << endl;
}
sqlite3_free_table(pResult);
return true;
}
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;
cout << "原始数据:" << endl;
//以查询表方式查询数据
SelectStudent_QueryTable(pDataBase);
//插入一条记录
InsertStudent(pDataBase);
cout << endl << "插入一条记录后数据:" << endl;
SelectStudent_QueryTable(pDataBase);
//修改年龄
UpateStudent(pDataBase);
cout << endl << "修改年龄后数据:" << endl;
SelectStudent_QueryTable(pDataBase);
//删除数据
DeleteStudent(pDataBase);
cout << endl << "删除一条记录后数据:" << endl;
SelectStudent_QueryTable(pDataBase);
//关闭数据库
iRet= sqlite3_close(pDataBase);
if (0 == iRet)
{
cout << "数据库关闭成功!" << endl;
}
}
getchar();
return 0;
}
栏目导航
上一篇:SQLite学习笔记(六)– 数据查询功能两种实现方式(下)_查询表方式(C++实现)
下一篇:SQLite学习笔记(八)– BLOB数据的插入与查询(C++实现)