(Cocos2d-x game engine development notes (20)] SQLite topics

Source: Internet
Author: User

Original article. For more information, see http://blog.csdn.net/zhy_cheng/article/details/8634967.


Hello everyone, I haven't updated my blog for a long time. I am very pleased to see your comments in my blog. This time I bring you a cocos2d-x using SQLite Database Blog.


In cocos2d-x, simple data storage, you can use ccuserdefault, you can look at my previous blog. For massive and irregular data storage, SQLite databases are used. For how to use the SQLite database, see my Blog C ++ to operate the SQLite database. In this blog, I will mainly explain how to use C ++ to operate the database. SQLite database is originally written in C language, so the cocos2d-x using SQLite is also handy.


In fact, the use of cocos2d-x operation database on windows I have been resolved, mainly in the use of Android platform.


Let's take a look at how to write the code. The code is king.


First, go to the SQLite website to download the source code of SQLite. These two files are sqlite3.h and sqlite3.c. I will download them at the end of the blog. Add these two files to the project.

sqlite3 *pdb=NULL;std::string path=CCFileUtils::sharedFileUtils()->getWriteablePath()+"save.db3";std::string sql;int result;result=sqlite3_open(path.c_str(),&pdb);if(result!=SQLITE_OK)CCLog("open database failed,  number%d",result);

The above code is to create an SQLite database file. Note: The file path must be provided.

result=sqlite3_exec(pdb,"create table student(ID integer primary key autoincrement,name text,sex text)",NULL,NULL,NULL);if(result!=SQLITE_OK)CCLog("create table failed");

This is the SQL statement used to create a table.

sql="insert into student  values(1,'zhycheng','male')";result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);if(result!=SQLITE_OK)CCLog("insert data failed!");sql="insert into student  values(2,'liuyali','female')";result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);if(result!=SQLITE_OK)CCLog("insert data failed!");sql="insert into student  values(3,'zhy_cheng','male')";result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);if(result!=SQLITE_OK)CCLog("insert data failed!");

Insert data.

sql="delete from student where ID=1";result=sqlite3_exec(pdb,sql.c_str(), ,NULL,NULL);if(result!=SQLITE_OK)CCLog("delete data failed!");

Delete data.

char **re;int r,c;sqlite3_get_table(pdb,"select * from student",&re,&r,&c,NULL);CCLog("row is %d,column is %d",r,c);CCLabelTTF *liu=CCLabelTTF::create(re[2*c+1],"Arial",24);liu->setPosition(ccp(200,200));addChild(liu,1);CCLog(re[2*c+1]);sqlite3_free_table(re);sqlite3_close(pdb);

Query data.

After the data is found, a text is displayed on the screen to see the effect on the Android phone.

Finally, you must modify the MK file when publishing it to your Android phone. The file is proj. Android/Jin/Android. mk. Add the sqlite3.c compilation and change it to the following:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := game_sharedLOCAL_MODULE_FILENAME := libgameLOCAL_SRC_FILES := hellocpp/main.cpp \                   ../../Classes/AppDelegate.cpp \                   ../../Classes/HelloWorldScene.cpp\../../Classes/sqlite3.c                   LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes                   LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static            include $(BUILD_SHARED_LIBRARY)$(call import-module,CocosDenshion/android) \$(call import-module,cocos2dx) \$(call import-module,extensions)

Well, the running effect on my mobile phone is as follows:


The database file is stored in/data/COM. zhycheng. sqlitetest/Save. db3.

Finally, download the android source code: Click to open the link.

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.