Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SQLite database

Discussion in 'Scripting' started by abdallah123, Sep 29, 2011.

  1. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    Hi,
    I know that this is a unity forum but I thought that someone can help me here.
    So does anyone know what should I install to read a SQLite database (.s3db) as a (.s3db) file cz I have a game that must read from a s3db database and when I moved this game to another pc the database was read as a S3DB file with no extension so what should I install?
    i used the SQLite admin to create the database.

    On the vista laptop the database is a (.s3db) file but the game is not able to read data from the database what could be the reason? On a Xp laptop the database is a S3DB file with no extension how can I fix that?
     
  2. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    3 easy steps to resolve this

    1. open google
    2. search for "program to read .s3db file"
    3. read

    enjoy
     
  3. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    Ok thank you, I know those steps but that didn't work.
     
  4. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    My problem is that I have a SQLite database linked to unity and this database will be dropped when the project is built I have to place another one manually in the project's folder so I did that and I've written a code that searches for the data's path dynamically and it worked but when I moved this game to another pc the game could not load data from database what could be the problem?
     
  5. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Did you check the log files ?
     
  6. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    I found the log files there is:
    Editor and Editor-prev but there's no error messages.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The XP laptop is probably set up to hide filename extensions. It's a folder setting in Windows Explorer.

    Check your output log in your standalone build to determine what's happening. Is it not finding the database file? Is the connection failing? Is the SQL query incorrect?
     
  8. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    ok where should I find this file log if I was searching for the game's log file and not the editor?
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  10. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    yeah I saw this thread they are talking about webplayer log file but I think that this is what I need:
    EXECNAME_Data\output_log.txt

    thank you for your help!!
     
  11. namoricoo

    namoricoo

    Joined:
    Apr 14, 2011
    Posts:
    534
    I'm the Engineer who put together UniSql, now in the Asset Store. When you compile unity. It does not export the file, you always have to manually add the the file. There are also several other problems with Sqlite. How slow it can get. That's why I developed UniDDatabase. A video game Optimized database. You get Zero configuration. Automatic data persistance, and when I release 3.0 later this week, You be able to export your current Sqlite file to SQL. Import SQL to UniDDatabase in order to take advantage of all the inovations. In video two, you see how I build a simple example with Zero configuration and get basic data persistance at the same time.



     
  12. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    Hi, I didn't understand much about what you're telling me but I think you're trying to export the SQLite database as a SQL database and minimize its errors. So good luck with that :)
     
  13. namoricoo

    namoricoo

    Joined:
    Apr 14, 2011
    Posts:
    534
    1)In your post you said" "My problem is that I have a SQLite database linked to unity and this database will be dropped when the project is built I have to place another one manually in the project's folder" My short response. That's normal. You won't be able to get it to behave any differently. I spent month try when I developed UniSqlite.


    2) I used Sqlite in the past. It was not optimized for Video games. What I was trying to show In the Video is how you to migrate data from other systems into My product. For example, since you are using Sqlite. There's a 99% probability that you should be able to export it to SQL. When I use Sqlite I use SQlite Browser, it's a free program. If I go up to File>>Export>>There's an option to Export to SQL. All I was saying it that My product will be able to import SQL files so that you won't have to re-enter the information twice.

    3)Video 2 is show how I'm able to Build a database in unity and not have to copy the file over manually. It's all done for me. I get the database, I can also reset the database back to the original state with one line of code, and I get an Encrypted file. All things that Sqlite does not do. I hope that made a little more sense.
     
  14. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    OK, I think I am gonna try to create an XML database to fix the problem.
    But thank you I will see your videos and hope to hear about your solutions soon :)
     
  15. namoricoo

    namoricoo

    Joined:
    Apr 14, 2011
    Posts:
    534
    Here's a Link to my Videos. I'm working on new videos. My solution is already in the Asset Store. The update that will allow you to import Sql files will come out soon. Look for UniDDatabase 3.0. It's the path of least resistance. To use xmL you will have to waste a lot of time writing code. Doing conversions, and wasting time with a product that's not optimized for video games. With my product. You'll be taking the path of least resistance. It's a four step process.

    1)Export Sqlite to Sql
    2)Import Sql to UniDDatabase
    3)Adapt your code to work with UniDDatabase
    4)Build and run.

    There also an Export file. If you want to export any changes you make back to Sql in order to re-import modifications into Sqlite.
    http://vimeo.com/user7296588/videos
     
  16. niks07

    niks07

    Joined:
    Oct 11, 2011
    Posts:
    11
    hello ol.

    i am new in unity.

    i want to create a game in which i need to create local database.
    i read many a posts on unity/google but i cant find anything very clear and fast..

    so any one can tell me the clear steps for creating a local database in unity-game for iphone/ipad...???

    here is the code that i used but it only creates the database file while i run it on MAC..

    //==============

    /*--- this code i am using to create database------*/

    import System.Data; // we import our data class
    import Mono.Data.Sqlite; // we import sqlite

    public class dbAccess
    {
    // variables for basic query access
    var connection : String;
    var dbcon : IDbConnection;
    var dbcmd : IDbCommand;
    var reader : IDataReader;

    function OpenDB(p : String)
    {
    connection = "URI=file:" + p; // we set the connection to our database
    dbcon = new SqliteConnection(connection);
    dbcon.Open();
    }

    function BasicQuery(q : String, r : boolean) // run a basic Sqlite query
    {
    dbcmd = dbcon.CreateCommand(); // create empty command
    dbcmd.CommandText = q; // fill the command
    reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    if(r) // if we want to return the reader
    {
    return reader; // return the reader
    }
    }

    // This returns a 2 dimensional ArrayList with all the
    // data from the table requested
    function ReadFullTable(tableName : String)
    {
    var query : String;
    query = "SELECT * FROM " + tableName;
    dbcmd = dbcon.CreateCommand();
    dbcmd.CommandText = query;
    reader = dbcmd.ExecuteReader();
    var readArray = new ArrayList();
    while(reader.Read())
    {
    var lineArray = new ArrayList();
    for (var i : int = 0; i < reader.FieldCount; i++)
    lineArray.Add(reader.GetValue(i)); // This reads the entries in a row
    readArray.Add(lineArray); // This makes an array of all the rows
    }
    return readArray; // return matches
    }

    // This function deletes all the data in the given table. Forever. WATCH OUT! Use sparingly, if at all
    function DeleteTableContents(tableName : String)
    {
    var query : String;
    query = "DELETE FROM " + tableName;
    dbcmd = dbcon.CreateCommand();
    dbcmd.CommandText = query;
    reader = dbcmd.ExecuteReader();
    }

    function CreateTable(name : String, col : Array, colType : Array) // Create a table, name, column array, column type array
    {
    var query : String;
    query = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0];
    for(var i=1; i < col.length; i++)
    {
    query += ", " + col + " " + colType;
    }
    query += ")";
    dbcmd = dbcon.CreateCommand(); // create empty command
    dbcmd.CommandText = query; // fill the command
    reader = dbcmd.ExecuteReader(); //execute command which returns a reader
    }

    function InsertIntoSingle(tableName : String, colName : String, value : String) // single insert
    {
    var query : String;
    query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")";
    dbcmd = dbcon.CreateCommand(); // create empty command
    dbcmd.CommandText = query; // fill the command
    reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    }

    function InsertIntoSpecific(tableName : String, col : Array, values : Array) // Specific insert with col and values
    {
    var query : String;
    query = "INSERT INTO " + tableName + "(" + col[0];
    for(var i=1; i<col.length; i++)
    {
    query += ", " + col;
    }
    query += ") VALUES (" + values[0];
    for(i=1; i<values.length; i++)
    {
    query += ", " + values;
    }
    query += ")";
    dbcmd = dbcon.CreateCommand();
    dbcmd.CommandText = query;
    reader = dbcmd.ExecuteReader();
    }

    function InsertInto(tableName : String, values : Array) // basic Insert with just values
    {
    var query : String;
    query = "INSERT INTO " + tableName + " VALUES (" + values[0];
    for(var i=1; i<values.length; i++)
    {
    query += ", " + values;
    }
    query += ")";
    dbcmd = dbcon.CreateCommand();
    dbcmd.CommandText = query;
    reader = dbcmd.ExecuteReader();
    }

    // This function reads a single column
    // wCol is the WHERE column, wPar is the operator you want to use to compare with,
    // and wValue is the value you want to compare against.
    // Ex. - SingleSelectWhere("puppies", "breed", "earType", "=", "floppy")
    // returns an array of matches from the command: SELECT breed FROM puppies WHERE earType = floppy;
    function SingleSelectWhere(tableName : String, itemToSelect : String, wCol : String, wPar : String, wValue : String) // Selects a single Item
    {
    var query : String;
    query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;
    dbcmd = dbcon.CreateCommand();
    dbcmd.CommandText = query;
    reader = dbcmd.ExecuteReader();
    var readArray = new Array();
    while(reader.Read())
    {
    readArray.Push(reader.GetString(0)); // Fill array with all matches
    }
    return readArray; // return matches
    }


    function CloseDB()
    {
    reader.Close(); // clean everything up
    reader = null;
    dbcmd.Dispose();
    dbcmd = null;
    dbcon.Close();
    dbcon = null;
    }
    }

    //============

    /*--- this code i am using to get my GUI on screen------*/


    /* Script for testing out SQLite in Javascript
    This script is a GUI script - attach it to your main camera.
    It creates/opens a SQLite database, and with the GUI you can read and write to it.
    */

    // This is the file path of the database file we want to use
    // Right now, it'll load TestDB.sqdb in the project's root folder.
    // If one doesn't exist, it will be automatically created.

    public var DatabaseName : String = "TestDB.sqlite";

    // This is the name of the table we want to use
    public var TableName : String = "TestTable";
    var db : dbAccess;

    function Start()
    {
    // Give ourselves a dbAccess object to work with, and open it
    db = new dbAccess();
    db.OpenDB(DatabaseName);
    // Let's make sure we've got a table to work with as well!
    var tableName = TableName;
    var columnNames = new Array("firstName","lastName");
    var columnValues = new Array("text","text");

    try
    {
    db.CreateTable(tableName,columnNames,columnValues) ;
    }

    catch(e)// Do nothing - our table was already created
    {
    //- we don't care about the error, we just don't want to see it
    }
    }

    // These variables just hold info to display in our GUI
    var firstName : String = "First Name";
    var lastName : String = "Last Name";
    var DatabaseEntryStringWidth = 100;
    var scrollPosition : Vector2;
    var databaseData : ArrayList = new ArrayList();

    // This GUI provides us with a way to enter data into our database
    // as well as a way to view it

    function OnGUI()
    {
    GUI.Box(Rect (25,25,Screen.width - 50, Screen.height - 50),"");
    GUILayout.BeginArea(Rect(50, 50, Screen.width - 100, Screen.height - 100));

    // This first block allows us to enter new entries into our table
    GUILayout.BeginHorizontal();
    firstName = GUILayout.TextField(firstName, GUILayout.Width (DatabaseEntryStringWidth));
    lastName = GUILayout.TextField(lastName, GUILayout.Width (DatabaseEntryStringWidth));
    GUILayout.EndHorizontal();

    if (GUILayout.Button("Add to database"))
    {
    // Insert the data
    InsertRow(firstName,lastName);
    // And update the readout of the database
    databaseData = ReadFullTable();
    }
    // This second block gives us a button that will display/refresh the contents of our database
    GUILayout.BeginHorizontal();
    if (GUILayout.Button ("Read Database"))
    databaseData = ReadFullTable();
    if (GUILayout.Button("Clear"))
    databaseData.Clear();
    GUILayout.EndHorizontal();

    GUILayout.Label("Database Contents");
    scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100));
    for (var line : ArrayList in databaseData)
    {
    GUILayout.BeginHorizontal();
    for (var s in line)
    {
    GUILayout.Label(s.ToString(), GUILayout.Width(DatabaseEntryStringWidth));
    }
    GUILayout.EndHorizontal();
    }

    GUILayout.EndScrollView();

    if (GUILayout.Button("Delete All Data"))
    {
    DeleteTableContents();
    databaseData = ReadFullTable();
    }

    GUILayout.EndArea();
    }

    // Wrapper function for inserting our specific entries into our specific database and table for this file
    function InsertRow(firstName, lastName)
    {
    var values = new Array(("'"+firstName+"'"),("'"+lastName+"'"));
    db.InsertInto(TableName, values);
    }

    // Wrapper function, so we only mess with our table.
    function ReadFullTable()
    {
    return db.ReadFullTable(TableName);
    }

    // Another wrapper function...
    function DeleteTableContents()
    {
    db.DeleteTableContents(TableName);
    }
     
  17. abdallah123

    abdallah123

    Joined:
    Aug 5, 2011
    Posts:
    146
    Hi,
    you can see my sqlite connection file in the attachments and after that you finish your database you must drag it and put it in the assets and then it will work this is how it works for pc games I don't know if it's the same for ipad, and don't forget to add the database in the folder of the built game when you finish building your game and download the SQLite.dll and SQLite3.dll from http://forum.unity3d.com/threads/106495-Urgent. I added them there and put them in a plugin folder in your game so that you can get them in the built game folder after that you build your game. If you need to post a new thread access forum actions> new thread if you want to add your own thread Good luck :)
    In my game I added all the needed functions in this file and I called the functions from other functions and .js file or classes.
     

    Attached Files:

  18. niks07

    niks07

    Joined:
    Oct 11, 2011
    Posts:
    11
    hello.

    1. i am trying to create database for iphone.

    2. i didn't get how and where i can put the database file in xcode build project.
    please tell me how i can do this.

    3. i had downloaded the sqlite3.dll but i didn't find the file sqlite.dll in the link u given above.

    thanks
     
    Last edited: Oct 17, 2011
  19. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    hello
    i am new in unity and in programming in general can any one help to translate this code into the c#
    thank you
     
  20. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221