首页 > 解决方案 > Python 和 MySQL 连接

问题描述

我知道如何连接 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)

标签: pythonmysqlpython-3.7mysql-8.0

解决方案


看看这个,我希望它是你需要的!

https://codetheory.in/how-to-write-a-python-script-which-communicates-with-the-mysql-server/

推荐阅读