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

当C++遇到iOS应用开发---SQLite篇

[日期:2012-11-07] 来源:www.cnblogs.com/daizhj  作者:daizhj, 代震军 [字体:大 中 小]

注意:关于Row的实现是老代码中所没有的,因为要考虑到尽量逼成ADO.NET的对象结构,所以这里加入了进来;

有了上面的对象支持,接下来就可以写一个封装类DBHelper.h来实现常用数据访问操作了,如下:

  1. #ifndef DBHelper_h  
  2. #define DBHelper_h  
  3.  
  4. #include "SQLiteHelper.h"  
  5.  
  6. //#include <ctime>  
  7. #include <iostream>  
  8.  
  9. using namespace std; 
  10. using namespace SQLiteWrapper; 
  11.  
  12. namespace SQLiteWrapper { 
  13. class DBHelper 
  14. { 
  15.      
  16.        
  17. private: 
  18.     DBHelper() 
  19.     {} 
  20.     virtual ~DBHelper(void) 
  21.     {} 
  22.      
  23. public: 
  24.     static DB db; 
  25.    
  26.     static DB loadDb() 
  27.     { 
  28.         DB database; 
  29.         { 
  30.             NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
  31.             NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory/*NSCachesDirectory*/, NSUSErDomainMask, YES); 
  32.             NSString *path = [arr objectAtIndex:0]; 
  33.             path = [path stringByAppendingPathComponent:@"MySqlLitePath"]; 
  34.  
  35.             // create directory for db if it not exists  
  36.             NSFileManager *fileManager = [[NSFileManager alloc] init]; 
  37.             BOOL isDirectory = NO; 
  38.             BOOL exists = [fileManager  fileExistsAtPath:path isDirectory:&isDirectory]; 
  39.             if (!exists) { 
  40.                 [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil]; 
  41.                 if (![fileManager fileExistsAtPath:path]) { 
  42.                     [NSException raise:@"FailedToCreateDirectory" format:@"Failed to create a directory for the db at '%@'",path]; 
  43.                 } 
  44.             } 
  45.             [fileManager release]; 
  46.             // create db object  
  47.             NSString *dbfilePath = [path stringByAppendingPathComponent:@"Blogs"]; 
  48.             std::string dbpathstr =[dbfilePath UTF8String]; 
  49.  
  50.          
  51.             const char *dbpath = dbpathstr.c_str();//"/Users/MySqlLitePath/Blogs";  
  52.             database.open(dbpath); 
  53.             [pool release]; 
  54.         } 
  55.         return database; 
  56.     } 
  57.      
  58.      
  59.     static bool tableExists(const char* szTable) 
  60.     { 
  61.        return db.tableExists(szTable); 
  62.     } 
  63.      
  64.        
  65.     static int execNoQuery(const char* szSQL) 
  66.     { 
  67.         return db.execDML(szSQL); 
  68.     } 
  69.      
  70.     static int execNoQuery(const NSString* szSQL) 
  71.     { 
  72.         return db.execDML([szSQL UTF8String].c_str()); 
  73.     } 
  74.      
  75.     static Query execQuery(const char* szSQL) 
  76.     { 
  77.         return db.execQuery(szSQL); 
  78.     } 
  79.      
  80.     static Query execQuery(const NSString* szSQL) 
  81.     { 
  82.         return db.execQuery([szSQL UTF8String].c_str()); 
  83.     } 
  84.      
  85.     static Query execScalar(const char* szSQL) 
  86.     { 
  87.         return db.execQuery(szSQL); 
  88.     } 
  89.      
  90.     static int execScalar(const NSString* szSQL) 
  91.     { 
  92.         return db.execScalar([szSQL UTF8String].c_str()); 
  93.     } 
  94.      
  95.     static Table getTable(const char* szSQL) 
  96.     { 
  97.         return db.getTable(szSQL); 
  98.     } 
  99.      
  100.     static Table getTable(const NSString* szSQL) 
  101.     { 
  102.         return db.getTable([szSQL UTF8String].c_str()); 
  103.     } 
  104.      
  105.     static Statement compileStatement(const char* szSQL) 
  106.     { 
  107.         return db.compileStatement(szSQL); 
  108.     } 
  109.      
  110.     static Statement compileStatement(const NSString* szSQL) 
  111.     { 
  112.         return db.compileStatement([szSQL UTF8String].c_str()); 
  113.     } 
  114.      
  115.     static sqlite_int64 lastRowId() 
  116.     { 
  117.         return db.lastRowId(); 
  118.     } 
  119.      
  120.    
  121.     static void setBusyTimeout(int nMillisecs) 
  122.     { 
  123.         db.setBusyTimeout(nMillisecs); 
  124.     } 
  125.        
  126. }; 
  127. } 
  128.  
  129. DB DBHelper::db = DBHelper::loadDb(); //在全局区进行对象初始化操作。 
linux
相关资讯       C++  iOS应用开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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