SQLite学习笔记(九)-- 视图的定义与删除(C++实现)

xiaoxiao2021-02-28  50

1.视图基本介绍

什么是视图? 视图是从数据库基本表中选取的数据组成的逻辑窗口,是有基本表中的部分行和列构成的。 视图是一个虚拟表,是查询语句产生的结果。数据库中只存放视图的定义,并不存放视图中包含的数据。视图的作用 简化数据查询语句; 使用户可以从多角度看待同一数据; 提高数据的安全性; 提供了一定程度的逻辑独立性。

2.代码实例

代码说明 本例主要说明如何对数据表进行插入、更新和删除操作。测试平台 1.开发语言:C++ 2.开发工具:VS2015 3.操作系统:Win7 X64

测试数据说明 测试表为Student表,其基本结构和建立方法参考上一篇: SQLite学习笔记(四)– 数据表的定义、修改与删除(C++实现) 测试表的内容如下:

具体代码

#include <iostream> #include <Windows.h> 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; } //函数功能:删除视图 //参数: sqlite3 *db[IN] --数据库操作句柄 //返回值: bool --函数执行成功,则返回true;否则返回false bool DropView(sqlite3 *db) { bool bRet = false; int rc; char sql[3000]; char *pErrMsg = 0; sprintf_s(sql, "DROP VIEW VIEW_BOY;"); 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 CreateView(sqlite3 *db) { bool bRet = false; int rc; char sql[3000]; char *pErrMsg = 0; sprintf_s(sql, "CREATE VIEW VIEW_BOY AS SELECT * FROM Student WHERE Ssex='%s';", G2U("男")); 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_QueryView(sqlite3 *db) { bool bRet = false; int rc; char sql[3000]; char *pErrMsg = 0; sprintf_s(sql, "SELECT * FROM 'VIEW_BOY';"); 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; i<nRow; i++) { for (int j = 0; j<nCol; j++) { cout << 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; //建立视图,存放男生信息 CreateView(pDataBase); cout << endl << "视图数据列表:" << endl; SelectStudent_QueryView(pDataBase); //删除视图 DropView(pDataBase); //关闭数据库 iRet= sqlite3_close(pDataBase); if (0 == iRet) { cout << "数据库关闭成功!" << endl; } } getchar(); return 0; } 输出结果

栏目导航 上一篇:SQLite学习笔记(八)– BLOB数据的插入与查询(C++实现) 下一篇:SQLite学习笔记(十)– 事务基本概念和代码实现(C++实现)

转载请注明原文地址: https://www.6miu.com/read-2620151.html

最新回复(0)