python-3.x - 如何从 mysql 数据库和树视图中删除 python ttk 树视图上的选定项目?
问题描述
嗨所以我正在尝试从树视图和数据库中删除选定的项目我处理了一些代码但它一直失败
这是代码
def delete():
selected_item = tree.selection()[0]
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database="loginsystem"
)
mycursor = mydb.cursor()
sql_select_Query = ("DELETE FROM data WHERE idData = '%s'")
mycursor.execute(sql_select_Query, selected_item)
mydb.commit()
tree.delete(selected_item)
print('delete')
def viewdata():
global screen7
global tree
screen7 = Toplevel(screen)
screen7.title("HOT or SUPER HOT(GUEST)")
screen7.geometry("800x450+550+220")
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database="loginsystem"
)
mycursor = mydb.cursor()
sql_select_Query = "select * from data"
mycursor.execute(sql_select_Query)
records = mycursor.fetchall()
height = 5
width = 2
tree = ttk.Treeview(screen7 ,style="mystyle.Treeview")
selected_items = tree.selection()
style = ttk.Style()
style.configure("mystyle.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11)) # Modify the font of the body
style.configure("mystyle.Treeview.Heading", font=('Calibri', 13, 'bold')) # Modify the font of the headings
style.layout("mystyle.Treeview", [('mystyle.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
tree["columns"] = ("one", "two")
tree.column("one", width=200)
tree.column("two", width=200)
tree.heading("#0", text='ID', anchor='center')
tree.column("#0", anchor="center")
tree.heading("one", text="Car")
tree.heading("two", text="Quantity")
tree.tag_configure('odd', background='#E8E8E8')
tree.tag_configure('even', background='#DFDFDF')
cpt = 0 # Counter representing the ID of your code.
for row in records:
# I suppose the first column of your table is ID
tree.insert('', 'end', text=str(cpt), values=(row[1], row[2]), tags =('ttk', 'simple'))
cpt += 1 # increment the ID
tree.place(x = 100, y = 130)
Label(screen7,text = "DATA" , bg = "black", width = w , height = h, font = ("Calibri", 20) , fg = "white").pack()
Button(screen7, text="Back", height=h, width="20", command=on_closemindta).place(x=500, y=380)
Button(screen7, text="Add", height=h, width="20", command=adddata).place(x=150, y=380)
Button(screen7, text="Delete", height=h, width="20", command=delete).place(x=325, y=380)
每次我运行它都会给我一个截断不正确的 DOUBLE 值的错误:'%s' 我真的不知道这意味着什么,我错过了什么吗?这是我单击删除按钮时打印的完整错误代码
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/lenovo/PycharmProjects/Pylog/App.py", line 420, in delete
mycursor.execute(sql_select_Query, selected_item)
File "C:\Users\lenovo\PycharmProjects\Pylog\venv\lib\site-packages\mysql\connector\cursor.py", line 569, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Users\lenovo\PycharmProjects\Pylog\venv\lib\site-packages\mysql\connector\connection.py", line 553, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Users\lenovo\PycharmProjects\Pylog\venv\lib\site-packages\mysql\connector\connection.py", line 442, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.DataError: 1292 (22007): Truncated incorrect DOUBLE value: '%s'
解决方案
这很正常,因为您为查询提供了一个字符串,而您的查询替换中需要一个整数
'%s' by %s
推荐阅读
- caching - 从纯粹缓存的角度来看,使用新的 Cache API 代替常规的 http 缓存有什么优势吗?
- shell - 如何使用 Shell 脚本创建并立即导航到目录?
- python - 处理列表并将输出写入 csv 文件
- android - 如何检测列表视图上任意位置的点击
- oracle - 无法连接到数据库(无法从此套接字读取更多数据)
- spring - 如何在 bootJar 中有条件地启用 launchScript()?
- mongodb - 用于排序查询的 MongoDB 单索引或复合索引?
- c - 从C中的通用(void *)列表中删除元素
- java - spring-boot微服务OAuth2TokenRelayFilter 500错误
- python - 在命令行中运行 python 脚本不会打印任何输出