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:


[Cpp]
# Include "sqlite3.h"

# Include "sqlite3.h"
Then declare the following functions and members:


[Cpp]
// 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 a table
Void createTable ();
// Insert data
Void insertData ();
// View data
Void LookUpData ();
// Delete data
Void deleteData ();

// 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 a table
Void createTable ();
// Insert data
Void insertData ();
// View data
Void LookUpData ();
// Delete data
Void deleteData ();

 

Insert the following code in HelloWorldScene. cpp:


[Cpp]
// On "init" you need to initialize your instance
Bool 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 );
CCMenuItemLabel * insertItem = CCMenuItemLabel: 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_selector (HelloWorld: LookUpData ));
CCMenu * lookUpMenu = CCMenu: create (lookUpItem, NULL );
LookUpMenu-> setPosition (ccp (100,100 ));
This-> addChild (lookUpMenu );


CCLabelTTF * deleteLabel = CCLabelTTF: create ("delete data", "Arial", 24 );
CCMenuItemLabel * deleteItem = CCMenuItemLabel: create (deleteLabel, this, menu_selector (HelloWorld: deleteData ));
CCMenu * deleteMenu = CCMenu: create (deleteItem, NULL );
DeleteMenu-> setPosition (ccp (200,100 ));
This-> addChild (deleteMenu );


Return true;
}
// Create a table
Void HelloWorld: createTable ()
{
// Get the Save path + save the 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 );

// Create a table
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 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 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 );

}

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.