Unity3d using the embedded database SQLite on the Android platform to resolve problems that could not find the database file

Source: Internet
Author: User

Do a need to embedded database SQLite Unity3d project, on the PC running well, need to publish to the Android platform, so, a variety of pit dad ... A problem was encountered that could not find the database file. When using SQLite on a PC, when executing sqliteconnection dbConnection = new Sqliteconnection ("Data Source = test.db"); If there is a database file, the connection is established. If not, the file is created and the connection is established. When it comes to Android, it's time to start, and you can't do it anyway (don't ask me how I know it, it's all tears). In the Unity Official forum, foreigners also encountered this problem, but also a variety of pit fathers, but finally solved the problem, referring to a foreigner's method, combined with my own project features, on the Android platform automatically set up database files, to get the detailed process of solving the problem:

First in the Windows environment Unity3d program to publish to the Android platform, must be three files: Mono.Data.Sqlite.dll, Sqlite3.dll, System.Data.dll, three files must be placed under the Plugins folder and a folder will be created without a folder. Also need a Linux under the SQLite compiler library file: libsqlite3.so This file must be placed in the Plugins/android folder (click to download the downloaded file has many, unity platform different or different version of the required files, Choose the required files, error words deleted, this is Win7 platform), no folder on the creation of a.

public static class Config{public    static string filepath = Application.persistentdatapath + "/victory.db";  Victory.db is the database file to be created public    static string dbconnectionstring = "Uri=file:" + filepath;}

Because the project requires the database to be created the first time after the program is installed, the various data are initialized. So it is necessary to determine whether the local is already having the data file, if none is created, and if so, skip the Create step.

        if (file.exists (Config.filepath)) {            //database already exists, establish connection, start various operations            Sqliteconnection conn = new Sqliteconnection ( config.dbconnectionstring);        } else {            //If the database file is not created, create a database file            www loaddb = new www ("jar:file://" + Application.datapath + "!/assets/" + "Victory . db ");            while (!loaddb.isdone) {            }            file.writeallbytes (Config.filepath, loaddb.bytes);            Establish database connection            Sqliteconnection conn = new sqliteconnection (config.dbconnectionstring);            Next, perform the database initialization operation.        }

In short, a statement on the PC sideSqliteconnection conn = new Sqliteconnection (config.dbconnectionstring), you can create a database connection, and if there is no database file, the database file is created automatically and the connection is established , but on the Android platform, only with the above code to achieve this function.

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

Unity3d using the embedded database SQLite on the Android platform to resolve problems that cannot be found on the database file

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