Sqlite3_last_insert_rowid() is unsafe in a multithreaded environment (and documented as such on SQLite) UpdateMessagesRecipients() //method not shown MessagesRecipients.AddMessages_RecipientsRow(row) Var row = messagesRecipients.NewMessages_RecipientsRow() reference to the new row in the Messages table, but it does not.įoreach (var recipient in message.Recipients) Var x = messagerow //I hoped the messagerow would hold a Messagerow=messages.AddMessagesRow(message.Sender, Pm_databaseDataSet.MessagesRow messagerow My Persistence code (messages and messagesRecipients are DataTables): public void Persist(Message message) But how do I call this statement from ADO.Net? But how can I hold a reference to the MessageRow I just added in order to retrieve this new MessageID? I can always search the database for the last row added of course, but that could possibly return a different row in a multithreaded environment?ĮDIT: As I understand it for SQLite you can use the SELECT last_insert_rowid(). The ID of a new message will be created after it has been stored in the database. This table represents the many to many relation between recipients and messages. I created a table Messages_Recipients with two columns: MessageID (referring to the ID column of the Messages table and Recipient (referring to the username column in the Users table). I have a table Users with columns username (primary key, text) and Hash.Ī message is sent by one Sender (user) to many recipients (user) and a recipient (user) can have many messages. commit() to the connection object.I have a table Messages with columns ID (primary key, autoincrement) and Content (text). To actually save the changes you need to call. When creating tables, adding rows and modifying values, the table is in a temporary state. This means after each entry is put into the table, each is assigned a different id being 1, 2, 3, 4, ect. This means I don't have to worry about making different id's for each entry as the library will take care of it for me. ![]() One feature of SQLite is that if you have an integer as a primary key in a table, it will auto-increment based off the last entry if you do not assign. format or adding strings together using '+' but this method is much safer. ![]() It is possible to just add your values by using. We use this to stop SQL injections and error happening from dynamic data being added to queries. In my example above, the first item in the tuple would be put in place of the first question mark, this will also happen for the second item and second question mark. ![]() execute() can take two arguments, the query and a tuple of data to be put into the query. When you are passing external data into your database that you can not guarantee is 100% safe (don't assume), it is best practice to pass them in with methods provided by the sqlite3 library. These are placeholders for external data. In the code above, I used two question marks in the values. execute() to execute all your SQL commands and queries like creating a table, adding rows, modifying entries and even selecting data (will be described soon). This above example will create the table I have instructed. execute ( 'INSERT INTO url(url, count) VALUES(?, ?)', ( url, 0 )) execute ( 'CREATE TABLE url (id INTEGER PRIMARY KEY, url TEXT, count INTEGER) ' ) cursor.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |