立即学习:https://edu.csdn.net/course/play/26755/340155?utm_source=blogtoedu
题一:如何创建MySQL数据库
pymysql
pip install pymysql
from pymysql import *
def connectDB():
db = connect('127.0.0.1','root','1234567','meituan',charset='utf-8')
return db
print(type(db))
def createTable(db):
cursor = db.cursor()
c = db.cursor()
try:
c.execute('''create table persons
(id int primary key not null,
name text not null,
age int not null,
address char(100),
salary real);''')
db.commit()
except:
db.rollback()
return False
if createTable(db):
print('create table sucess')
else:
print('create table failed')
题二:如何向MySQL表中插入数据库
def insertRecords(db):
cursor = db.cursor()
try:
except Exception as e:
print(e)
db.rollback()
题三:如何查询MySQL的数据