SQLiteDatabase.Insert C# (CSharp) Method

Insert() public method

Allows the programmer to easily insert into the DB
public Insert ( String tableName, String>.Dictionary data ) : bool
tableName String The table into which we insert the data.
data String>.Dictionary A dictionary containing the column names and data for the insert.
return bool
    public bool Insert(String tableName, Dictionary<String, String> data)
    {
        String columns = "";
        String values = "";
        Boolean returnCode = true;
        foreach (KeyValuePair<String, String> val in data)
        {
            columns += String.Format(" {0},", val.Key.ToString());
            values += String.Format(" @PARM_{0},", val.Key.ToString());
        }
        columns = columns.Substring(0, columns.Length - 1);
        values = values.Substring(0, values.Length - 1);

        try
        {
            SQLiteCommand command = new SQLiteCommand(String.Format("insert into {0}({1}) values({2});", tableName, columns, values));
            foreach (KeyValuePair<String, String> val in data)
            {
                if (val.Value.ToString() == "current_timestamp")
                    command.Parameters.AddWithValue(String.Format("@PARM_{0}", val.Key.ToString()), DateTime.UtcNow);
                else
                    command.Parameters.AddWithValue(String.Format("@PARM_{0}", val.Key.ToString()), val.Value);
            }
            this.ExecuteNonQuery(command);
        }
        catch(Exception fail)
        {
            MessageBox.Show(fail.Message);
            returnCode = false;
        }
        return returnCode;
    }

Usage Example

Example #1
0
        public void AddNewJoke(string message)
        {
            //Create sanitised string with the joke
            List<string> strings = new List<string>(message.Split(' '));
            strings.RemoveAt(0);
            string joke = String.Join(" ", strings.ToArray());
            joke = System.Text.RegularExpressions.Regex.Escape(joke.Replace("'", @"''")) ;

            SQLiteDatabase db = new SQLiteDatabase();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("JokeBody", joke);
            try
            {
                db.Insert("Jokes", data);
            }
            catch (Exception crap)
            {
                Logger.GetLogger().Error(crap.Message);
            }
        }
All Usage Examples Of SQLiteDatabase::Insert