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

probem after building

Discussion in 'Editor & General Support' started by sawi, Mar 19, 2015.

  1. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    hello everyone
    i have to create a database and i used the script bellow for that.
    it works normally and there is no error but the problem is that after building it didn't work even when i click to the buttons nothing happens
    can someone help me please
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine;
    3. using System;
    4. using System.IO;
    5. using System.Collections;
    6. using System.Data;
    7. using System.Text;
    8. using Mono.Data.SqliteClient;
    9. using System.Collections.Generic;
    10.  
    11. public class dbAccess : MonoBehaviour {
    12.     private string connection;
    13.     private IDbConnection dbcon;
    14.     private IDbCommand dbcmd;
    15.     private IDataReader reader;
    16.     private StringBuilder builder;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.        
    21.     }
    22.    
    23.     public void OpenDB(string p)
    24.     {
    25.         Debug.Log("Call to OpenDB:" + p);
    26.         // check if file exists in Application.persistentDataPath
    27.         string filepath = Application.persistentDataPath + "/" + p;
    28.         if(!File.Exists(filepath))
    29.         {
    30.             Debug.LogWarning("File \"" + filepath + "\" does not exist. Attempting to create from \"" +
    31.                             Application.dataPath + "!/assets/" + p);
    32.             // if it doesn't ->
    33.             // open StreamingAssets directory and load the db ->
    34.             WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + p);
    35.             while(!loadDB.isDone) {}
    36.             // then save to Application.persistentDataPath
    37.             File.WriteAllBytes(filepath, loadDB.bytes);
    38.         }
    39.        
    40.         //open db connection
    41.         connection = "URI=file:" + filepath;
    42.         Debug.Log("Stablishing connection to: " + connection);
    43.         dbcon = new SqliteConnection(connection);
    44.         dbcon.Open();
    45.     }
    46.    
    47.     public void CloseDB(){
    48.         reader.Close(); // clean everything up
    49.           reader = null;
    50.           dbcmd.Dispose();
    51.           dbcmd = null;
    52.           dbcon.Close();
    53.           dbcon = null;
    54.     }
    55.    
    56.     public IDataReader BasicQuery(string query){ // run a basic Sqlite query
    57.         dbcmd = dbcon.CreateCommand(); // create empty command
    58.         dbcmd.CommandText = query; // fill the command
    59.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    60.         return reader; // return the reader
    61.    
    62.     }
    63.    
    64.    
    65.     public bool CreateTable(string name,string[] col, string[] colType){ // Create a table, name, column array, column type array
    66.         string query;
    67.         query  = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0];
    68.         for(var i=1; i< col.Length; i++){
    69.             query += ", " + col[i] + " " + colType[i];
    70.         }
    71.         query += ")";
    72.         try{
    73.             dbcmd = dbcon.CreateCommand(); // create empty command
    74.             dbcmd.CommandText = query; // fill the command
    75.             reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    76.         }
    77.         catch(Exception e){
    78.            
    79.             Debug.Log(e);
    80.             return false;
    81.         }
    82.         return true;
    83.     }
    84.    
    85.     public int InsertIntoSingle(string tableName, string colName , string value ){ // single insert
    86.         string query;
    87.         query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")";
    88.         try
    89.         {
    90.             dbcmd = dbcon.CreateCommand(); // create empty command
    91.             dbcmd.CommandText = query; // fill the command
    92.             reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    93.         }
    94.         catch(Exception e){
    95.            
    96.             Debug.Log(e);
    97.             return 0;
    98.         }
    99.         return 1;
    100.     }
    101.    
    102.     public int InsertIntoSpecific(string tableName, string[] col, string[] values){ // Specific insert with col and values
    103.         string query;
    104.         query = "INSERT INTO " + tableName + "(" + col[0];
    105.         for(int i=1; i< col.Length; i++){
    106.             query += ", " + col[i];
    107.         }
    108.         query += ") VALUES (" + values[0];
    109.         for(int i=1; i< col.Length; i++){
    110.             query += ", " + values[i];
    111.         }
    112.         query += ")";
    113.         Debug.Log(query);
    114.         try
    115.         {
    116.             dbcmd = dbcon.CreateCommand();
    117.             dbcmd.CommandText = query;
    118.             reader = dbcmd.ExecuteReader();
    119.         }
    120.         catch(Exception e){
    121.            
    122.             Debug.Log(e);
    123.             return 0;
    124.         }
    125.         return 1;
    126.     }
    127.    
    128.     public int InsertInto(string tableName , string[] values ){ // basic Insert with just values
    129.         string query;
    130.         query = "INSERT INTO " + tableName + " VALUES (" + values[0];
    131.         for(int i=1; i< values.Length; i++){
    132.             query += ", " + values[i];
    133.         }
    134.         query += ")";
    135.         try
    136.         {
    137.             dbcmd = dbcon.CreateCommand();
    138.             dbcmd.CommandText = query;
    139.             reader = dbcmd.ExecuteReader();
    140.         }
    141.         catch(Exception e){
    142.            
    143.             Debug.Log(e);
    144.             return 0;
    145.         }
    146.         return 1;
    147.     }
    148.    
    149.     public ArrayList SingleSelectWhere(string tableName , string itemToSelect,string wCol,string wPar, string wValue){ // Selects a single Item
    150.         string query;
    151.         query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;  
    152.         dbcmd = dbcon.CreateCommand();
    153.         dbcmd.CommandText = query;
    154.         reader = dbcmd.ExecuteReader();
    155.         //string[,] readArray = new string[reader, reader.FieldCount];
    156.         string[] row = new string[reader.FieldCount];
    157.         ArrayList readArray = new ArrayList();
    158.         while(reader.Read()){
    159.             int j=0;
    160.             while(j < reader.FieldCount)
    161.             {
    162.                 row[j] = reader.GetString(j);
    163.                 j++;
    164.             }
    165.             readArray.Add(row);
    166.         }
    167.         return readArray; // return matches
    168.     }
    169.     public List<List<object>> ReadFullTable (string tableName)
    170.     {
    171.         string query;
    172.         query = "SELECT * FROM " + tableName;
    173.         dbcmd = dbcon.CreateCommand ();
    174.         dbcmd.CommandText = query;
    175.         reader = dbcmd.ExecuteReader ();
    176.         List<List<object>> readArray = new List<List<object>> ();
    177.         while (reader.Read())
    178.         {
    179.             List<object> lineArray = new List<object> ();
    180.             for (int i = 0; i < reader.FieldCount; i++)
    181.             {
    182.                 lineArray.Add (reader.GetValue (i));
    183.             } // This reads the entries in a row
    184.             readArray.Add (lineArray); // This makes an array of all the rows
    185.         }
    186.         return readArray; // return matches
    187.     }
    188.     public void DeleteTableContents (string tableName)
    189.     {
    190.         string query;
    191.         query = "DELETE FROM " + tableName;
    192.         dbcmd = dbcon.CreateCommand ();
    193.         dbcmd.CommandText = query;
    194.         reader = dbcmd.ExecuteReader ();
    195.     }
    196.     // Update is called once per frame
    197.     void Update () {
    198.    
    199.     }
    200. }
    201. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Data;
    6. using Mono.Data.SqliteClient;
    7.  
    8. public class usethedatabase : MonoBehaviour {
    9.  
    10.     public string  DatabaseName = "DBprodt.sqdb";
    11.  
    12.     // This is the name of the table we want to use
    13.     public string  TableName = "produittable";
    14.     private dbAccess db ;
    15.  
    16.     void Start ()
    17.     {
    18.         // Give ourselves a dbAccess object to work with, and open it
    19.         db = new dbAccess ();
    20.         db.OpenDB (DatabaseName);
    21.         // Let's make sure we've got a table to work with as well!
    22.         string tableName = TableName;
    23.         string[] columnNames = new string[]{"firstName", "lastName"};
    24.         string[] columnValues = new string[]{"text", "text"};
    25.         //try
    26.         //{
    27.         db.CreateTable (tableName, columnNames, columnValues);
    28.         //}
    29.  
    30.     }
    31.  
    32.     // These variables just hold info to display in our GUI
    33.     private string firstName = "First Name";
    34.     private string  lastName = "Last Name";
    35.     private int DatabaseEntryStringWidth = 100;
    36.     private Vector2 scrollPosition ;
    37.     private List<List<object>> databaseData = new List<List<object>> ();
    38.  
    39.     // This GUI provides us with a way to enter data into our database
    40.     //  as well as a way to view it
    41.     void OnGUI ()
    42.     {
    43.         GUI.Box (new Rect (25, 25, Screen.width - 50, Screen.height - 50), "");
    44.         GUILayout.BeginArea (new Rect (50, 50, Screen.width - 100, Screen.height - 100));
    45.         // This first block allows us to enter new entries into our table
    46.         GUILayout.BeginHorizontal ();
    47.         firstName = GUILayout.TextField (firstName, GUILayout.Width (DatabaseEntryStringWidth));
    48.         lastName = GUILayout.TextField (lastName, GUILayout.Width (DatabaseEntryStringWidth));
    49.         GUILayout.EndHorizontal ();
    50.  
    51.         if (GUILayout.Button ("Add to database"))
    52.         {
    53.             // Insert the data
    54.             InsertRow (firstName, lastName);
    55.             // And update the readout of the database
    56.             databaseData = ReadFullTable ();
    57.         }
    58.         // This second block gives us a button that will display/refresh the contents of our database
    59.         GUILayout.BeginHorizontal ();
    60.         if (GUILayout.Button ("Read Database"))
    61.         {
    62.             databaseData = ReadFullTable ();
    63.         }
    64.         if (GUILayout.Button ("Clear"))
    65.         {
    66.             databaseData.Clear ();
    67.         }
    68.         GUILayout.EndHorizontal ();
    69.  
    70.         GUILayout.Label ("Database Contents");
    71.         scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Height (100));
    72.         foreach (List<object> line in databaseData)
    73.         {
    74.             GUILayout.BeginHorizontal ();
    75.             foreach (object s in line)
    76.             {
    77.                 GUILayout.Label (s.ToString (), GUILayout.Width (DatabaseEntryStringWidth));
    78.             }
    79.             GUILayout.EndHorizontal ();
    80.         }
    81.  
    82.         GUILayout.EndScrollView ();
    83.         if (GUILayout.Button ("Delete All Data"))
    84.         {
    85.             DeleteTableContents ();
    86.             databaseData = ReadFullTable ();
    87.         }
    88.  
    89.         GUILayout.EndArea ();
    90.     }
    91.  
    92.     // Wrapper function for inserting our specific entries into our specific database and table for this file
    93.     private void InsertRow (string firstName, string lastName)
    94.     {
    95.         string[] values = new string[]{("'" + firstName + "'"), ("'" + lastName + "'")};
    96.         db.InsertInto (TableName, values);
    97.     }
    98.  
    99.     // Wrapper function, so we only mess with our table.
    100.     private List<List<object>> ReadFullTable ()
    101.     {
    102.         return db.ReadFullTable (TableName);
    103.     }
    104.  
    105.     // Another wrapper function...
    106.     private void DeleteTableContents ()
    107.     {
    108.         db.DeleteTableContents (TableName);
    109.     }
    110. }
    111.  
     
    Last edited: Mar 19, 2015
  2. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    Well... First glance.. I would put a few error readouts on the ReadFullTable()
    and check that db.ReadFullTable("produittable"); actually did something...

    I would then step by step in Visual Studio, or worst case just dive in the sqllite db "by-hand" in the command line and see if anything had actually been written to it.

    I would check the steps via the command line were correct to produce the output I wanted.
    I would then do a very simple "Connect and Create Table" test case: to ensure my permissions were in order to the database - inside of the unity code.

    When I had a result from that that surprised me - i.e. my simple test case didn't work and I had some error details to share that were of concern; I would post those here and probably get a better answer.

    That is what I have.
    Hope it helps.
     
  3. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    actually i think that the problem with me is in the path because i have no problem until i building
     
  4. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    Then you should get
    • Code (csharp):
      1. Debug.LogWarning("File \"" + filepath + "\" does not exist. Attempting to create from \"" +
      2.                            Application.dataPath + "!/assets/" + p);

    Do you? Spit that into an onscreen text rather than a log if you can't see it.
     
  5. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    thank you :)