我在Python语言中使用MySQL的update语句有困难,我已经创建了一个用于执行查询的类,我所有的其他查询都可以工作,但更新我需要一双新的眼睛。
database_handler.sql_post_query_with_args_performed_successfully("UPDATE members SET email ='test@test.ie' WHERE id= '%s'",id) 函数sql_post_query_with_args_performed_successfully在执行查询时返回true。按照原样,此代码返回True,但不更新表代码的字段,如下所示: def sql_post_query_with_args_performed_successfully(self,query,args):使用UseDatabase(self.MYSQL_DETAILS)作为游标: try: cursor.execute(query,args) return true except mysql.connector.errors.IntegrityError: return False
这个函数使用了"UseDatabase“另一个类来处理连接和查询的执行,这里的id是代码:
import mysql.connector
class UseDatabase:
def __init__(self, configuration:dict):
self.config = configuration
def __enter__(self) -> 'cursor':
"""Connect to database and create a DB cursor.
Return the database cursor to the context manager.
"""
self.conn = mysql.connector.connect(**self.config)
self.cursor = self.conn.cursor()
return self.cursor
def __exit__(self, exc_type, exc_value, exc_traceback):
self.cursor.close()
self.conn.commit()
self.conn.close()发布于 2015-11-26 18:43:52
在进一步挖掘之后,我意识到我的sql语句需要是:"UPDATE members SET verified =1 WHERE id = %s“
我的函数调用需要在元组中传递id,如下所示:测试成员SET email =‘sql_post_query_with_args_performed_successfully("UPDATE @test.ie’WHERE id= %s",(id,))
发布于 2015-11-22 00:31:26
经过反复试验,我在这里找到了答案:Python mysql.connector.errors. %s passed to SQL query with quotes
该语句是:"UPDATE members SET email=‘id=@test.ie’WHERE test‘{0}’“.format(Id))
https://stackoverflow.com/questions/33845016
复制相似问题