注意:password與passwd都可以使用,da_name不需要在兩邊加‘ ’符號,會自動加上
# python + pymysql 創建數據庫
import pymysql
# 創建連接
conn = pymysql.connect(host='localhost',user='root',password='123456',charset='utf8mb4')
# 創建游標
cursor = conn.cursor()
# 創建數據庫的sql(如果數據庫存在就不創建,防止異常)
sql = "CREATE DATABASE IF NOT EXISTS db_name"
# 執行創建數據庫的sql
cursor.execute(sql)
# 創建表
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)