欢迎光临散文网 会员登陆 & 注册

python3 + pymysql 创建数据库

2023-06-23 02:08 作者:青阳小栈  | 我要投稿


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



python3 + pymysql 创建数据库的评论 (共 条)

分享到微博请遵守国家法律