Cocos2d-x the Reference Sqlite3 library problem when porting Android

Source: Internet
Author: User

1. First talk about Cocos2d-x 3.x simple steps to transplant Android

(1) Enter your project's directory and open the ANDROID.MK in the JNI directory (don't forget to back up one first)

If you have less engineering documents, you can honestly include all CPP files in, such as:

Local_src_files: = hellocpp/main.cpp \
.. /.. /classes/appdelegate.cpp \
.. /.. /classes/audio.cpp \
.. /.. /classes/chinese.cpp \
.. /.. /classes/comboeffect.cpp \

This is the safest, but the file is more hundred, you can try the following methods:

The original
Local_src_files: = AppDelegate.cpp \

My_cpp_list: = $ (wildcard $ (local_path)/*.cpp)
My_cpp_list + = $ (wildcard $ (local_path)/hellocpp/*.cpp)
My_cpp_list + = $ (wildcard $ (local_path)/.. /.. /classes/*.cpp)

Local_src_files: = $ (my_cpp_list:$ (Local_path)/%=%)

(2) Do not need to execute command, white waste time. We used eclipse to import the project directly into Eclipse, by using file, new, and other ...->android project from Existing Code, Libco Cos Library with that E:\cocos2d-x-3.3\cocos\platform\android\java (in advance to build a project to import) do not copy what Java Ah, Ogg Ah, not copy is OK, a copy of the wonderful flower mistake.

Plug in the phone after import, the phone does not let it into hibernation, the project right-click Debug as-Andorod project will be directly executed
Compile-time almost 100% error, because Eclipse inspection error is more stringent than VS, such as non-ANSI standard ITOA function is not allowed, some function branches no return value is not warning and error, etc., please be patient and correct

And the killer is running the phone when the flash back, reported what is wrong I forgot, return is fatal not prompt cause of the error, but the program can run a little, this time you only from the program entrance a little bit log ("xxx") function, see Logcat where error.

2.sqlite3 Library Cocos2d-x 3.x transplant Android problem

(1) Error not found in SQLite all library functions

There is no compile in that contains. C, we will sqlite3.h, sqlite3.c into the classes directory, and then write in

My_cpp_list: = $ (wildcard $ (local_path)/*.cpp)
My_cpp_list + = $ (wildcard $ (local_path)/hellocpp/*.cpp)
My_cpp_list + = $ (wildcard $ (local_path)/.. /.. /classes/*.cpp)
My_cpp_list + = $ (wildcard $ (local_path)/.. /.. /CLASSES/*.C)//This sentence is new and compiles all the. c Files

This does not error in the program, but still run error, as long as the use of the database is not valid, because access to your xxx.db file error.

The reason is that Android has a file permission check, not a database on a read-write path. db file cannot be accessed. If you put it under your resource, you have to cuff it to a read-write path,

Let's write a function:

We are in a tool class, specifically a class of tools functions, so define

SQLite files are placed directly under the resources in the Android environment is not readable, to be copied to read and write path can read and write, this function will copy the. db file to Writablepath, and then return its path
Static std::string Getdatabasefilepath (std::string filePath);

Then the implementation file writes like this:

String Tools::getdatabasefilepath (std::string filePath) {sqlite3* DBFile = nullptr;string Path = "";p ath = FileUtils:: GetInstance ()->fullpathforfilename (FilePath); #if Cc_target_platform = = Cc_platform_androidpath = FileUtils:: GetInstance ()->getwritablepath (); Access to the Android environment can read and write path, is generally the SD card path + = FilePath; file* file = fopen (Path.c_str (), "R"); Attempt to open. db on a read-write path, but there should be no such file at this time, so file returns an empty if (file = = nullptr) {ssize_t size;const char* data = (char*) (FileUtils:: GetInstance ()->getfiledata (FilePath, "RB", &size)); Obtain the contents of the database file under Resource files = fopen (Path.c_str (), "WB");  Open the Android read-write path under the file, here is a new file, using the Write method fwrite (data, size, 1, file);  Copy the database file under resource into the Android read-write path Cc_safe_delete_array (data);} fclose (file); #endiflog ("Database file:%s", Path.c_str ()); return path;
The code is to copy the. db file under the resource (which must be directly in the resource root directory) to the read-write path, and then we can use it.

In the place where you want to use the database file, write this:

Open a database, and if the database does not exist, create a database file string path = Tools::getdatabasefilepath ("game.db"); result = Sqlite3_open (Path.c_str ( ), &pdb);
That path is a valid database file path, and we're done.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos2d-x the Reference Sqlite3 library problem when porting Android

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.