![]() Now get the ID of the last row inserted: Command.CommandText = "select last_insert_rowid()" Insert a row of data into the table: // Reuse the command object and insert a row into the table.Ĭommand.CommandText = "INSERT INTO Customers (UNIQUEID, ADDRESS) VALUES = Ĭ(new "Fred Bloggs")) Ĭ(new "Acacia Avenue")) SQLiteCommand Command = new SQLiteCommand(SQL, Conn) ![]() String SQL = "CREATE TABLE Customers (ID INTEGER PRIMARY KEY, UNIQUEID VARCHAR(30), ADDRESS VARCHAR(100))" SQLiteConnection Conn = new SQLiteConnection("Data Source = " + mPathName) Ĭreate a table (Customers) in the SQLite database: // Create a table. Here is an example in C# showing how to get the ID of the last row inserted into a table in a database.Ĭreate the SQLite database and open a connection to it: String mPathName = Path.Combine(Environment.GetFolderPath(), "testdba.db3") SQLite has a special SQL function – last_insert_rowid() – that returns the ID of the last row inserted into the database so getting the ID of a new row after performing a SQL insert just involves executing the last_insert_rowid() command. add a new customer into the Customers table taking the example above, then you will need to find out the ID of the new row so you can write it back into the Customer object you’ve just added. ![]() When you insert a new row in the table, e.g. In SQLite, if you have a field of type INTEGER PRIMARY KEY in a table, the database engine will automatically fill the field with the ID for the row. For example, if you have a Customers table in a database then if you populate a list of Customer objects in your application from the rows in the table, storing the row ID for each object lets you easily update the correct row if you edit the values in one of the Customer objects. When working with SQL databases it can often be useful to keep a record of the ID of the row in a table from which a piece of data was read. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |