热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《Python:如何使用PyMySQL创建mysql数据库?》经验,为你挑选了1个好方法。

我正在使用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 (...)')


推荐阅读
author-avatar
公民不是百姓2
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有