sql update syntax
How do you do an update statement?
I would like to update UserID with this string theUserid and I would like to update picturepath with fileuploadpaths(string) in the same table (Pictures)
OdbcCommand cmd = new OdbcCommand("UPDATE Pictures
SET ('" + theUserId + "','" + fileuploadpaths + "')
WHERE (UserID, picturepath)", cn);
Not sure if its
UPDATE Pictures
SET UserID = "+ theUserid +"
picturepath="+ fileuploadpaths +"
Where UserID = theUserid
picturepath = something already in my db?
EDIT:
Ive tryed this:
OdbcCommand cmd = new OdbcCommand("UPDATE Pictures SET picturepath ='" + fileuploadpaths + "' WHERE UserId = '" + theUserId + "')", cn);
but I get the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the rig开发者_开发技巧ht syntax to use near ')' at line 1
Use:
OdbcCommand cmd = new OdbcCommand("UPDATE PICTURES
SET userid = ?,
picturepath = ?
WHERE userid = ?");
cmd.Parameters.AddWithValue("@theuser", theUserid);
cmd.Parameters.AddWithValue("@picpath", fileuploadpaths);
cmd.Parameters.AddWithValue("@userid", userid);
try
{
myConnection.executeNonQuery(cmd);
}
catch (Exception e)
{
Console.Write("Update failed: "+e.Message);
}
Though the parameters have names, they are bind variables. Meaning the value specified needs to be in the position order of the "?" placeholders, starting from the first character of the query string ("Update...").
Reference
- http://www.daniweb.com/software-development/csharp/threads/235966
- http://www.java2s.com/Code/CSharp/Database-ADO.net/PassparameterstoOdbcCommand.htm
You probably want something like this:
Update Pictures set picturepath = 'SomePath' where UserId = 'someUserId'
You are just querying by the user ID, correct? In that case, it doesn't need to be included in your UPDATE
clause, and the picturepath
doesn't need to be in the WHERE
clause.
UPDATE Pictures
SET picturepath = '/somewhere/picture.png'
Where UserID = 12345
See the MySQL UPDATE
docs for more.
精彩评论