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 'Editor & General Support' started by sawi, Mar 11, 2015.

  1. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    hello
    i find this code in java script and i tried to translate it into a c#
    but u have an error
    Assets/Plugins/dbAccess.cs(57,13): error CS0161: `dbAccess.DeleteTableContents(string)': not all code paths return a value
    can anyone help me to correct tha one ?
    thank you
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Data;
    5. using Mono.Data.SqliteClient;
    6.  
    7. public class dbAccess : MonoBehaviour {
    8.     private string connection;
    9.     private IDbConnection dbcon;
    10.     private IDbCommand dbcmd;
    11.     private IDataReader reader;
    12.    
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     public void OpenDB(string p)
    19.     {
    20.         connection = "URI=file:" + p; // we set the connection to our database
    21.         dbcon = new SqliteConnection(connection);
    22.         dbcon.Open();
    23.     }
    24.    
    25.     public void CloseDB(){
    26.         reader.Close(); // clean everything up
    27.         reader = null;
    28.         dbcmd.Dispose();
    29.         dbcmd = null;
    30.         dbcon.Close();
    31.         dbcon = null;
    32.     }
    33.    
    34.     IDataReader BasicQuery(string query){ // run a baic Sqlite query
    35.         dbcmd = dbcon.CreateCommand(); // create empty command
    36.         dbcmd.CommandText = query; // fill the command
    37.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    38.         return reader; // return the reader
    39.        
    40.     }
    41.  
    42. public  ArrayList ReadFullTable(string tableName) {
    43.         string query;
    44.         query = "SELECT * FROM " + tableName;
    45.         dbcmd = dbcon.CreateCommand();
    46.         dbcmd.CommandText = query;
    47.         reader = dbcmd.ExecuteReader();
    48.         var readArray = new ArrayList();
    49.         while(reader.Read()) {
    50.             var lineArray = new ArrayList();
    51.             for (var i=0; i < reader.FieldCount; i++)
    52.                 lineArray.Add(reader.GetValue(i)); // This reads the entries in a row
    53.             readArray.Add(lineArray); // This makes an array of all the rows
    54.         }
    55.         return readArray; // return matches
    56.     }
    57. public  int DeleteTableContents(string tableName){
    58.         string query;
    59.         query = "DELETE FROM " + tableName;
    60.         dbcmd = dbcon.CreateCommand();
    61.         dbcmd.CommandText = query;
    62.         reader = dbcmd.ExecuteReader();
    63.     }
    64.  
    65.  
    66.    
    67.     public  int CreateTable(string name,string[] col, string[] colType){ // Create a table, name, column array, column type array
    68.         string query;
    69.         query = "CREATE TABLE " + name + "(" + col [0] + " " + colType [0] + ")";
    70.         for(var i=1; i < col.Length ; i++){
    71.             query += ", " + col[i] + " " + colType[i];
    72.         }
    73.  
    74.         query += ")";
    75.             try{
    76.             dbcmd = dbcon.CreateCommand(); // create empty command
    77.             dbcmd.CommandText = query; // fill the command
    78.             reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    79.         }
    80.         catch(Exception e){
    81.            
    82.             Debug.Log(e);
    83.             return 0;
    84.         }
    85.         return 1;
    86.         }
    87.        
    88.     public    int InsertIntoSingle(string tableName, string colName , string value ){ // single insert
    89.             string query;
    90.             query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")";
    91.             try
    92.             {
    93.                 dbcmd = dbcon.CreateCommand(); // create empty command
    94.                 dbcmd.CommandText = query; // fill the command
    95.                 reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    96.             }
    97.             catch(Exception e){
    98.                
    99.                 Debug.Log(e);
    100.                 return 0;
    101.             }
    102.             return 1;
    103.         }
    104.        
    105.     public    int InsertIntoSpecific(string tableName, string[] col, string[] values){ // Specific insert with col and values
    106.             string query;
    107.             query = "INSERT INTO " + tableName + "(" + col[0];
    108.         for(var i=1; i<col.Length; i++){
    109.             query += ", " + col[i];
    110.         }
    111.                 query += ") VALUES (" + values[0];
    112.         for(var i=1; i<values.Length; i++){
    113.             query += ", " + values[i];
    114.         }
    115.                 query += ")";
    116.                 try
    117.                 {
    118.                 dbcmd = dbcon.CreateCommand();
    119.                 dbcmd.CommandText = query;
    120.                 reader = dbcmd.ExecuteReader();
    121.             }
    122.             catch(Exception e){
    123.                
    124.                 Debug.Log(e);
    125.                 return 0;
    126.             }
    127.             return 1;
    128.             }
    129.            
    130.     public        int InsertInto(string tableName , string[] values ){ // basic Insert with just values
    131.                 string query;
    132.                 query = "INSERT INTO " + tableName + " VALUES (" + values[0];
    133.         for(var i=1; i<values.Length; i++){
    134.             query += ", " + values[i];
    135.         }
    136.                     query += ")";
    137.                     try
    138.                     {
    139.                     dbcmd = dbcon.CreateCommand();
    140.                     dbcmd.CommandText = query;
    141.                     reader = dbcmd.ExecuteReader();
    142.                 }
    143.                 catch(Exception e){
    144.                    
    145.                     Debug.Log(e);
    146.                     return 0;
    147.                 }
    148.                 return 1;
    149.                 }
    150.                
    151.                 public string[] SingleSelectWhere(string tableName , string itemToSelect,string wCol,string wPar, string wValue){ // Selects a single Item
    152.                     string query;
    153.                     query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;  
    154.                     dbcmd = dbcon.CreateCommand();
    155.                     dbcmd.CommandText = query;
    156.                     reader = dbcmd.ExecuteReader();
    157.                     string[] readArray = new string[reader.RecordsAffected];
    158.                     int i=0;
    159.                     while(reader.Read()){
    160.                         readArray[i]=reader.GetString(0); // Fill array with all matches
    161.                         i++;
    162.                     }
    163.                     return readArray; // return matches
    164.                 }
    165.                
    166.                
    167.                
    168.                
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,000
    That function should return some int value (missing return reader; perhaps?)
    public int DeleteTableContents(string tableName)

    if it doesnt need to return any value, can change it to
    public void DeleteTableContents(string tableName)
     
  3. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    thank you