MySQL连接

xeonds

2023-08-06 17:56:09

mysql -h 192.168.1.100 -P 3306 -u root -p

python

您好,这是Bing。我可以帮您了解如何使用Python连接MySQL数据库。😊

使用Python连接MySQL数据库的方法有以下几个步骤:

pip install PyMySQL

或者

pip install mysql-connector-python

import pymysql db = pymysql.connect(host='localhost', user='root', password='123456', port=3306, database='test')

或者

import mysql.connector db = mysql.connector.connect(host='localhost', user='root', password='123456', port=3306, database='test')

cursor = db.cursor()

sql = "SELECT * FROM student" cursor.execute(sql) data = cursor.fetchall() print(data)

sql = "INSERT INTO student VALUES ('Tom', 'Lee', 18, 'M', 3000)" cursor.execute(sql) db.commit() cursor.close() db.close()