手机版
你好,游客 登录 注册
背景:
阅读新闻

C++操作SQLite数据库

[日期:2012-06-16] 来源:Linux社区  作者:zhy_cheng [字体:大 中 小]

我做的一个实战工程

在我的工程中,我将API封装了一下,便于操作。
我新建了一个叫做SQLiteHelper类  头文件如下
  1. #if !defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)  
  2. #define AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_   
  3.   
  4. #if _MSC_VER > 1000   
  5. #pragma once   
  6. #endif // _MSC_VER > 1000   
  7. #include "sqlite3.h"   
  8. #include <windows.h>   
  9. class SQLiteHelper    
  10. {  
  11. public:  
  12.     SQLiteHelper();  
  13.     virtual ~SQLiteHelper();  
  14.     sqlite3 *db;  
  15.     void execSQL(char *sql);  
  16.     char**rawQuery(char *sql,int *row,int *column,char **result);  
  17.     void openDB(char *path);  
  18.     void closeDB();  
  19.   
  20.       
  21.       
  22.   
  23.   
  24. };  
  25.   
  26. #endif // !defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)   
  27. </span>  
源文件如下
  1. #include "SQLiteHelper.h"  
  2. #include <iostream.h>   
  3.   
  4. //////////////////////////////////////////////////////////////////////   
  5. // Construction/Destruction   
  6. //////////////////////////////////////////////////////////////////////   
  7.   
  8. SQLiteHelper::SQLiteHelper()  
  9. {  
  10.       
  11. }  
  12.   
  13. SQLiteHelper::~SQLiteHelper()  
  14. {  
  15.   
  16. }  
  17. void SQLiteHelper::execSQL(char *sql)  
  18. {  
  19.     sqlite3_exec(db,sql,0,0,0);  
  20. }  
  21. char **SQLiteHelper::rawQuery(char *sql,int *row,int *column,char **result)  
  22. {  
  23.     sqlite3_get_table(db,sql,&result,row,column,0);  
  24.     return result;  
  25. }  
  26. void SQLiteHelper::openDB(char *path)  
  27. {  
  28.     int last=sqlite3_open(path,&db);  
  29.     if(SQLITE_OK!=last)  
  30.     {  
  31.         cout<<"打开数据库出错"<<endl;  
  32.         return;  
  33.         PostQuitMessage(0);  
  34.     }  
  35. }  
  36. void SQLiteHelper::closeDB()  
  37. {  
  38.     sqlite3_close(db);  
  39. }   
我的主函数类如下
  1. include <iostream.h>  
  2. #include <windows.h>   
  3. #include "sqlite3.h"   
  4. #include "SQLiteHelper.h"   
  5. #pragma comment(lib,"sqlite3.lib")   
  6. //utf-8转换到GB3212   
  7. char* U2G(const char* utf8)  
  8. {  
  9.  int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);  
  10.  wchar_t* wstr = new wchar_t[len+1];  
  11.  memset(wstr, 0, len+1);  
  12.  MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);  
  13.  len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);  
  14.  char* str = new char[len+1];  
  15.  memset(str, 0, len+1);  
  16.  WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);  
  17.  if(wstr) delete[] wstr;  
  18.  return str;  
  19. }  
  20. //GB2312到UTF-8的转换   
  21. char* G2U(const char* gb2312)  
  22. {  
  23.  int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);  
  24.  wchar_t* wstr = new wchar_t[len+1];  
  25.  memset(wstr, 0, len+1);  
  26.  MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);  
  27.  len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);  
  28.  char* str = new char[len+1];  
  29.  memset(str, 0, len+1);  
  30.  WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);  
  31.  if(wstr) delete[] wstr;  
  32.  return str;  
  33. }  
  34.   
  35.   
  36.   
  37. void main()  
  38. {  
  39.   
  40.     SQLiteHelper *help=new SQLiteHelper();  
  41.     help->openDB("d:\\zhycheng.db3");  
  42.     char *sql="insert into dota values(6,'zhycheng')";  
  43.     help->execSQL(sql);  
  44.     char *sql2="select * from dota";  
  45.     int row,col;  
  46.     char *eee="i";  
  47.     char **result=&eee;  
  48.     char **re=help->rawQuery(sql2,&row,&col,result);  
  49.     char *ll=U2G(re[(2+1)*col+1]);  
  50.     cout<<ll<<endl;  
  51.     help->closeDB();  
  52.   
  53. }

这里我讲解一下re[(2+1)*col+1]

re是指向数组的指针。(2+1)为第3行,1表示第2列。

从中可以看出,我将“张译成”这个字符串读出了。大家注意,在写入的时候,如果要写入中文的话,就要将中文从GB2312转换到utf-8再写入,大家根据自己项目的需要,函数我已经给出了。
linux
相关资讯       C++  SQLite教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款