Python连接MySQL目前主要使用官方的python-mysql-connector和非官方的pymysql
import pymysql
connection = pymysql.connect(user='foo', password='foo', db='foo')
try:
with connection.cursor() as cursor:
cursor.execute("DROP TABLE IF EXISTS person")
cursor.execute("CREATE TABLE person(id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL)")
cursor.execute("INSERT INTO person(name) VALUES ('ant'), ('bee'), ('cat')")
cursor.execute("SELECT * FROM person")
print(cursor.fetchall())
connection.commit()
finally:
connection.close()
((1, 'ant'), (2, 'bee'), (3, 'cat'))
import mysql.connector
conn = mysql.connector.connect(user='foo', password='foo', db='foo')
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS person")
cursor.execute("CREATE TABLE person(id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL)")
cursor.execute("INSERT INTO person(name) VALUES ('ant'), ('bee'), ('cat')")
cursor.execute("SELECT * FROM person")
result = cursor.fetchall()
for item in result:
print(item)
(1, 'ant')
(2, 'bee')
(3, 'cat')