Using the Sqllite database in Cocos2d-x

Source: Internet
Author: User
Tags sqlite sqlite database

SQLite, is a lightweight database, is to adhere to the Acid relational database management system, its design goals are embedded, and has been used in many embedded products, it occupies very low resources, in embedded devices, may only need hundreds of K of memory is enough. It can support Windows/linux/unix and other mainstream operating systems, and can be combined with many programming languages, such as TCL, C #, PHP, Java, and ODBC interface '


Using the Sqllite database in Cocos2d-x

First download the Sqllite database, Sqllite database: http://download.csdn.net/detail/u010105970/8168569

A compressed file is obtained when the download is complete


After decompression, there are 3 files in the folder sqlite3.c, Sqlite3.h, sqlite3ext.h

Create a cocos2d-x project to place these three files in the classes file in the project directory


Execute the following code in the program to create the Sqllite database

  Create SQLite database sqlite3* psqlite;    Open SQLite database int ret = Sqlite3_open ("MyDB", &psqlite);    When Sqllite database open fails    if (ret! = SQLITE_OK) {        //Get Sqltite Database open error Information const char* ERRMSG = sqlite3_errmsg (psqlite);        //Print database open failed information        Cclog ("SQLite Open Error:%s", errmsg);        return false;}

After successful execution, you will see a mydb file in the resource folder in the project directory, and the MyDB file holds the Sqllite database information.

Execute the following code implementation to create the table

  CREATE TABLE    //First parameter: Open database    //second parameter: SQL statement ret = sqlite3_exec (psqlite, "CREATE TABLE TUSER (ID integer, name text, Password text) ", NULL, NULL, NULL);    If you create a table failure if    (ret! = SQLITE_OK) {        //Get the information to create TABLE failed const char* errmsg = sqlite3_errmsg (psqlite);        Print CREATE TABLE failed information        cclog ("SQLite exec Error:%s", errmsg);        return false;}

Execute the following code to insert data into the table

  Inserting data into a table ret = sqlite3_exec (Psqlite, "insert into TUSER values (1, ' Xueguoliang ', ' Password ')", NULL, NULL, NULL);    When inserting data into a table fails    if (ret! = SQLITE_OK) {        //Gets information to insert data into the table fails const char* errmsg = sqlite3_errmsg (psqlite);        Print the information that failed to insert data        cclog ("SQLite Insert Error:%s", errmsg);        return false;}


Querying data in a table

Querying data    //querying user information for user name Xueguoliang ret = sqlite3_exec (Psqlite, "select * from TUSER", Selectcallback, "Xueguoliang", NULL);


callback function for querying data

callback function for querying data int selectcallback (void* key, int c, char** value, char** cols) {for (int i = 0; i < C; i++) {Cclog ("%s=%s", C Ols[i], value[i]);}    Cclog (""); if (strcmp (Value[1], (char*) key) = = 0) {return-1;} return 0;}


The data queried


Close the Sqllite database

Close Database Sqlite3_close (psqlite);




Using the Sqllite database in Cocos2d-x

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.