Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to connect a sqlite DB in unity 3d 5?(no plugins)

Hello i'm working on an android game project in unity3d 5 (language c#)that needs to save some data to sqlite database. the game doesn't have a lot of data to store it has only 1 table .there are some plugins for unity that connect to sqlite but i don't want to use them . so how can i store my data on sqlite file in unity ?

like image 204
Farhod ODIN Avatar asked Dec 15 '22 10:12

Farhod ODIN


1 Answers

You can find your answer here

  1. Create new folder under Assets Folder Rename it Plugins .
  2. Copy sqlite3.def and sqlite3.dll into Assets/Plugins in your unity project .You can download these files here http://www.sqlite.org/download.html for windows (Precompiled Binaries for Windows)
  3. Download SQLite Browser http://sourceforge.net/projects/sqlitebrowser/ or http://sqliteadmin.orbmu2k.de/ download SQLite Administrator tool
  4. Create Database in Assets folder in your unity project using SQLite Browser.
  5. Copy System.Data.dll and Mono.Data.Sqlite.dll from **C:\Program Files (x86)\Unity \Editor\Data\Mono\lib\mono\2.0* and paste them in your Assets/Plugins* folder in your unity project.
  6. Add these namespaces using Mono.Data.Sqlite; using System.Data; using System;
  7. string conn= "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db";

Replace PickAndPlaceDatabase.s3db with your database name

void Start () {
    string conn = "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db"; //Path to database.
    IDbConnection dbconn;
    dbconn = (IDbConnection) new SqliteConnection(conn);
    dbconn.Open(); //Open connection to the database.
    IDbCommand dbcmd = dbconn.CreateCommand();
    string sqlQuery = "SELECT value,name, randomSequence " + "FROM PlaceSequence";
    dbcmd.CommandText = sqlQuery;
    IDataReader reader = dbcmd.ExecuteReader();
    while (reader.Read())
    {
        int value = reader.GetInt32(0);
        string name = reader.GetString(1);
        int rand = reader.GetInt32(2);

        Debug.Log( "value= "+value+"  name ="+name+"  random ="+  rand);
    }
    reader.Close();
    reader = null;
    dbcmd.Dispose();
    dbcmd = null;
    dbconn.Close();
    dbconn = null;
}

enter image description here enter image description here

Further SQLite Help Visit : http://www.tutorialspoint.com/sqlite/

like image 74
Hossein Rashno Avatar answered Dec 17 '22 01:12

Hossein Rashno