# -*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding("utf-8") import MySQLdb import random # 1.连接数据库 connect = MySQLdb.connect( host = 'localhost',# 数据库地址 port = 3306, # 端口号 usrt = 'root', # 用户名 passwd = '123456', # 密码 charset = 'utf8', # 编码 use_ubicode = True, db = 'student_test' # 数据库名称 ) # 2.创建游标 cursor = connect.cursor()# 3.准备sql# primary key 主键 # unique唯一 # auto_increment 自增 # not null 非空sql = 'create table 表名(id int promary key not null auto_increment,name varchar(10),age int)'# 执行sql cursor.execute(sql)connect.commit()# 提交
# 关闭 cursor.close() connect.close()