python3 + pymysql 创建数据库

发布时间:2023-06-23 01:56:35 来源: 哔哩哔哩

# 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)

标签:

Copyright   2015-2022 人人创新网版权所有  备案号:粤ICP备18023326号-36   联系邮箱:8557298@qq.com