python3 + pymysql 創(chuàng)建數(shù)據(jù)庫
# python + pymysql 創(chuàng)建數(shù)據(jù)庫?
import pymysql
# 創(chuàng)建連接
conn = pymysql.connect(host='localhost',user='root',password='123456',charset='utf8mb4')
# 創(chuàng)建游標(biāo)
cursor = conn.cursor()
?
# 創(chuàng)建數(shù)據(jù)庫的sql(如果數(shù)據(jù)庫存在就不創(chuàng)建,防止異常)
sql = "CREATE DATABASE IF NOT EXISTS db_name"?
# 執(zhí)行創(chuàng)建數(shù)據(jù)庫的sql
cursor.execute(sql)
?
?
# 創(chuàng)建表
sql_2 = '''CREATE TABLE `employee` (
? `id` INT NOT NULL AUTO_INCREMENT,
? `topic` INT ,
? `ptid` INT NOT NULL,
? `level` INT NOT NULL,
? `time` TIME,
? `consume` INT NOT NULL,
? `err` INT NOT NULL,
? `points` INT NOT NULL,
? `gid` INT NOT NULL,
? PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
'''
cursor.execute(sql_2)
標(biāo)簽: