问题
I want to use query parameters in my update sql, and had this code:
internal static int UpdatePhotosetName(string oldPhotosetName, string newPhotosetName)
{
String qryUpdateBaseTable = "UPDATE PhotraxBaseData SET photosetName = @newName WHERE photosetName = @oldName";
int rowsUpdated;
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
SQLiteCommand cmd = new SQLiteCommand(db);
cmd.CommandText = qryUpdateBaseTable;
cmd.Parameters.Add(new SQLiteParameter("@newName"));
cmd.Parameters.Add(new SQLiteParameter("@oldName"));
Command.Parameters["@newName"].Value = newPhotosetName;
Command.Parameters["@oldName"].Value = oldPhotosetName;
rowsUpdated = cmd.ExecuteNonQuery();
db.Close();
}
return rowsUpdated;
}
...but all of the following were flagged as being unavailable/nonexistent in the current context:
Parameters
SQLiteParameter
Command
I do have Sqlite-Net installed (SQLite.cs and SQLiteAsync.cs), and version 3.8.7.1 of "SQLite for Windows Runtime (Windows 8.1)"
I based this code on what I found here, but apparently the WinRT way of doing this differs significantly from the more staid way.
How can I utilize query parameters in a WinRT app?
UPDATE
This is not as sexy as some fancy-pants LINQ, I reckon, but it does work:
internal static int UpdatePhotosetName(string oldPhotosetName, string newPhotosetName)
{
String qryUpdateBaseTable = String.Format(
@"UPDATE PhotraxBaseData SET photosetName = '{0}' WHERE photosetName = '{1}'",
newPhotosetName, oldPhotosetName);
int rowsUpdated;
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
SQLiteCommand cmd = new SQLiteCommand(db);
cmd.CommandText = qryUpdateBaseTable;
rowsUpdated = cmd.ExecuteNonQuery();
}
return rowsUpdated;
}
That's okay; I'm too sexy for my compiler already, anyway.
UPDATE 2
I guess one can never really be too sexy; I'm going with Thomas Levesque's ultra-trim look, after all:
internal static int UpdatePhotosetName(string oldPhotosetName, string newPhotosetName)
{
String qryUpdateBaseTable = "UPDATE PhotraxBaseData SET photosetName = ? WHERE photosetName = ?";
int rowsUpdated;
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
rowsUpdated = db.Execute(qryUpdateBaseTable, newPhotosetName, oldPhotosetName); // look, ma: no superfluous code!
}
return rowsUpdated;
}
回答1:
db.Execute("UPDATE PhotraxBaseData SET photosetName = ? WHERE photosetName = ?", newName, oldName);
来源:https://stackoverflow.com/questions/26685223/how-can-i-use-sqlite-query-parameters-in-a-winrt-app