Creating a SQLite database in Cocos2d-x

Source: Internet
Author: User
Tags call back sqlite database

below we describe how to implement Mynotes database creation through the API provided by SQLite3. Creating a database typically takes the following three steps.
(1) Use the Sqlite3_open function to open the database.
(2) Create a database table using the Sqlite3_exec function to execute the CREATE TABLE statement.
(3) Use the Sqlite3_close function to release resources.
In this process, we used three SQLite3 API functions, all of which are pure C language functions. Calling C functions through C + + in Cocos2d-x is certainly not a problem, the Notedao::initdb () function in NoteDAO.cpp can initialize the database, and its associated code is as follows:
int Notedao::initdb () {Auto sharedfileutils = Fileutils::getinstance (); string path = Dbdirectoryfile (); ①bool isexist = False;isexist = sharedfileutils->isfileexist (path); ②if (!isexist) {log ("Noteslist.sqlite3 doesn ' t exist."); return-1;} sqlite3* db= null;if (Sqlite3_open (Path.c_str (), &db)! = SQLITE_OK) {③sqlite3_close (db); ④ccassert (False, "DB open Failure. "); ⑤} else {char *err;string createsql = "CREATE TABLE IF not EXISTS Note (cdate text PRIMARY KEY, content TEXT)"; ⑥if (SQLite 3_exec (Db,createsql.c_str (), null,null,&err)! = SQLITE_OK) {⑦sqlite3_close (db); ⑧ccassert (False, "Create table Failure. "); ⑨}sqlite3_close (db); ⑩}return 0;}


The above code INITDB () function is an initialization database function that creates objects such as tables in a database and invokes it every time a crud operation is made to the database. The process in this function is to determine whether the database file Noteslist.sqlite3 exists, if it does not exist, or if it is initialized, jump out of the function if it already exists. Where code line ① calls the Dbdirectoryfile () function, which is our own encapsulated function, which is used to obtain the full path to the database file in the writable directory noteslist.sqlite3. The ② line of code is to determine whether the database file Noteslist.sqlite3 exists in the writable directory, and if no program jumps out of the function.
The code for line ③ is the creation of a database where the first parameter of the Sqlite3_open function is the full path of the database file, it is important to note that the SQLite3 function accepts const char* type data and needs to use the string type Data c_str () The function is converted to the Const char* type, the second argument is the address of the SQLITE3 pointer variable db, and the return value is the int type. In SQLite3, we define a number of constants, and if the return value equals constant SQLITE_OK, the creation succeeds. If the database fails to open, we need to use the ④ Line code sqlite3_close (db) to close the database to release resources, similar to the ⑧ line and the ⑩ line code. The ⑤ line code uses the Ccassert assertion macro, which asserts that the macro is the first argument false, throws an exception, terminates the program, and outputs the second parameter as the log content.
The ⑥ line of code is to write a SQL statement with the following code:
CREATE TABLE IF not EXISTS Note (cdate text PRIMARY KEY, content TEXT)
The Build Table statement creates a table if not exists to determine whether the tables exist, that do not exist, and not created. If you use the Build Table statement to create TABLE, this causes the data to throw an exception if the table exists. There is a similar line of code ⑨.
The ⑦ line code is the execution of the build table statement, where the statement sqlite3_exec (DB,CREATESQL.C_STR (), NULL,NULL,&ERR) executes the SQL statement of the first ⑥ row, sqlite3_ The first parameter of the EXEC function is the address of the SQLITE3 pointer variable db, the second argument is the SQL statement to execute, the third argument is the function to be recalled, the fourth parameter is the argument to the callback function, and the fifth parameter is the error message that performs the error.
In order to be able to invoke the Initialize database function Initdb () in Notedao, we need to call in the Helloworldscene scenario. HelloWorldScene.cpp main code is as follows:
void Helloworld::onclickmenu1 (cocos2d::ref* psender) {notedao::initdb (); Notedao::create ("2008-08-16 10:01:02", "Initialize data.");


The HELLOWORLD::ONCLICKMENU1 function is a function that players call back when they click on the Init DB menu, where the initialization of the database is implemented through the NOTEDAO::INITDB () statement. The Initdb () function in Notedao is a static function that can be called directly by instantiating Notedao. The following notedao::create ("2008-08-16 10:01:02", "Initialize data.") The statement is to call the CREATE function in Notedao to insert a piece of data into the note table.


For more information, please pay attention to the first Cocos2d-x 3.2 Edition book "Cocos2d-x: C + + volume" book Exchange discussionwebsite: http://www.c ocoagame.net
For more exciting video courses, please follow Cocos class: http://v.51work6.com
Welcome to join Cocos2d-x Technical Discussion group: 257760386 Welcome to Luxgen iOS Classroom public platform

Creating a SQLite database in Cocos2d-x

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.