Monotouch’s mono.data.sqlite: How to get the value of the auto increment field after inserting a line?

Well, this may be very basic stuff, but it took me a long time to figure it out. I think there are more .NET programmers like me, Monotouch and SQLite Newbies don’t know this.

I use Ado.NET (System.Data) with Monotouch and SQLite. In SQLite, each row of each table has a name called The 64-bit signed integer of ROWID. You can use it, or if you want, you can use INTEGER PRIMARY KEY AUTOINCREMENT to specify a field, and SQLite will link to ROWID.

But, how to retrieve after inserting a new record The value of this field? Like the @@identity keyword in Sql Server?

Searching I found that the iOS library of SQLite has a method sqlite3_last_insert_rowid() to retrieve it, but there is no corresponding in Mono.Data.Sqlite. In the older implementation (Mono.Data.SqliteClient) there is A LastInsertRowID() method, but the method disappeared in Mono.Data.Sqlite. Why?

SQLite has some internal core functions. One of the functions is last_insert_rowid(). Therefore, all you have to do is Is to issue the command “SELECT last_insert_rowid()”. Examples in Monotouch:

public long GetLastInsertRowId(SqliteConnection connection)
{
/ / Assuming connection is an open connection from your INSERT
using (SqliteCommand command = new SqliteCommand("SELECT last_insert_rowid()", connection))
{
return (long)command.ExecuteScalar() ;
}
}

Or you can combine selection and insert, for example:

string SqlCommand = "INSERT into Customers ([Name], .... [City]) VALUES (@Name, ... @City);SELECT last_insert_rowid();";

Okay, This may be very basic stuff, but it took me a long time to figure it out. I think there are more .NET programmers like me, novices to Monotouch and SQLite who don’t know this.

I use Ado.NET (System.Data) with Monotouch and SQLite. In SQLite, each row of each table has a 64-bit signed integer called ROWID. You can use it, or If you want, you can use INTEGER PRIMARY KEY AUTOINCREMENT to specify a field, and SQLite will link to ROWID.

However, how to insert a new record After retrieving the value of this field? Like the @@identity keyword in Sql Server?

Searching I found that the iOS library of SQLite has a method sqlite3_last_insert_rowid() to retrieve it, but there is no corresponding in Mono.Data.Sqlite. In the older implementation (Mono.Data.SqliteClient) there is A LastInsertRowID() method, but the method disappeared in Mono.Data.Sqlite. Why?

SQLite has some internal core functions. One of the functions is last_insert_rowid(). Therefore, all you have to do is issue the command “SELECT last_insert_rowid()”. Monotouch Examples in:

public long GetLastInsertRowId(SqliteConnection connection)
{
// Assuming connection is an open connection from your INSERT
using (SqliteCommand command = new SqliteCommand("SELECT last_insert_rowid()", connection))
{
return (long)command.ExecuteScalar();
}
}< /pre>

Or you can combine selection and insert, for example:

string SqlCommand = "INSERT into Customers ([Name], .... [City] ) VALUES (@Name, ... @City);SELECT last_insert_rowid();";

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 868 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.