# 安装MySQL pip install mysql-connector-python
# 创建MySQL数据库 mysql -u root -p CREATE DATABASE mydb;
# 连接MySQL数据库 import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="password", database="mydb" ) mycursor = mydb.cursor()
# 创建MySQL表 mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
# 添加MySQL表数据 sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = ("John", "Highway 21") mycursor.execute(sql, val) mydb.commit()
# 查询MySQL表数据 mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x)
本文链接:http://task.lmcjl.com/news/1781.html