Using SQLite in Cocos2d-x

Source: Internet
Author: User
Tags sqlite database






In a single game there are dozens of scene props, each has its own state (get, not get, get the number) and so on, if in the game is trivial involved in the real-time storage of these props, then the use of text will be slightly slower, you can use the SQLite database to complete.



: http://www.sqlite.org/download.html



After downloading, you can import sqlite3.c and sqlite3.h two files in your project.






Attach the Use code: (If the table does not exist, create two tables, store two props, and then get the data from the two tables)


#include "sqlite / sqlite3.h"
M_pDb = NULL; // sqlite3 * m_pDb;

string path = "infor \\ data.db"; // The txt text created, change the suffix name
#if CC_TARGET_PLATFORM! = CC_PLATFORM_WIN32
path = CCFileUtils :: sharedFileUtils ()-> getWritablePath () + "infor \\ data.db";
#endif

CCString sql;
int result;
result = sqlite3_open (path.c_str (), & m_pDb);
if (result! = SQLITE_OK)
{
CCLog ("open database failed, number% d", result);
m_pDb = NULL;
return;
}

// help map
sql = "CREATE TABLE help_map (ID INTEGER PRIMARY KEY, count INTEGER DEFAULT 0)";
result = sqlite3_exec (m_pDb, sql.getCString (), NULL, NULL, NULL);
if (result! = SQLITE_OK)
CCLog ("create table failed");
for (int i = 0; i <100; ++ i)
{
sql.initWithFormat ("insert into help_map values (% d, 0)", i + 1);
result = sqlite3_exec (m_pDb, sql.getCString (), NULL, NULL, NULL);
if (result! = SQLITE_OK)
CCLog ("insert data failed!");
}

// special tool
sql = "CREATE TABLE special_tool (ID INTEGER PRIMARY KEY, state INTEGER DEFAULT 0, count INTEGER DEFAULT 0)";
result = sqlite3_exec (m_pDb, sql.getCString (), NULL, NULL, NULL);
if (result! = SQLITE_OK)
CCLog ("create table failed");
for (int i = 0; i <20; ++ i)
{
sql.initWithFormat ("insert into special_tool values (% d, 0, 0)", i + 1);
result = sqlite3_exec (m_pDb, sql.getCString (), NULL, NULL, NULL);
if (result! = SQLITE_OK)
CCLog ("insert data failed!");
}

// select
char ** re;
int r, c;
{
// The third parameter is the query result, which is still a one-dimensional array (don't think it is a two-dimensional array, let alone think it is a three-dimensional array).
// Its memory layout is: the first line is the field name, followed by the value of each field
// col data is the table data, before the column name
result = sqlite3_get_table (m_pDb, "select * from help_map", & re, & r, & c, NULL);
CCLog ("help_map: row is% d, column is% d", r, c);

m_helpMap.clear ();
for (int i = 0; i <100; ++ i)
{
CCLog ("row% d ---> id =% s, count =% s", i + 1, re [c + i * c], re [c + i * c +1]);
int val = CCString (re [c + i * c +1]). intValue ();
m_helpMap.push_back (val);
}
sqlite3_free_table (re);
}


{
result = sqlite3_get_table (m_pDb, "select * from special_tool", & re, & r, & c, NULL);
CCLog ("special_tool row is% d, column is% d", r, c);

m_specialItemState.clear ();
m_specialItem.clear ();
for (int i = 0; i <20; ++ i)
{
CCLog ("row% d ---> id =% s, state =% s, count =% s", i + 1, re [c + i * c], re [c + i * c +1], re [c + i * c + 2]);
Ranch
int val = CCString (re [c + i * c +1]). intValue ();
m_specialItemState.push_back (val);
val = CCString (re [c + i * c +2]). intValue ();
m_specialItem.push_back (val);
}
sqlite3_free_table (re);
} 


In fact, in the program to store props for the vector can be replaced by a hash table, because the table elements of the primary key is linearly increased, so that the time to access each element of the complexity of only O (1).









Data.db (database file created) after inserting the data, want to see what data, or planners responsible for maintaining the data sheet inside, you can download SQLite database browser, a very good tool to browse the SQLite databases.



: http://sourceforge.net/projects/sqlitebrowser/




















Related Article

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.