Ejemplo n.º 1
0
        public int ExecuteNonQuery(string query)
        {
            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(-1);
            }
            int result = -1;

            try
            {
                this.connection.Open();
                this.command             = this.connection.CreateCommand();
                this.command.CommandText = query;
                result = this.command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to execute query.\n" + ex.Message);
                }
            }
            finally
            {
                this.command.Dispose();
                this.connection.Close();
            }
            return(result);
        }
Ejemplo n.º 2
0
        public int Insert(string tableName, List <SQLiteDB.DB_DataPair> dataList)
        {
            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(-1);
            }
            if (!this.IsTableExists(tableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Table does not exists!");
                }
                return(-1);
            }
            int result = -1;

            try
            {
                string str  = "insert into " + tableName + "(";
                string text = " values('";
                for (int i = 0; i < dataList.Count; i++)
                {
                    SQLiteDB.DB_DataPair dB_DataPair = dataList[i];
                    if (i == dataList.Count - 1)
                    {
                        str  = str + dB_DataPair.fieldName + ") ";
                        text = text + dB_DataPair.value + "')";
                    }
                    else
                    {
                        str  = str + dB_DataPair.fieldName + ",";
                        text = text + dB_DataPair.value + "','";
                    }
                }
                string commandText = str + text;
                this.connection.Open();
                this.command             = this.connection.CreateCommand();
                this.command.CommandText = commandText;
                result = this.command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to insert.\n" + ex.Message);
                }
            }
            finally
            {
                this.command.Dispose();
                this.connection.Close();
            }
            return(result);
        }
Ejemplo n.º 3
0
 public bool ConnectToDefaultDatabase(string dbName, bool loadFresh)
 {
     if (string.IsNullOrEmpty(this._dbPath.Trim()))
     {
         this._dbPath = Application.persistentDataPath;
     }
     if (loadFresh)
     {
         try
         {
             string text  = Path.Combine(Application.streamingAssetsPath, dbName);
             string text2 = Path.Combine(this._dbPath, dbName);
             if (Application.platform == RuntimePlatform.Android)
             {
                 text = "jar:file://" + Application.dataPath + "!/assets/" + dbName;
                 WWW wWW = new WWW(text);
                 while (!wWW.isDone)
                 {
                 }
                 if (!wWW.isDone)
                 {
                     if (SQLiteEventListener.onErrorInvoker != null)
                     {
                         SQLiteEventListener.onErrorInvoker("Database not created, please check the default database file. ");
                     }
                     bool result = false;
                     return(result);
                 }
                 if (File.Exists(text2))
                 {
                     File.Delete(text2);
                 }
                 File.WriteAllBytes(text2, wWW.bytes);
             }
             else
             {
                 if (File.Exists(text2))
                 {
                     File.Delete(text2);
                 }
                 File.Copy(text, text2);
             }
         }
         catch (Exception ex)
         {
             if (SQLiteEventListener.onErrorInvoker != null)
             {
                 SQLiteEventListener.onErrorInvoker("Database error, " + ex.Message);
             }
             bool result = false;
             return(result);
         }
     }
     return(this.CreateDatabase(dbName, false));
 }
Ejemplo n.º 4
0
 public string GetStringValue(string fieldName)
 {
     if (this.rowCounter >= 0 && this.dataTable != null && this.dataTable.Count > 0)
     {
         return(this.dataTable[(int)this.rowCounter][fieldName].ToString());
     }
     if (SQLiteEventListener.onErrorInvoker != null)
     {
         SQLiteEventListener.onErrorInvoker("No record found");
     }
     return(string.Empty);
 }
Ejemplo n.º 5
0
 public object GetValue(string fieldName)
 {
     if (this.rowCounter >= 0 && this.dataTable != null && this.dataTable.Count > 0)
     {
         return(this.dataTable[(int)this.rowCounter][fieldName]);
     }
     if (SQLiteEventListener.onErrorInvoker != null)
     {
         SQLiteEventListener.onErrorInvoker("No record found");
     }
     return(null);
 }
Ejemplo n.º 6
0
        public DBReader GetAllData(string tableName)
        {
            if (!this.IsTableExists(tableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Table does not exists!");
                }
                return(null);
            }
            string query = "select * from " + tableName;

            return(this.Select(query));
        }
