Esempio n. 1
0
        public int InsertRow(string tableName, params SQLiteField[] fields)
        {
            StringBuilder cmdBuilder = new StringBuilder();

            cmdBuilder.Append($"INSERT INTO {tableName} (");
            StringBuilder valueRefBuilder = new StringBuilder();

            valueRefBuilder.Append(" VALUES (");

            SQLiteCommand cmd = new SQLiteCommand(connection);

            for (int i = 0; i < fields.Length; ++i)
            {
                SQLiteField field = fields[i];

                cmdBuilder.Append($"{field.columnName}");
                valueRefBuilder.Append("?");
                cmd.Parameters.Add(field.value);

                if (i < fields.Length - 1)
                {
                    cmdBuilder.Append(",");
                    valueRefBuilder.Append(",");
                }
            }
            cmdBuilder.Append(")");
            valueRefBuilder.Append(")");
            cmdBuilder.Append(valueRefBuilder.ToString());
            cmdBuilder.Append(";");

            cmd.CommandText = cmdBuilder.ToString();
            return(cmd.ExecuteNonQuery());
        }
Esempio n. 2
0
        public int UpdateRow(string tableName, string whereCondition, params SQLiteField[] fields)
        {
            SQLiteCommand cmd        = new SQLiteCommand(connection);
            StringBuilder cmdBuilder = new StringBuilder();

            cmdBuilder.Append($"UPDATE {tableName} SET ");
            for (int i = 0; i < fields.Length; ++i)
            {
                SQLiteField field = fields[i];
                cmdBuilder.Append($"{field.columnName}=?");
                cmd.Parameters.Add(field.value);
            }
            if (!string.IsNullOrEmpty(whereCondition))
            {
                cmdBuilder.Append($"WHERE {whereCondition}");
            }
            cmdBuilder.Append(";");

            cmd.CommandText = cmdBuilder.ToString();
            return(cmd.ExecuteNonQuery());
        }