Help us understand the problem. What is going on with this article?

cocos2d-xでsqlite3を使う方法

More than 5 years have passed since last update.

初投稿です。cocos2d-xでsqlite3を使えないか調べていたら
ブログで発見したので自分で試したらうまく動いてくれたので忘却防止の為、投稿します。

準備

sqlite3.hヘッダーファイルをインクルードします。

HelloWorldScene.cpp
#include "sqlite3.h" 

テーブルの作成

HelloWorldScene.cpp
    // SQLITE3
    sqlite3 *pDB = NULL;
    char* errMsg = NULL;
    std::string sqlstr;
    int result;
    std::string dbPath = CCFileUtils::sharedFileUtils()->getWriteablePath();
    dbPath.append("Test.db");
    result = sqlite3_open(dbPath.c_str(),&pDB);
    if (result != SQLITE_OK){
        CCLOG("OPENING WRONG, %d, MSG:%s",result,errMsg);
    }else{
        CCLOG("OK! %d, %s",result,errMsg);
    }

    // 読み書きファイルパスの表示
    CCLog("%s",dbPath.c_str());

    result=sqlite3_exec( pDB, "create table MyTable_1( ID integer primary key autoincrement, name nvarchar(32) ) " , NULL, NULL, &errMsg );
    if( result != SQLITE_OK )
        CCLog( "失敗 :%d ,原因:%s\n" , result, errMsg );

    //insert
    sqlstr=" insert into MyTable_1( name ) values ( 'いちご' ) ";
    result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
    if(result != SQLITE_OK )
        CCLog( "失敗 :%d ,原因:%s\n" , result, errMsg );

    //insert
    sqlstr=" insert into MyTable_1( name ) values ( 'ぶどう' ) ";
    result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
    if(result != SQLITE_OK )
        CCLog( "失敗 :%d ,原因:%s\n" , result, errMsg );

    //insert
    sqlstr=" insert into MyTable_1( name ) values ( 'ばなな' ) ";
    result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
    if(result != SQLITE_OK )
        CCLog( "失敗 :%d ,原因:%s\n" , result, errMsg );

    sqlite3_close(pDB);
nsy999jp
たなかです。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした