Note that there are some explanatory texts on larger screens.

plurals
  1. POEditing table fields?
    primarykey
    data
    text
    <p>I have a C# winforms application with SQLite as database which have a few tables. </p> <p>The application has a <code>datagridview</code> control that represents data from database.</p> <p>I want a user to be able to change database table fields, which is easy by using SQL `ALTER query.</p> <p>The problem comes when the user wants to insert some data to the database table because I need to know the table's field name in order to construct a valid <code>INSERT</code> query.</p> <p>I usually just hard-coded field names, because I never had an option that allows user to change the table's field name in a database.</p> <p>My idea is that my <code>INSERT</code> query uses <code>datagridview</code>'s column names, so, when a user changes the table's field name, it also changes the grid's field names, and every time the application runs, grid's columns are named to the names same as database table has.</p> <p>That way I always have an access to the right field name.</p> <p>Or can I just use an index instead of string to reference a field?</p> <p>Usually I use this code to insert data.</p> <pre><code>class SQLiteDatabase { //... public bool Insert(String tableName, Dictionary&lt;String, String&gt; data) { String columns = ""; String values = ""; Boolean returnCode = true; foreach (KeyValuePair&lt;String, String&gt; val in data) { columns += String.Format(" {0},", val.Key.ToString()); values += String.Format(" '{0}',", val.Value); } columns = columns.Substring(0, columns.Length - 1); values = values.Substring(0, values.Length - 1); try { this.ExecuteNonQuery(String.Format("insert into {0}({1}) values({2});", tableName, columns, values)); } catch(Exception fail) { MessageBox.Show(fail.Message); returnCode = false; } return returnCode; } //... } db = new SQLiteDatabase(); Dictionary&lt;String, String&gt; myData = new Dictionary&lt;String, String&gt;(); myData.Add("NAME", "something"); myData.Add("LAST_NAME", "something"); try { db.Insert("TABLE_NAME", myData); } catch(Exception ex) { MessageBox.Show(ex.Message); } </code></pre> <p>Edit: I just read that in SQLite it is not possible to rename a column, remove a column, or add or remove constraints from a table.... WTF? This is the solution to rename columns...</p> <p><a href="https://stackoverflow.com/questions/805363/how-do-i-rename-a-column-in-a-sqlite-database-table">How do I rename a column in a SQLite database table?</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload