使用Python连接MySQL数据库

2018-11-16  本文已影响0人  龙小江i

导入pymysql库

import pymysql

创建data数据库

db = pymysql.connect(host = '127.0.0.1', user = 'root', password = 'qqqqqq', port = 3306)
cursor = db.cursor()
cursor.execute('create database data default character set utf8mb4')
db.close()

创建data数据表

db = pymysql.connect(host = '127.0.0.1', user = 'root', password = 'qqqqqq', port = 3306, db = 'data')
cursor = db.cursor()
cursor.execute('create table data(name varchar(4), age int(3) not null, school varchar(10) not null, primary key (name))')
db.close()
上一篇下一篇

猜你喜欢

热点阅读