我知道如何连接python脚本和MySQL数据库,但是当数据库没有运行时我无法做到这一点,所以我需要首先运行我的数据库。 主要问题是如何从 python 脚本运行我的数据库?通常我从 MySQL Workbench 运行它,但我希望它使用 GUI 类的 init 方法运行。 我正在使用 mysql.connector 模块进行连接。
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "12121212",
database = 'mydb',
auth_plugin = "mysql_native_password"
)
cursor = mydb.cursor()
cursor.execute("select * from tab")
results = cursor.fetchall()
print(results)
最佳答案
看看这个,我希望它是您所需要的!
https://codetheory.in/how-to-write-a-python-script-which-communicates-with-the-mysql-server/
关于Python 和 MySQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58474498/