python3.4怎么连接mysql pymysql连接python 连mysql数据库库

python3使用PyMysql连接mysql数据库实例
字体:[ ] 类型:转载 时间:
本篇文章主要介绍了python3使用PyMysql连接mysql数据库实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
python语言的3.x完全不向前兼容,导致我们在python2.x中可以正常使用的库,到了python3就用不了了.比如说mysqldb
目前MySQLdb并不支持python3.x , Python3.x连接MySQL的方案有:oursql, PyMySQL, myconnpy 等。
下面来说下python3如何安装和使用pymysql,另外两个方案我会在以后再讲。
1.pymysql安装
pymysql就是作为python3环境下mysqldb的替代物,进入命令行,使用pip安装pymysql
pip install pymysql3
2.pymysql使用
如果想使用mysqldb的方式,那么直接在py文件的开头加入如下两行代码即可。
#引入pymysql
import pymysql
#当成是mysqldb一样使用,当然也可以不写这句,那就按照pymysql的方式
pymysql.install_as_MySQLdb()
3. pymysql查询示例
__author__ = ''
#导入pymysql的包
import pymysql
#获取一个数据库连接,注意如果是UTF-8类型的,需要制定数据库
conn=pymysql.connect(host='localhost',user='pythontab',passwd='pythontab',db='pythontab',port=3306,charset='utf8')
cur=conn.cursor()#获取一个游标
cur.execute('select * from user')
data=cur.fetchall()
for d in data :
#注意int类型需要使用str函数转义
print("ID: "+str(d[0])+' 用户名: '+d[1]+" 注册时间: "+d[2])
cur.close()#关闭游标
conn.close()#释放数据库资源
except Exception :print("查询失败")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具推荐这篇日记的豆列
······1059人阅读
感悟(61)
Python(23)
由于一些书籍和博客讲解的方法不正确,在此给出正确的连接方法。我的环境是:
Ubuntu 14.04
Python 3.4.0
sudo pip3 install PyMySQL
import pymysql
conn = pymysql.connect(host='127.0.0.1',port=3306,
user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("USE scraping")
cur.execute("SELECT * FROM pages WHERE id=1")
print(cur.fetchone())
cur.close()
conn.close()
正确连接后,出现的截图:
书中《web scraping with Python collecting data from the modern web》在建立连接的过程中使用unix_socket='/tmp/mysql.sock',这样做在我的电脑环境下是错误的,只需将它改为port=3306即可。我们看书的时候,不要太相信书。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:184803次
积分:3341
积分:3341
排名:第8333名
原创:160篇
转载:27篇
评论:30条
(1)(2)(1)(1)(3)(8)(4)(1)(44)(2)(2)(28)(14)(16)(7)(2)(7)(3)(2)(12)(20)(12)Python 3.4.0 with MySQL database - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
J it only takes a minute:
I have installed Python version 3.4.0 and I would like to do a project with MySQL database. I downloaded and tried installing MySQLdb, but it wasn't successful for this version of Python. Any suggestions how could I fix this problem and install it properly?
10.8k33150
MySQLdb does not support Python 3 but it is not the only MySQL driver for Python.
is essentially just a fork of MySQLdb with Python 3 support merged in (and a few other improvements).
is a pure python MySQL driver, which means it is slower, but it does not require a compiled C component or MySQL libraries and header files to be installed on client machines. It has Python 3 support.
Another option is simply to use another database system like .
13.6k23351
Use . I prefer to install it with pip from :
pip install --allow-external mysql-connector-python mysql-connector-python
Have a look at its
If you are going to use pooling make sure your database has enough connections available
as the default settings may not be enough.
10.8k33150
Install pip:
apt-get install pip
For acess MySQL from Python, install:
pip3 install mysqlclient
There is a Ubuntu solution available either through the Ubuntu Software Center or through the Synaptic Package Manager. This will connect Python version 3.4.0
to MySQL. Download "python3-mysql.connector" version 1.1.6-1.
Note that the connection syntax does not use "MySQLdb".
Instead read:
I solved it this way:
download the zipped package from
and follow this set of instructions:
/path/to/downloads/folder/mysql-connector-python-VER.zip
In case u got a .gz u can use ->
tar xzf mysql-connector-python-VER.tar.gz
cd mysql-connector-python-VER
sudo python3 setup.py install # NOTICE I USED PYTHON3 INSTEAD OF PYTHON
You can read about it
is a fork of MySQLdb and can serve as a drop-in replacement with Python 3.4 support. If you have trouble building it on Windows, you can download it from
To connect Python with extern MySQL database I've followed:
1. Installation
For connector download You have to choose: Platform independent
Python program
Alternatively, you can use
or . For oursql, use the oursql py3k series as my link points to.
It seems that at the moment Ubuntu 15.10 has a but with python3 and pip3.
As elaborated in .
The problem makes pip3 install to python3.5 while python3 is actually running python3.4 :(
Until a proper solution will be available via the updates you can do one of the following:
python3 -m pip install pymysql
instead of
pip3 install pymysql
(or any other package)
import pymysql
should work in python3 and in idle3.
Alternatively, if you explicitly need 3.5 you can use explicit python3.5 instead of python3. but idle3 will still point to 3.4...
Maybe you can use a work around and try something like:
import datetime
#import mysql
import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1',user = 'someUser', passwd = 'foobar',db = 'foobardb')
cursor = conn.cursor()
3,30021940
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
rev .25296
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 python mysql pymysql 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信