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:
[HTML]View Plaincopy
- 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 (Sqlite3_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:
[HTML]View Plaincopy
- 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 Discussion website: http://www.cocoagame.net
For more exciting video courses, please follow the Cocos class: Http://v.51work6.com Welcome to join the COCOS2D-X Technical Discussion group: 257760386 Welcome to the Luxgen iOS classroom public Platform
To create a SQLite database in Cocos2d-x