文本框输入sql语句,然后点按钮弹出框显示查询的集合
时间: 2023-04-01 21:00:17 浏览: 62
你可以使用数据库连接库,如MySQLdb或pymysql,在Python中编写代码来实现这个功能。具体步骤如下:
1. 导入数据库连接库和tkinter库。
2. 创建一个GUI窗口,包括一个文本框和一个按钮。
3. 在按钮的回调函数中,获取文本框中输入的SQL语句。
4. 使用数据库连接库连接到数据库,并执行SQL语句。
5. 将查询结果转换为字符串,并在弹出框中显示。
以下是示例代码:
```python
import tkinter as tk
import pymysql
def query():
# 获取SQL语句
sql = text.get("1.", "end-1c")
# 连接数据库
conn = pymysql.connect(host="localhost", user="root", password="password", database="test")
cursor = conn.cursor()
# 执行SQL语句
cursor.execute(sql)
result = cursor.fetchall()
# 转换为字符串
result_str = ""
for row in result:
result_str += str(row) + "\n"
# 显示结果
tk.messagebox.showinfo("查询结果", result_str)
# 关闭数据库连接
cursor.close()
conn.close()
# 创建GUI窗口
root = tk.Tk()
root.title("SQL查询工具")
# 创建文本框和按钮
text = tk.Text(root, width=50, height=10)
text.pack()
button = tk.Button(root, text="查询", command=query)
button.pack()
# 运行窗口
root.mainloop()
```