Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python mysql Update语句中将字符串变量和int变量放在一起_Python_Mysql_Variables - Fatal编程技术网

Python mysql Update语句中将字符串变量和int变量放在一起

Python mysql Update语句中将字符串变量和int变量放在一起,python,mysql,variables,Python,Mysql,Variables,我必须使用python在mysql Update语句中同时输入字符串变量和int变量 我的代码如下: z=input("enter name:") u=int(input("enter id")) sn='update table1 set colname:"%s" where colid=%s'%z cursor.execute(sn,(u,)) mydb.commit() 在我上面给出的代码中,colname应该获取字符串值表示用户名,colid应该获取int值表示数字中的id。但是在所有

我必须使用python在mysql Update语句中同时输入字符串变量和int变量

我的代码如下:

z=input("enter name:")
u=int(input("enter id"))
sn='update table1 set colname:"%s" where colid=%s'%z
cursor.execute(sn,(u,))
mydb.commit()
在我上面给出的代码中,colname应该获取字符串值表示用户名,colid应该获取int值表示数字中的id。但是在所有这些命令之后,我得到了错误:

错误:字符串的参数不足

我不明白为什么会出现错误,因为对于字符串变量,我们必须在mysql语句中使用%s,并在语句之后将其值赋给%z,正如我所给出的;对于where子句中的int变量,我们必须在括号中给出其变量,正如我上面所给出的

注意:当我用常量整数替换colid的%s并从execute语句中删除u时,代码将正确运行,没有任何错误。

将这两个变量都放在游标中。execute调用


您可以使用f字符串作为占位符。name=world那么fhello{name}会给你打个招呼world应该是colname=,而不是colname:哦,对不起,我错把它写在这里了,但在我的电脑上我只键入了“=”。但它也显示了错误。
sn='update table1 set colname = %s where colid=%s'
cursor.execute(sn, (z, u))