Python3链接MySQL数据库
Python 2.x 上连接MySQL的库倒是不少的,其中比较著名就是MySQLdb(Django项目都使用它;我也在开发测试系统时也使用过),见:http://sourceforge.net/projects/mysql-python/
不过,目前MySQLdb并不支持python3.x,网上找了一些方法,后来我还是偶然发现MySQL官方已经提供了MySQL连接器,而且已经有支持Python3.x的版本了。MySQL Connector/Python, a self-contained Python driver for communicating with MySQL servers. 这个用起来还是感觉比较顺手的。
关于MySQL Connector/Python的各种介绍、安装、API等文档,还是参考官网吧:http://dev.mysql.com/doc/connector-python/en/index.html
(注意:安装程序将关于MySQL Connnector的python2的源文件复制到了python3库的位置(运行时会报语法错误),我就直接手动复制了其中python3/目录下的文件过去就解决。)
另外,Python3.x连接MySQL的其他方案有:oursql, PyMySQL, myconnpy 等,参考如下链接:
http://packages.python.org/oursql/
https://github.com/petehunt/PyMySQL/
https://launchpad.net/myconnpy
下面只是贴一个试用 MySQL Connector/Python 的Python脚本吧(包括创建表、插入数据、从文件读取并插入数据、查询数据等):
1 |
#!/usr/bin/python3 |
另外,最后再贴一个使用MySQLdb的python2.x代码示例吧:
1 |
#!/usr/bin/python2.7 |
最新文章
- Struts 2之动态方法调用,不会的赶紧来
- paip.花生壳 服务启动失败 以及不能安装服务,权限失败的解决
- ubuntu系统从中文环境改成英文环境
- windows下面go语言环境搭建
- 【SpringMVC】SpringMVC系列14之SpringMVC国际化
- LeetCode Combinations (DFS)
- ruby学习--block
- windows下配置环境变量时,在cmd窗口执行配置的命令时无效的原因
- spark-shell 执行脚本并传入参数
- 如何使用json在前后台进行数据传输
- FileUtils.copyDirectory without .SVN
- Protel99se轻松入门:特殊技巧和高级设置(一)
- js中访问对象的方法
- STM32的外部中断配置及使用
- Spring Boot 注解的使用
- UWP 手绘视频创作工具技术分享系列 - Ink &; Surface Dial
- eclipse中如何同期化
- RedHat/Fedora/Centos 下bash 自动补全命令
- codeforces-1132 (div2)
- mongodb怎么创建数据库和配置用户