Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save data in SQLite in Unity3d

    • 0
    • 2
    • 1
    • 2
    • 1
    • 0
    • 0
    • 0
    • 5.37k
    Comment on it

    To start up with CURD(Create,Update,Read,Delete) operations in unity3D using SQLite. You need to follow few steps:

    Steps for installing and use sqlite browser:

    1.You need to have an SQLite Browser installed in your system: Which you can get from the zip folder attach with the post

    2.Click on the sqlitebrowser.exe file It will not install but will be opened directly. Like This:

    3.Now create a new database with any name MyDB(in my case) by clicking on new database tab and save the database at the asset folder of your unity project.

    4.As soon as you click on save it will ask for table name and fields in the table.Fill it with all neccesary data.

    5.This is all done with the sqlite side you need to done.

    Steps to be performed at unity side:

    1.Create two new folder rename one as Plugins and another as Script under Assets Folder.

    2.Copy sqlite3.def and sqlite3.dll into Plugins folder in your unity project. sqlite3.dll is needed to link dynamically to the sqlite. and sqlite3.def is needed to define what symbols (or functions) needed to link with program using sqlite.

    3.Now create a new c# script in scripts folder under Assets and assign that script to camera.

    4.Now write following line of code in that script:

    
         using UnityEngine;
         using System.Collections;
         using Mono.Data.Sqlite;
         using System.Data; 
         using System;
        public class DatabseOperation : MonoBehaviour {
        void Start () 
        {
            string conn = "URI=file:" + Application.dataPath + "/MyDB.db"; //returns the complete path to database file exist.
    
            Debug.Log("Path:"+conn);//By this line  of code you can see the complete path of the database
      //insert into Database
    
            IDbConnection dbconn;//here we initialize a variable dbconn of type IDbConnection
    
            dbconn = (IDbConnection) new SqliteConnection(conn);//Here we assign dbconn to hold the refrence of SqliteConnection and then type cast it with IDbConnection.
    
            dbconn.Open(); //Open connection to the database once.
    
            IDbCommand dbcmd = dbconn.CreateCommand();//Here we are creating a variable of type databse command
    
            string insertQuery = "INSERT INTO Test(Name,RollNumber) VALUES ('Xyz','1')";//Here we are inserting values in the table.Here field name need to be same as in database you have mentioned.
    
                dbcmd.CommandText = insertQuery ;
    
            IDataReader reader = dbcmd.ExecuteReader(); //This is used to execute the command which is of string type.
    
    
    //read from Database
    
            IDbCommand dbcmd1 = dbconn.CreateCommand();
            string readQuery = "SELECT * FROM Test";//Query to read all data from database
            dbcmd1.CommandText = readQuery;
            IDataReader reader1 = dbcmd1.ExecuteReader();
    
        while (reader1.Read())//this loop will run untill read doesn't reach to the end of the rows of the table
            {
                string name = reader1.GetString(0);//this will read data of the first column
                int rollnumber= reader1.GetInt32(1);//this will read data of the second column
    
                Debug.Log( "  name ="+name+"  rollnumber="+  rollnumber);
            }
    
                //Close all the variale you have created above and put the values to be null
            reader.Close();
            reader = null;
            dbcmd.Dispose();
            dbcmd = null;
                    reader1.Close();
            reader1 = null;
            dbcmd1.Dispose();
            dbcmd1 = null;
            dbconn.Close();
            dbconn = null;
    
        }
    
        }
    

    now run the program from your unity editor and than open the database with the sqlite browser now you can see the values that you have entered.

    Note:Please see the attach files with the post for sqlite browser.exe and sqlite.def/dll file. Thanks, Please leave the comments if you have any query.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: