作者:公民不是百姓2 | 来源:互联网 | 2023-01-19 20:02
我正在使用python 3.6
,我PyMySQL
用来连接mysql.
我将创建几个数据库.我想编写一个Python脚本来轻松创建和删除它们.
在PyMySQL文档中有一个例子,其中包含一段代码:
PyMySQL docs:https://pymysql.readthedocs.io/en/latest/user/examples.html
cOnnection= pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
在上面的代码中,数据库已经存在.
现在mysql中没有数据库.我想通过python和PyMySQL创建它们.我该怎么办?
1> Thomas Munk..:
请忽略db:
cOnn= pymysql.connect(host='localhost',
user='user',
password='passwd')
然后获取一个游标并创建一个数据库:
conn.cursor().execute('create database dbname')
在新数据库中创建一个表:
conn.cursor().execute('create table dbname.tablename (...)')