Cocos2d-x笔记(二)Sqlite数据库基本操作
来源:互联网 发布:淘宝免费赠品网 编辑:程序博客网 时间:2024/04/29 20:09
简单的数据可以使用文件方式存储,CCUserDefault、CCFileUtil等类都可以完成文件的存储。如数据量比较大、或涉及增删改比较多,就需要考虑使用数据库。以前一直做
android开发,一想到数据库肯定是Sqlite。
Sqlite数据库本身就是C语言编写的,官方也已经提供了Sqlite的API,这样工作就轻松多了。
1.先到Sqlite官网上下载源码http://www.sqlite.org/sqlite-amalgamation-3071000.zip ,主要使用的事sqlite3.h和sqlite3.c两个文件。
2.将下载的文件添加到项目中,这里有两种办法,一种是直接拷贝到Classes目录下。另一种这是通过添加附加目录(右键点项目-》属性-》配置属性-》C++-》常规-》附加包含目录)。然后右键点项目-》添加-》现有项,选择那四个源代码文件。
3.接下来就是编写代码了:
添加一条数据
sqlite3 *pDB = NULL;char *errMsg = NULL;std::string sqlStr;int result = -1;/***在windows上可以使用std::string path="test.db";数据库文件就保存resource文件夹下在android中需要使用std::string path=CCFileUtils::sharedFileUtils()->getWritablePath()+"test.db";如果不设置路径,会报unable to open the database file*/std::string path=CCFileUtils::sharedFileUtils()->getWritablePath()+"test.db"; result = sqlite3_open(path.c_str(),&pDB);if(result != SQLITE_OK)CCLog("open database failed ,error code:%d,error reason:%s\n",result,errMsg);result = sqlite3_exec(pDB,"create table TestTable(id integer primary key autoincrement,name nvchar(32))",NULL,NULL,&errMsg);if(result != SQLITE_OK){CCLog("create database table failed,error code:%d,error reason:%s",result,errMsg);}sqlStr = "insert into TestTable( name ) values('zhangsan')";result = sqlite3_exec(pDB,sqlStr.c_str(),NULL,NULL,&errMsg);if(result != SQLITE_OK){CCLog("insert into table failed,error code:%d,error reason:%s\n",result,errMsg);fflush(stdout);}
删除数据
sqlstr="delete from TestTable where ID = 2"; sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
更新数据
sqlstr="update TestTble set name='lishi' where ID = 3"; sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
判断表是否存在
sqlstr="select count(type) from sqlite_master where type='table' and name='TestTable'"; sqlite3_exec( pDB, sqlstr.c_str() , isExisted, &isExisted_, &errMsg );
其中isExited是一个回调函数
int isExited( void * para, int n_column, char ** column_value, char ** column_name ) { boolean *isExited=(boolean*)para; return 0; }
如果想在windows 查看数据库中的内容,可以使用Sqlite管理工具sqlitebrowser,下载地址:http://sourceforge.net/projects/sqlitebrowser/files/latest/download?source=files
- Cocos2d-x笔记(二)Sqlite数据库基本操作
- Cocos2d-x笔记(三)Sqlite数据库封装
- 我的Android笔记(二)—— SQLite数据库的基本操作
- SQLite数据库对数据的基本操作(二)
- cocos2d-x SQLite基本使用
- SQLite学习笔记(4)-数据库基本操作
- cocos2d-x sqlite的使用,数据库操作的封装
- cocos2d-x sqlite的使用,数据库操作的封装
- Cocos2d-x学习笔记(二)——Cocos2d的基本框架认识
- sqlite数据库基本操作
- SQLite数据库基本操作
- sqlite数据库基本操作
- SQLite数据库基本操作
- 数据库sqlite基本操作
- SQLite数据库基本操作
- sqlite 数据库基本操作
- SQLite数据库基本操作
- cocos2d-x基本类(二)
- JAVA的动态代理
- 三维计算几何模板整理
- 软件之美
- spring获取webapplicationcontext,applicationcontext几种方法详解
- The World Wide Web
- Cocos2d-x笔记(二)Sqlite数据库基本操作
- CentOS忘记密码,修改root密码
- Android文件命名规范
- ubuntu系统下关于apache2重启问题
- 使用AngularJS构建大型Web应用
- Subversion/Git/ReviewBoard工作流程
- OCP-1Z0-053-V12.02-648题
- linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl)
- android项目命名规范