天天看點

python建立mysql資料庫_Python:如何使用PyMySQL建立mysql資料庫?

我使用python 3.6,我使用PyMySQL连接mysql.

我将创建几个数据库.我想编写一个Python脚本来轻松创建和删除它们.

connection = pymysql.connect(host='localhost',

user='user',

password='passwd',

db='db',

charset='utf8mb4',

cursorclass=pymysql.cursors.DictCursor)

在上面的代码中,数据库已经存在.

现在mysql中没有数据库.我想通过python和PyMySQL创建它们.我该怎么办?

解决方法:

请忽略db:

conn = pymysql.connect(host='localhost',

user='user',

password='passwd')

然后获取一个游标并创建一个数据库:

conn.cursor().execute('create database dbname')

在新数据库中创建一个表:

conn.cursor().execute('create table dbname.tablename (...)')

标签:python,mysql

来源: https://codeday.me/bug/20190717/1486750.html