在不开启MySQL服务的情况下,无法连接本地MySQL数据库。MySQL服务是必须启动的,才能通过Python连接到本地MySQL数据库。以下是Python连接本地MySQL的示例代码:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="username",
  password="password",
  database="database_name"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM table_name")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

请确保已经安装mysql-connector-python库,可以使用以下命令进行安装:

pip install mysql-connector-python
在不开启mysql服务的情况下python怎么连接本地mysql

原文地址: https://www.cveoy.top/t/topic/eE01 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录