SQLiteの使用二

4257 ワード

(ここではどのようにCocos 2 d-xエンジンでSQLiteデータベースを使うかを言います。)
1、公式サイトにSQLiteをダウンロードする(http://www.sqlite.org/download.html )そしてインストールする;
2、SQLiteヘッダファイルを取得する(http://download.csdn.net/detail/jacedy/7922513 );
3、Cococos 2 d-xプロジェクトを新たに作成し、SQLiteヘッダファイルをプロジェクトに追加し、同時にプログラムにヘッダファイルを含める:
#include "GameScene.h"
#include "sqlite3.h"      //     
    
    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_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");
    クエリー操作
    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_close(pdb);
完全コード:
//  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;
}