Windows Phone 8 SQLite Question
-
How do I add method for last_insert_rowid I added a record to the table. As soon as it is entered I want to get the rowed that SQLite gave to it. How can I accomplish this?
-
How do I add method for last_insert_rowid I added a record to the table. As soon as it is entered I want to get the rowed that SQLite gave to it. How can I accomplish this?
You should be able to use last_insert_rowid for the last row id that was inserted for that connection. Basically, inside the same connection, issue a separate select command:
SELECT last_insert_rowid
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
You should be able to use last_insert_rowid for the last row id that was inserted for that connection. Basically, inside the same connection, issue a separate select command:
SELECT last_insert_rowid
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierHi Pete, I tried this and it wont let me compile. I don't know the correct syntax. There is not much out there for WP8 SQLite
string query = "select last_insert_rowid() as id from myTable";
using (var db = new SQLite.SQLiteConnection(System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, "SM.db"), true))
{SQLite.SQLiteCommand cmd = new SQLite.SQLiteCommand(db);
var result = cmd.ExecuteNonQuery();
} -
Hi Pete, I tried this and it wont let me compile. I don't know the correct syntax. There is not much out there for WP8 SQLite
string query = "select last_insert_rowid() as id from myTable";
using (var db = new SQLite.SQLiteConnection(System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, "SM.db"), true))
{SQLite.SQLiteCommand cmd = new SQLite.SQLiteCommand(db);
var result = cmd.ExecuteNonQuery();
}No. Only select last_insert_rowid();. You don't need the rest. There's a reaaon I didn't include it in my answer there.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
No. Only select last_insert_rowid();. You don't need the rest. There's a reaaon I didn't include it in my answer there.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierI tried that and I get this error
SQLite.SQLiteException: Misuse
-
No. Only select last_insert_rowid();. You don't need the rest. There's a reaaon I didn't include it in my answer there.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierI tried this also and the error is "Row" SQLite.SQLiteCommand cmd = new SQLite.SQLiteCommand(db); cmd.CommandText = query.ToString(); var result2 = cmd.ExecuteNonQuery();
-
I tried this also and the error is "Row" SQLite.SQLiteCommand cmd = new SQLite.SQLiteCommand(db); cmd.CommandText = query.ToString(); var result2 = cmd.ExecuteNonQuery();
If you're trying to retrieve the record, use
ExecuteScalar
, notExecuteNonQuery
.I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
If you're trying to retrieve the record, use
ExecuteScalar
, notExecuteNonQuery
.I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierOk, So what is the syntax for that? cmd.ExecuteScalar< ? >; Sorry, I just don't know SQLite
-
Ok, So what is the syntax for that? cmd.ExecuteScalar< ? >; Sorry, I just don't know SQLite
It should just be something like:
private int GetLastRowId(SQLLite.SQLLiteConnection connection)
{
SQLLite.SQLLiteCommand cmd = new (connection);
cmd.CommandText = "SELECT last_insert_rowid";
object rowId = cmd.ExecuteScalar();
return Convert.ToInt32(rowId);
}I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
It should just be something like:
private int GetLastRowId(SQLLite.SQLLiteConnection connection)
{
SQLLite.SQLLiteCommand cmd = new (connection);
cmd.CommandText = "SELECT last_insert_rowid";
object rowId = cmd.ExecuteScalar();
return Convert.ToInt32(rowId);
}I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierThanks Pete
-
Thanks Pete
No problem.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
Hi Pete, I tried this and it wont let me compile. I don't know the correct syntax. There is not much out there for WP8 SQLite
string query = "select last_insert_rowid() as id from myTable";
using (var db = new SQLite.SQLiteConnection(System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, "SM.db"), true))
{SQLite.SQLiteCommand cmd = new SQLite.SQLiteCommand(db);
var result = cmd.ExecuteNonQuery();
}Cursor cursor = getReadableDatabase().
rawQuery("select * from todo where _id = ?", new String[] { id });