【问题标题】:Python and MySQL connectionPython 和 MySQL 连接
【发布时间】:2020-02-16 20:19:08
【问题描述】:

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

【问题讨论】:

标签: python mysql python-3.7 mysql-8.0


【解决方案1】:

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

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

【讨论】:

  • 不是真的,我与正在运行的 MySQL 服务器的连接没有问题,但我想知道如何从 python 脚本运行这个服务器。
  • 你可以使用os模块运行sql server
【解决方案2】:

MySQL服务器可以这样运行:

import os

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

【讨论】:

    猜你喜欢
    • 2019-06-15
    • 2013-02-20
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2016-10-08
    • 2021-07-12
    相关资源
    最近更新 更多