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

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

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

这里要注意的一点就是,在其静态方法loadDb()中,要使用Object-C中的NSAutoreleasePool 对象来“框住”数据库的加载逻辑代码,否则会在下面这一样产生内存泄露:

NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory/*NSCachesDirectory*/, NSUSErDomainMask, YES); 

下面我们来看如何使用它,上DEMO, 呵呵。

判断当前数据库中是否存在某个TABLE,如存在则删除,之后创建新表:

  1. if(DBHelper::tableExists("emp")){ 
  2.     DBHelper::execNoQuery("drop table emp;"); 
  3. } 
  4. DBHelper::execNoQuery("create table emp(empno int, empname char(20));");   

接着向新表中接入数据并返回插入成功的条数,如下:

  1. int nRows = DBHelper::execNoQuery("insert into emp values (7, 'David Beckham');"); 
  2. cout << nRows << " rows inserted" << endl; 

然后进行UPDATE,DELETE操作并返回操作的记录条数:

  1. nRows = DBHelper::execNoQuery("update emp set empname = 'Christiano Ronaldo' where empno = 7;"); 
  2. cout << nRows << " rows updated" << endl; 
  3.      
  4. nRows = DBHelper::execNoQuery("delete from emp where empno = 7;"); 
  5. cout << nRows << " rows deleted" << endl; 

基于事务属性进行批量操作,以提升性能。

  1. int nRowsToCreate(50000); 
  2.   cout << endl << "Transaction test, creating " << nRowsToCreate; 
  3.   cout << " rows please wait..." << endl; 
  4.   DBHelper::execNoQuery("begin transaction;"); 
  5.        
  6.   for (int i = 0; i < nRowsToCreate; i++) 
  7.   { 
  8.       char buf[128]; 
  9.       sprintf(buf, "insert into emp values (%d, 'Empname%06d');", i, i); 
  10.       DBHelper::execNoQuery(buf); 
  11.   } 
  12.   DBHelper::execNoQuery("commit transaction;"); 

进行select count操作:

  1. cout << DBHelper::execScalar("select count(*) from emp;") << " rows in emp table in "; 

使用Buffer进行SQL语句构造:

  1. Buffer bufSQL; 
  2.  bufSQL.format("insert into emp (empname) values (%Q);", "He's bad"); 
  3.  cout << (const char*)bufSQL << endl; 
  4.  DBHelper::execNoQuery(bufSQL); 
  5.  DBHelper::execNoQuery(bufSQL); 
  6.        
  7.  bufSQL.format("insert into emp (empname) values (%Q);", NULL); 
  8.  cout << (const char*)bufSQL << endl; 
  9.  DBHelper::execNoQuery(bufSQL); 

遍历数据集方式1:

  1. Query q = DBHelper::execQuery("select * from emp order by 1;"); 
  2.        
  3.  for (int fld = 0; fld < q.fieldsCount(); fld++){ 
  4.      cout << q.fieldName(fld) << "(" << q.fieldDeclType(fld) << ")|"; 
  5.  } 
  6.  cout << endl; 
  7.        
  8.  while (!q.eof()){ 
  9.      cout << q.fieldValue(0) << "|" ; 
  10.      cout << q.fieldValue(1) << "|" << endl;             
  11.      cout << q.fieldValue("empno") << "||" ; 
  12.      cout << q.fieldValue("empname") << "||" << endl; 
  13.  
  14.      //或使用[]索引,效果同q.fieldValue  
  15.      cout << q[0] << "|" ; 
  16.      cout << q[1] << "|" << endl; 
  17.      cout << q["empno"] << "||" ; 
  18.      cout << q["empname"] << "||" << endl; 
  19.      q.nextRow(); 
  20.  } 

遍历数据集方式2:

  1. Table t = DBHelper::getTable("select * from emp order by 1;"); 
  2.          
  3.    for (int fld = 0; fld < t.fieldsCount(); fld++){ 
  4.        cout << t.fieldName(fld) << "|"; 
  5.    } 
  6.    for (int row = 0; row < t.rowsCount(); row++){   
  7.        Row r= t.getRow(row); 
  8.        cout << r["empno"] << " " << r["empname"] << "|"; 
  9.        cout << endl; 
  10.    } 

预编译Statements测试(使用场景不多):

  1. cout << endl << "Transaction test, creating " << nRowsToCreate; 
  2.    cout << " rows please wait..." << endl; 
  3.    DBHelper::execNoQuery("drop table emp;"); 
  4.    DBHelper::execNoQuery("create table emp(empno int, empname char(20));"); 
  5.    DBHelper::execNoQuery("begin transaction;"); 
  6.    Statement stmt = DBHelper::compileStatement("insert into emp values (?, ?);"); 
  7.    for (int i = 0; i < nRowsToCreate; i++){ 
  8.        char buf[16]; 
  9.        sprintf(buf, "EmpName%06d", i); 
  10.        stmt.bind(1, i); 
  11.        stmt.bind(2, buf); 
  12.        stmt.execDML(); 
  13.        stmt.reset(); 
  14.    } 
  15.          
  16.    DBHelper::execNoQuery("commit transaction;"); 

最后我们只要找一个相应的.m文件改成.mm后缀,将上面测试语句执行一下,就可以了。

好了,今天的内容就先到这里了。

下面是源码下载地址:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/11月/7日/当C++遇到iOS应用开发---SQLite篇

linux
相关资讯       C++  iOS应用开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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