Using SQLlite databases in Cocos2d-x
SQLite is a lightweight database and ACID-compliant relational database management system. It is designed to be embedded and has been used in many embedded products, it occupies very low resources. In embedded devices, it may only need several hundred KB of memory. It supports mainstream operating systems such as Windows, Linux, and Unix, and can be combined with many programming languages, such as Tcl, C #, PHP, Java, and ODBC interfaces'
Using SQLlite databases in Cocos2d-X
First download SQLlite database, SQLlite Database: http://download.csdn.net/detail/u010105970/8168569
After the download is complete, a compressed file is obtained.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + veLRubrzo6zOxLz + vNDW0NPQM7j2zsS8/queues + cjxwpjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20141118/2014111808212058.png" alt = "\">
Create a Cocos2d-X project and place these three files in the Classes file under the project directory
Execute the following code in the program to create the SQLlite Database
// Create the sqlite database sqlite3 * pSqlite; // open the sqlite database int ret = sqlite3_open ("mydb", & pSqlite); // if (ret! = SQLITE_ OK) {// obtain the sqltite database opening error message const char * errmsg = sqlite3_errmsg (pSqlite); // print the database opening Failure Information CCLog ("sqlite open error: % s ", errmsg); return false ;}
After the execution is successful, you will see a mydb file in the Resource folder under the project directory, and the SQLlite database information is saved in the mydb file.
Run the following code to create a table:
// Create a table // The first parameter: Open Database // The second parameter: SQL statement ret = sqlite3_exec (pSqlite, "create table TUSER (id integer, name text, password text) ", NULL); // if table creation fails if (ret! = SQLITE_ OK) {// get the information about failed table creation. const char * errmsg = sqlite3_errmsg (pSqlite); // print the information about failed table creation. CCLog ("sqlite exec error: % s ", errmsg); return false ;}
Execute the following code to insert data to the table.
// Insert data to the table: ret = sqlite3_exec (pSqlite, "insert into TUSER values (1, 'xueguoliang ', 'Password')", NULL ); // if (ret! = SQLITE_ OK) {// get the information about failed data insertion into the table. const char * errmsg = sqlite3_errmsg (pSqlite); // print the information about failed data insertion. CCLog ("sqlite insert error: % s ", errmsg); return false ;}
Query table data
// Query data // query user information with the username xueguoliang ret = sqlite3_exec (pSqlite, "select * from TUSER", selectCallback, "xueguoliang", NULL );
Data Query callback function
// Callback function int selectCallback (void * key, int c, char ** value, char ** cols) {for (int I = 0; I <c; I ++) {CCLog ("% s = % s", cols [I], value [I]);} CCLog (""); if (strcmp (value [1], (char *) key) = 0) {return-1;} return 0 ;}
Queried data
Disable SQLlite Database
// Close the database sqlite3_close (pSqlite );
Zookeeper