Python 和 MySQL 连接

Python and MySQL connection

我知道如何连接 python 脚本和 MySQL 数据库,但是当数据库不是 运行ning 时我无法做到,所以我需要 运行 首先是我的数据库。 主要问题是如何从 python 脚本 运行 我的数据库?通常我从 MySQL Workbench 运行 宁它,但我希望它 运行 例如用我的 init 方法GUI 的 class。 我正在使用 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/

MySQL服务器可以这样运行:

import os

os.chdir("C:/")
os.system("net start mysql80") #running service mysql[version] (my version is 8.0)