SQL更新语法

发布于 2024-10-27 10:10:30 字数 883 浏览 3 评论 0原文

你如何做更新声明?

我想用这个字符串theUserid更新UserID,我想用同一个表(图片)中的fileuploadpaths(字符串)更新图片路径

OdbcCommand cmd = new OdbcCommand("UPDATE Pictures 
                                      SET ('" + theUserId + "','" + fileuploadpaths + "')                                  
                                    WHERE (UserID, picturepath)", cn);

不确定它是否

UPDATE Pictures 
   SET UserID = "+ theUserid +" 
       picturepath="+ fileuploadpaths +" 
 Where UserID = theUserid 
       picturepath = something already in my db?

编辑:

我尝试过这个:

OdbcCommand cmd = new OdbcCommand("UPDATE Pictures SET picturepath ='" + fileuploadpaths + "' WHERE UserId = '" + theUserId + "')", cn);

但我得到了错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 ')' 附近使用的正确语法

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 right syntax to use near ')' at line 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻了热茶 2024-11-03 10:10:30

用途:

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);
}

虽然参数有名称,但它们是绑定变量。这意味着指定的值需要按照“?”的位置顺序排列。占位符,从查询字符串的第一个字符开始(“更新...”)。

参考

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

权谋诡计 2024-11-03 10:10:30

你可能想要这样的东西:

Update Pictures set picturepath = 'SomePath' where UserId = 'someUserId'

You probably want something like this:

Update Pictures set picturepath = 'SomePath' where UserId = 'someUserId'
心的憧憬 2024-11-03 10:10:30

您只是通过用户 ID 进行查询,对吗?在这种情况下,它不需要包含在 UPDATE 子句中,并且 picturepath 不需要位于 WHERE 中条款。

UPDATE Pictures 
   SET picturepath = '/somewhere/picture.png'
 Where UserID = 12345

请参阅 MySQL UPDATE 文档更多的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文