Ejemplo n.º 7
0
        public bool CreateDatabase(string dbName, bool isOverWrite)
        {
            this._dbName      = dbName.Trim();
            this._isOverWrite = isOverWrite;
            bool result = false;

            if (string.IsNullOrEmpty(this._dbName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database name invalid!");
                }
                return(false);
            }
            string text = Path.Combine(this._dbPath, this._dbName);

            try
            {
                if (this._isOverWrite && File.Exists(text))
                {
                    File.Delete(text);
                }
                if (!File.Exists(text))
                {
                    FileStream fileStream = File.Create(text);
                    fileStream.Close();
                }
                this.connection = new SqliteConnection("URI=file:" + text);
                result          = true;
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker(string.Concat(new object[]
                    {
                        "Unable to create database. \n",
                        ex.Message,
                        "\n",
                        ex.InnerException,
                        "\n",
                        ex.StackTrace
                    }));
                }
                result = false;
            }
            return(result);
        }
Ejemplo n.º 8
0
 public short GetShortValue(string fieldName)
 {
     if (this.rowCounter >= 0 && this.dataTable != null && this.dataTable.Count > 0)
     {
         short result = 0;
         if (short.TryParse(this.dataTable[(int)this.rowCounter][fieldName].ToString(), out result))
         {
             return(result);
         }
     }
     else if (SQLiteEventListener.onErrorInvoker != null)
     {
         SQLiteEventListener.onErrorInvoker("No record found");
     }
     return(0);
 }
Ejemplo n.º 9
0
        public bool DeleteTable(string tableName)
        {
            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(false);
            }
            if (!this.IsTableExists(tableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Table does not exists.");
                }
                return(false);
            }
            bool result = false;

            try
            {
                string commandText = "drop table " + tableName;
                this.connection.Open();
                this.command             = this.connection.CreateCommand();
                this.command.CommandText = commandText;
                this.command.ExecuteNonQuery();
                result = true;
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to delete table.\n" + ex.Message);
                }
                result = false;
            }
            finally
            {
                this.command.Dispose();
                this.connection.Close();
            }
            return(result);
        }
Ejemplo n.º 10
0
        public bool DeleteDatabase()
        {
            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(false);
            }
            bool result = false;

            try
            {
                string path = Path.Combine(this._dbPath, this._dbName);
                if (File.Exists(path))
                {
                    this.Dispose();
                    File.Delete(path);
                    result = true;
                }
                else
                {
                    if (SQLiteEventListener.onErrorInvoker != null)
                    {
                        SQLiteEventListener.onErrorInvoker("Database does not exists!");
                    }
                    result = false;
                }
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to delete database.\n" + ex.Message);
                }
                result = false;
            }
            finally
            {
            }
            return(result);
        }
Ejemplo n.º 11
0
        public int ClearTable(string tableName)
        {
            int result = -1;

            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(-1);
            }
            if (!this.IsTableExists(tableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Table does not exists.");
                }
                return(-1);
            }
            try
            {
                string commandText = "delete from " + tableName;
                this.connection.Open();
                this.command             = this.connection.CreateCommand();
                this.command.CommandText = commandText;
                result = this.command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to clear table.\n" + ex.Message);
                }
            }
            finally
            {
                this.command.Dispose();
                this.connection.Close();
            }
            return(result);
        }
Ejemplo n.º 12
0
        public DBReader Select(string tableName, List <string> fields, SQLiteDB.DB_ConditionPair condition)
        {
            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(null);
            }
            if (!this.IsTableExists(tableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Table does not exists!");
                }
                return(null);
            }
            if (string.IsNullOrEmpty(condition.fieldName.Trim()) || string.IsNullOrEmpty(condition.value.Trim()))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Wrong condition!");
                }
                return(null);
            }
            DBReader result = null;

            try
            {
                string text = "select ";
                for (int i = 0; i < fields.Count; i++)
                {
                    if (i == fields.Count - 1)
                    {
                        text += fields[i];
                    }
                    else
                    {
                        text = text + fields[i] + ",";
                    }
                }
                text = text + " from " + tableName;
                string text2 = text;
                text = string.Concat(new string[]
                {
                    text2,
                    " where ",
                    condition.fieldName,
                    this.GetConditionSymbol(condition.condition),
                    "'",
                    condition.value,
                    "'"
                });
                this.connection.Open();
                this.command             = this.connection.CreateCommand();
                this.command.CommandText = text;
                this.reader = this.command.ExecuteReader();
                result      = new DBReader(this.reader);
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to select data.\n" + ex.Message);
                }
            }
            finally
            {
                this.command.Dispose();
                this.connection.Close();
            }
            return(result);
        }
