所以这段代码给了我错误
mycursor.execute("CREATE TABLE IF NOT EXISTS", str(share_name), "(nickname VARCHAR(255), date VARCHAR(255), bill VARCHAR(255), quantity VARCHAR(255), rate VARCHAR(255), amount(255))")
insert = f"INSERT INTO", str(share_name), "(nickname) VALUES (%s)"
val = str(share_nick)
mycursor.execute(insert, val)
p这就是错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0libtkinter__init__.py", line 1892, in __call__
return self.func(*args)
File "C:UsersShourya Kumar.DESKTOP-M2TQLD2Desktoppapa projectaddshare.py", line 36, in submit
mycursor.execute(insert, val)
File "C:UsersShourya Kumar.DESKTOP-M2TQLD2Desktoppapa projectvenvlibsite-packagesmysqlconnectorcursor.py", line 545, in execute
stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'
第36行是mycursor.execute(insert, val)
我刚刚给出了导致错误的代码,如果你们需要其余的代码,告诉我我会上传一个链接
首先,所有insert
变量都是您没有正确格式化的元组或f
字符串
insert = f"INSERT INTO {share_name} (nickname) VALUES (%s)"
mycursor.execute(insert, val)
我正在添加完整的代码,所以
share_name = 'tableName' # your table name
cursor.execute(f"CREATE TABLE IF NOT EXISTS {share_name} (nickname VARCHAR(255) , date VARCHAR(255), bill VARCHAR(255), quantity VARCHAR(255), rate VARCHAR(255), amount varchar(255))") #you don't have varchar in amount column
insert = f"INSERT INTO {share_name} (nickname) VALUES (%s)" #
cursor.execute(insert, (1,))
cnx.commit() #commit is necessary to perform real insert
cnx.close()