Cocos2dx - SQLITE erro database is locked

  • igorppbr
    Likes 0

    Problem Description

    Hello,

    I have a code where I get a value from my sqlite database and after I have to update this same field in the database, but when I try to do this I get the error “database is locked”.

    I dont know why this error is ocurring.

    First I have a method that execute this code:

    std::string path=CCFileUtils::sharedFileUtils()->getWritablePath()+"save.db3";
         sqlite3_stmt *stmt;
         char *errmsg=NULL;
         std::string sql;
         int ref = 0;
         sqlite3_open(path.c_str(),&pdb);
         sql = "SELECT * from mapas WHERE codigo_mapa = "+converIntToString(_codigoMapa)+" and codigo_mundo = "+converIntToString(MUNDO_ATUAL);
         CCLOG("BANCO DE DADOS RECORDE SQL AO BUSCAR %s",sql.c_str());
         sqlite3_prepare_v2(pdb,sql.c_str(),strlen(sql.c_str()),&stmt,NULL);
         while(sqlite3_step(stmt)==SQLITE_ROW) {
              char sqlbuf[4096];

              ref = sqlite3_column_int(stmt,2);
              CCLOG("BANCO DE DADOS RECORDE TESTE: %d",ref);
              puts(sqlbuf);
              sqlite3_exec(pdb,sqlbuf,NULL,NULL,&errmsg);
         }
         sqlite3_finalize(stmt);
         sqlite3_close(pdb);
         return ref;

     

    And after I try to update using this method:

    sqlite3_close(pdb);
        int result;
        std::string sql;
        std::string path=CCFileUtils::sharedFileUtils()->getWritablePath()+"save.db3";
        sql="update mapas set record = "+converIntToString(_recorde)+" where codigo_mundo = "+converIntToString(MUNDO_ATUAL)+" and codigo_mapa = "+converIntToString(_codigoFase);
        sql = sql + ";insert into mapas values("+converIntToString(MUNDO_ATUAL)+","+converIntToString(_codigoFase+1)+",0,1);";
        CCLOG("BANCO DE DADOS RECORDE SQL AO atualizar %s",sql.c_str());
        sqlite3_stmt *stmt;
        if (sqlite3_open(path.c_str(), &pdb) == SQLITE_OK)
        {
            sqlite3_busy_timeout(pdb, 0);
            sqlite3_prepare_v2(pdb, sql.c_str(), strlen(sql.c_str()), &stmt, NULL );//preparing the statement
            sqlite3_step(stmt);//executing the statement
            if(sqlite3_step(stmt)!=SQLITE_DONE)
            {
              CCLOG("BANCO DE DADOS. ERRO AO ATUALIZAR MAPA. --> %s",sqlite3_errmsg(pdb));
            }
            else{
                CCLOG("BANCO DE DADOS. ATUALIZOU COM SUCESSO O RECORDE DO MAPA");
                return 1;
            }
        }
        else
        {
            CCLOG("BANCO DE DADOS: ERRO AO ABRIR BANCO");
        }
        sqlite3_finalize(stmt);
        return 0;

     

    Anyone can help me?

     

    Thanks!

  • Sonar Systems admin
    Likes 0

    Where did you get this code from?

  • igorppbr
    Likes 0

    I found this http://discuss.cocos2d-x.org/t/a-simple-tutorial-how-to-use-sqlite-in-cocos2d-x-tutorial-by-yuye/10677

    But in the select method I don’t remeber where I got this code, I not found a tutorial about this in cocos2dx on the internet.

  • igorppbr
    Likes 0

    I found the error, before finish the method I have to finalize the sqlite3 stmt using sqlite3_finalize(stmt);

    Thanks!

  • Sonar Systems admin
    Likes 0

    Great :D

Login to reply