开发者

sql update syntax

开发者 https://www.devze.com 2023-02-19 16:27 出处:网络
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)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号