1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python创建mysql数据库_Python:如何使用PyMySQL创建mysql数据库?

python创建mysql数据库_Python:如何使用PyMySQL创建mysql数据库?

时间:2021-12-24 12:46:11

相关推荐

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/0717/1486750.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。