SQLite INSERT statement

I created the following insertion method, which works quite well, but I know that it can be more efficient. Can someone show me how to convert this method to use parameters and / or increase its efficiency?

public static void SQLiteTableINSERT(string tableName)
{
    int colCount = 0;
    using (SQLiteConnection Conn = new SQLiteConnection(SQLiteConn.Conn))
    {
        using (SQLiteTransaction sqliteTrans = Conn.BeginTransaction())
        {
            using (SQLiteCommand cmd = Conn.CreateCommand())
            {
                DataTableColumnNames();

                string query = "INSERT INTO " + tableName + "(";

                foreach (string name in DtColumns)
                {
                    query += "[" + name + "]";
                    ++colCount;

                    if (colCount < DtColumns.Count())
                        query += ",";
                }

                query += ")";
                query += " VALUES(";

                for (int i = 0; i < LocalDataSet.LocalDs.Tables[0].Rows.Count; ++i)
                {
                    cmd.CommandText = query;

                    foreach (DataColumn col in LocalDataSet.LocalDs.Tables[0].Columns)
                    {
                        string temp = LocalDataSet.LocalDs.Tables[0].Rows[i][col, DataRowVersion.Current].ToString();

                        if (temp == "True")
                            cmd.CommandText += 1;

                        else if (temp == "")
                            cmd.CommandText += 0;

                        if (temp != "True" && temp != "")
                            cmd.CommandText += "'" +temp + "'";

                        cmd.CommandText += ",";
                    }

                    cmd.CommandText = cmd.CommandText.Remove(cmd.CommandText.LastIndexOf(","));

                    cmd.CommandText += ")";

                    cmd.ExecuteNonQuery();
                }
            }

            sqliteTrans.Commit();
        }

    }
}
+2
source share
1 answer

You really have to switch this to using a prepared statement, and then bind the data to the parameters in that prepared statement. The basics are explained here for C / C ++:

http://www.sqlite.org/cintro.html

I suspect you are using dotConnect, so you can refer to this for your specific example:

http://www.devart.com/dotconnect/sqlite/docs/Parameters.html

. SQL , , . SQL- SQLite . SQL- .

+2

All Articles