本文共 3398 字,大约阅读时间需要 11 分钟。
(这里讲的是如何在Cocos2d-x引擎中使用SQLite数据库)
1、去官网下载SQLite( )并安装;
2、获取SQLite头文件( );
3、新建一个Cocos2d-x工程,将SQLite头文件添加到工程中,同时在程序中包含头文件:
1 2 | #include "GameScene.h" #include "sqlite3.h" //加入头文件 |
创建SQLite数据库文件
1 2 3 4 5 | sqlite3 *pdb = NULL; //数据库对象 std::string path = FileUtils::getInstance()->getWritablePath() + "test4.db" ; //保存路径 std::string sqlstr; //SQL语句 int result; |
1 | log ( "%s" , path.c_str()); ///Users/mac/Library/Application\ Support/iPhone\ Simulator/7.1-64/Applications/7B6164DE-7E7A-4C70-980B-2B548AB3774A/Documents/ |
打开一个数据库文件,如果不存在则新建
1 2 3 4 5 | result = sqlite3_exec(pdb, "create table student1(id integer, name text, sex text)" , NULL, NULL, NULL); if (result != SQLITE_OK) log ( "create table faild" ); else log ( "create table success" ); |
插入操作
1 2 3 4 5 6 | sqlstr = "insert into student1(id, name, sex) values(1, 'jiazedong', 'male')" ; result = sqlite3_exec(pdb, sqlstr.c_str(), NULL, NULL, NULL); if (result != SQLITE_OK) log ( "insert data faild" ); else log ( "insert data success" ); |
查询操作
1 2 3 4 5 6 7 8 9 | char **re; int r, c; sqlite3_get_table(pdb, "select * from student1" , &re, &r, &c, NULL); log ( "row is %d, column is %d" , r, c); // log(re[2*c+1]); for ( int i=0; i<r; i++) log ( "%s" , re[i]); |
关闭数据库
1 | sqlite3_close(pdb); |
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | // Created by Jacedy on 14-8-11. // // #include "GameScene.h" #include "sqlite3.h" //加入头文件 USING_NS_CC; cocos2d::Scene* GameScene::createScene() { auto scene = Scene::create(); //创建一个场景 auto layer = GameScene::create(); //创建一个图层 scene->addChild(layer); return scene; } //初始化当前的图层 bool GameScene::init() { if (!Layer::init()) //初始化父类 return false ; //获取屏幕大小 size = Director::getInstance()->getVisibleSize(); //auto size = Director::getInstance()->getWinSize(); //创建SQLite数据库文件 sqlite3 *pdb = NULL; //数据库对象 std::string path = FileUtils::getInstance()->getWritablePath() + "test4.db" ; //保存路径 std::string sqlstr; //SQL语句 int result; log ( "%s" , path.c_str()); ///Users/mac/Library/Application\ Support/iPhone\ Simulator/7.1-64/Applications/7B6164DE-7E7A-4C70-980B-2B548AB3774A/Documents/ //打开一个数据库文件,如果不存在则新建 result = sqlite3_open(path.c_str(), &pdb); if (result != SQLITE_OK) log ( "open databases faild %d" , result); else log ( "open databases success %d" , result); //创建表 result = sqlite3_exec(pdb, "create table student1(id integer, name text, sex text)" , NULL, NULL, NULL); if (result != SQLITE_OK) log ( "create table faild" ); else log ( "create table success" ); //插入操作 sqlstr = "insert into student1(id, name, sex) values(1, 'jiazedong', 'male')" ; result = sqlite3_exec(pdb, sqlstr.c_str(), NULL, NULL, NULL); if (result != SQLITE_OK) log ( "insert data faild" ); else log ( "insert data success" ); sqlstr = "insert into student1(id, name, sex) values(2, 'jiazedong', 'male')" ; result = sqlite3_exec(pdb, sqlstr.c_str(), NULL, NULL, NULL); if (result != SQLITE_OK) log ( "insert data faild" ); else log ( "insert data success" ); //查询操作 char **re; int r, c; sqlite3_get_table(pdb, "select * from student1" , &re, &r, &c, NULL); log ( "row is %d, column is %d" , r, c); // log(re[2*c+1]); for ( int i=0; i<r; i++) log ( "%s" , re[i]); // sqlite3_free_table(re); //关闭数据库 sqlite3_close(pdb); return true ; } |
转载地址:http://gvtwa.baihongyu.com/