Search Unity

problem with my database

Discussion in 'Scripting' started by sawi, Mar 29, 2015.

  1. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    hello
    i have a problem in retrieving data from my database .
    i use those scripts bellow to read the database and to select data from it
    Code (CSharp):
    1. public ArrayList ReadFullTable (string tableName)
    2.     {
    3.         string query;
    4.         query = "SELECT * FROM " + tableName;
    5.         dbcmd = dbcon.CreateCommand ();
    6.         dbcmd.CommandText = query;
    7.         reader = dbcmd.ExecuteReader ();
    8.         ArrayList readArray = new ArrayList ();
    9.         while (reader.Read())
    10.         {
    11.             ArrayList lineArray = new ArrayList ();
    12.             for (int i = 0; i < reader.FieldCount; i++)
    13.             {
    14.                 lineArray.Add (reader.GetValue (i));
    15.             } // This reads the entries in a row
    16.             readArray.Add (lineArray); // This makes an array of all the rows
    17.         }
    18.         return readArray; // return matches
    19.     }
    20. public string QueryString(string tablename, string column, string col_name, string value)
    21.     {
    22.                 string text = "Not Found";
    23.                 string query;
    24.                 query = "SELECT " + column + " FROM " + tablename + " WHERE " + col_name + "='" + value + "'";  
    25.                 dbcmd = dbcon.CreateCommand ();
    26.                 dbcmd.CommandText = query;
    27.                 reader = dbcmd.ExecuteReader ();
    28.  
    29.         if(reader.Read())
    30.         text = reader.GetString(0);
    31.         else
    32.             Debug.Log("QueryString - nothing to read...");
    33.         reader.Close();
    34.         dbcon.Close();
    35.         return text;
    36.     }
    and i have anther script
    Code (CSharp):
    1. private string firstName = "First Name";
    2.     private string  lastName = "Last Name";
    3.     private string get = "GET";
    4.     private string find = "FIND";
    5.     private int DatabaseEntryStringWidth = 100;
    6.     private Vector2 scrollPosition ;
    7.     private ArrayList databaseData = new ArrayList ();
    8.    
    9.     // This GUI provides us with a way to enter data into our database
    10.     //  as well as a way to view it
    11.     void OnGUI ()
    12.     {
    13.         GUI.Box (new Rect (25, 25, Screen.width - 50, Screen.height - 50), "");
    14.         GUILayout.BeginArea (new Rect (50, 50, Screen.width - 100, Screen.height - 100));
    15.         // This first block allows us to enter new entries into our table
    16.         GUILayout.BeginHorizontal ();
    17.         firstName = GUILayout.TextField (firstName, GUILayout.Width (DatabaseEntryStringWidth));
    18.         lastName = GUILayout.TextField (lastName, GUILayout.Width (DatabaseEntryStringWidth));
    19.         get = GUILayout.TextField (get, GUILayout.Width (DatabaseEntryStringWidth));
    20.         find = GUILayout.TextField (find, GUILayout.Width (DatabaseEntryStringWidth));
    21.         GUILayout.EndHorizontal ();
    22.        
    23.         if (GUILayout.Button ("Add to database"))
    24.         {
    25.             // Insert the data
    26.             InsertRow (firstName, lastName);
    27.             // And update the readout of the database
    28.             databaseData = ReadFullTable ();
    29.  
    30.         }
    31.         // This second block gives us a button that will display/refresh the contents of our database
    32.         GUILayout.BeginHorizontal ();
    33.         if (GUILayout.Button ("Read Database"))
    34.         {
    35.             databaseData = ReadFullTable ();
    36.        
    37.         }
    38.         if (GUILayout.Button ("Clear"))
    39.         {
    40.             databaseData.Clear ();
    41.         }
    42.         GUILayout.EndHorizontal ();
    43.        
    44.         GUILayout.Label ("Database Contents");
    45.         scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Height (100));
    46.         foreach (ArrayList line in databaseData)
    47.         {
    48.             GUILayout.BeginHorizontal ();
    49.             foreach (object s in line)
    50.             {
    51.                 GUILayout.Label (s.ToString (), GUILayout.Width (DatabaseEntryStringWidth));
    52.             }
    53.             GUILayout.EndHorizontal ();
    54.         }
    55.        
    56.         GUILayout.EndScrollView ();
    57.         if (GUILayout.Button("search"))
    58.             {
    59.             get = GetDefinitation(find);
    60.             databaseData = ReadFullTable();
    61.             }
    62.         if (GUILayout.Button ("Delete All Data"))
    63.         {
    64.             DeleteTableContents ();
    65.         databaseData = ReadFullTable ();
    66.         }
    67.        
    68.         GUILayout.EndArea ();
    69.     }
    70.    
    71.     // Wrapper function for inserting our specific entries into our specific database and table for this file
    72.     private void InsertRow (string firstName, string lastName)
    73.     {
    74.         string[] values = new string[]{("'" + firstName + "'"), ("'" + lastName + "'")};
    75.         db.InsertInto (TableName, values);
    76.     }
    77.    
    78.     // Wrapper function, so we only mess with our table.
    79.     private ArrayList ReadFullTable ()
    80.     {
    81.         return db.ReadFullTable(TableName);
    82.     }
    83.     public string GetDefinitation(string value)
    84.     {
    85.         return db.QueryString(TableName,"lastName","firstName", value);
    86.     }
    87.     // Another wrapper function...
    88.     private void DeleteTableContents ()
    89.     {
    90.         db.DeleteTableContents (TableName);
    91.     }
    92. }
    all the button works fine but the problem is in the button called 'search' when i click on it i have an error "SqliteSyntaxException: out of memory"
    i can't find where the problem is ?!! can anyone help me please ?!
    thank you