Ejemplo n.º 13
0
        public int Update(string tableName, List <SQLiteDB.DB_DataPair> dataList, SQLiteDB.DB_ConditionPair condition)
        {
            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(-1);
            }
            if (!this.IsTableExists(tableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Table does not exists!");
                }
                return(-1);
            }
            if (string.IsNullOrEmpty(condition.fieldName.Trim()) || string.IsNullOrEmpty(condition.value.Trim()))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Wrong condition!");
                }
                return(-1);
            }
            int result = -1;

            try
            {
                string text = "update " + tableName + " set ";
                for (int i = 0; i < dataList.Count; i++)
                {
                    if (i == dataList.Count - 1)
                    {
                        string text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            dataList[i].fieldName,
                            "='",
                            dataList[i].value,
                            "' where ",
                            condition.fieldName,
                            this.GetConditionSymbol(condition.condition),
                            "'",
                            condition.value,
                            "'"
                        });
                    }
                    else
                    {
                        string text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            dataList[i].fieldName,
                            "='",
                            dataList[i].value,
                            "',"
                        });
                    }
                }
                this.connection.Open();
                this.command             = this.connection.CreateCommand();
                this.command.CommandText = text;
                result = this.command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to update.\n" + ex.Message);
                }
            }
            finally
            {
                this.command.Dispose();
                this.connection.Close();
            }
            return(result);
        }
Ejemplo n.º 14
0
        public bool CreateTable(DBSchema schema)
        {
            if (!this.Exists)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Database does not exists!");
                }
                return(false);
            }
            if (this.IsTableExists(schema.TableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Table " + schema.TableName + " already exists.");
                }
                return(false);
            }
            if (string.IsNullOrEmpty(schema.TableName))
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("No schema name!");
                }
                return(false);
            }
            if (schema == null || schema.GetTableFields().Count == 0)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Empty schema!");
                }
                return(false);
            }
            bool result = false;

            try
            {
                int    num  = 0;
                string text = "create table " + schema.TableName + "(";
                for (int i = 0; i < schema.GetTableFields().Count; i++)
                {
                    if (i == schema.GetTableFields().Count - 1)
                    {
                        string text2 = text;
                        text = string.Concat(new object[]
                        {
                            text2,
                            schema.GetTableFields()[i].name,
                            " ",
                            this.GetDataType(schema.GetTableFields()[i].type),
                            "(",
                            schema.GetTableFields()[i].size,
                            ") "
                        });
                        if (schema.GetTableFields()[i].isNotNull)
                        {
                            text += "NOT NULL";
                        }
                        if (schema.GetTableFields()[i].isUnique || schema.GetTableFields()[i].isPrimaryKey)
                        {
                            text += "UNIQUE";
                        }
                        if (schema.GetTableFields()[i].isPrimaryKey)
                        {
                            num = i + 1;
                        }
                        if (num > 0)
                        {
                            text = text + ",PRIMARY KEY(" + schema.GetTableFields()[num - 1].name + ")";
                        }
                        text += ")";
                    }
                    else
                    {
                        string text2 = text;
                        text = string.Concat(new object[]
                        {
                            text2,
                            schema.GetTableFields()[i].name,
                            " ",
                            this.GetDataType(schema.GetTableFields()[i].type),
                            "(",
                            schema.GetTableFields()[i].size,
                            ") "
                        });
                        if (schema.GetTableFields()[i].isNotNull)
                        {
                            text += "NOT NULL";
                        }
                        if (schema.GetTableFields()[i].isUnique || schema.GetTableFields()[i].isPrimaryKey)
                        {
                            text += "UNIQUE";
                        }
                        if (schema.GetTableFields()[i].isPrimaryKey)
                        {
                            num = i + 1;
                        }
                        text += ",";
                    }
                }
                this.connection.Open();
                this.command             = this.connection.CreateCommand();
                this.command.CommandText = text;
                this.command.ExecuteNonQuery();
                result = true;
            }
            catch (Exception ex)
            {
                if (SQLiteEventListener.onErrorInvoker != null)
                {
                    SQLiteEventListener.onErrorInvoker("Unable to create table.\n" + ex.Message);
                }
                result = false;
            }
            finally
            {
                this.command.Dispose();
                this.connection.Close();
            }
            return(result);
        }