Example of sqlite3 in cocos2d-x2.1.2

Source: Internet
Author: User

First, import these two files to the project and you will be given them later. Of course, you can also download them from the official website.

Import the header file in helloworldscene. h:

 
# Include "sqlite3.h"

Then declare the following functions and members:

 
// There's no 'id' in CPP, so we recommend to return the class instance pointer static cocos2d: ccscene * scene (); // save path STD :: string path; // SQL statement STD: String SQL; // database object sqlite3 * PDB; // record whether the returned result is successful int result; // CREATE TABLE void createtable (); // Insert the data void insertdata (); // view the data void lookupdata (); // Delete the data void deletedata ();

Insert the following in helloworldscene. cpp:Code:

// On "init" you need to initialize your instancebool helloworld: Init () {// 1. super init first if (! Cclayer: Init () {return false;} PDB = NULL; cclabelttf * createlabel = cclabelttf: Create ("create table", "Arial", 24 ); ccmenuitemlabel * createitem = ccmenuitemlabel: Create (createlabel, this, menu_selector (helloworld: createtable); ccmenu * createmenu = ccmenu: Create (createitem, null ); createmenu-> setposition (CCP (100,300); this-> addchild (createmenu); cclabelttf * insertlabel = cclabelttf: Create ("insert data ", "Arial", 24); Parameters * insertitem = parameters: Create (insertlabel, this, menu_selector (helloworld: insertdata); ccmenu * insertmenu = ccmenu: Create (insertitem, null); insertmenu-> setposition (CCP (100,200); this-> addchild (insertmenu); cclabelttf * lookuplabel = cclabelttf: Create ("View data", "Arial ", 24); ccmenuitemlabel * lookupitem = ccmenuitemlabel: Create (lookuplabel, this, menu_select Or (helloworld: lookupdata); ccmenu * lookupmenu = ccmenu: Create (lookupitem, null); lookupmenu-> setposition (CCP (100,100 )); this-> addchild (lookupmenu); maid: Create ("delete data", "Arial", 24); ccmenuitemlabel * deleteitem = ccmenuitemlabel: Create (deletelabel, Abel, this, menu_selector (helloworld: deletedata); ccmenu * deletemenu = ccmenu: Create (deleteitem, null); deletemenu-> set Position (CCP (200,100); this-> addchild (deletemenu); Return true;} // create a table void helloworld: createtable () {// obtain the Save path + save file name Path = ccfileutils: sharedfileutils ()-> getwritablepath () + "Save. DB "; STD: cout <" path "<path <STD: Endl; // STD: String SQL; Result = sqlite3_open (path. c_str (), & PDB); If (result! = Sqlite_ OK) cclog ("Open Database failed, Number % d", result); // table creation method result = sqlite3_exec (PDB, "create table student (ID integer primary key autoincrement, Name text, sex text)", null); If (result! = Sqlite_ OK) cclog ("CREATE TABLE failed1"); sqlite3_close (PDB);} // insert data void helloworld: insertdata () {result = sqlite3_open (path. c_str (), & PDB); If (result! = Sqlite_ OK) cclog ("Open Database failed, Number % d", result); SQL = "insert into student values (1, 'changmen', 'male ')"; result = sqlite3_exec (PDB, SQL. c_str (), null); If (result! = Sqlite_ OK) cclog ("insert data failed! "); SQL =" insert into student values (2, 'xiaonanc', 'female ') "; Result = sqlite3_exec (PDB, SQL. c_str (), null); If (result! = Sqlite_ OK) cclog ("insert data failed! "); SQL =" insert into student values (3, 'peien', 'male') "; Result = sqlite3_exec (PDB, SQL. c_str (), null); If (result! = Sqlite_ OK) cclog ("insert data failed! ");} // Delete the data void helloworld: deletedata () {sqlite3_open (path. c_str (), & PDB); SQL = "delete from student where id = 1"; Result = sqlite3_exec (PDB, SQL. c_str (), null); If (result! = Sqlite_ OK) cclog ("delete data failed! ");} // View the data void helloworld: lookupdata () {char ** re; int r = 0, c = 0; sqlite3_get_table (PDB, "select * from student", & re, 0, 0, null); cclog ("row is % d, column is % d", R, c); cclabelttf * Liu = cclabelttf:: Create (Re [7], "Arial", 24); Liu-> setposition (CCP (200,200); addchild (Liu, 1 ); cclog (Re [2 * C + 1]); sqlite3_free_table (re); sqlite3_close (PDB );}

The running result is as follows:

Download code example:
Http://vdisk.weibo.com/s/BDn59yfnBU_2k

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.