python连接mysql
python连接mysql需要有该语言对应的mhsql的驱动或扩展,目前有以下三种方式可以连接mysql:1.是c语言版的驱动,连接示例如下:#!/usr/bin/python# -*- coding: UTF-8 -*-import MySQLdb但是连接时会可能会报以下错误: File "/home/work/www/cgi-bin/test.py", line
python连接mysql需要有该语言对应的mhsql的驱动或扩展,目前有以下三种方式可以连接mysql:
1.MySQLdb
2.PyMySQL 的使用
2.mysql-connector-python:是 MySQL 官方的纯 Python 驱动;
我们就以第1种为例来讲解,它是c语言版的驱动,连接示例如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
但是连接时会可能会报以下错误:
File "/home/work/www/cgi-bin/test.py", line 4, in <module>
import MySQLdb
ImportError: No module named MySQLdb
这表明mysql-python没有按照,可以直接pip install mysql-python
如果是编译安装的mysql,那么这个时候可能因为找不到mysql的mysql_config文件而抛出以下错误:
EnvironmentError: mysql_config notfound
很明显,是安装该驱动时需要用的mysql的mysql_config的可执行文件,但是没有在相关环境变量中找到,那么怎么办? 只需要简单的把mysql_config软连接一下过来;
即:
如,我的mysql是编译安装的,并且安装在/home/work/app目录下的,所以直接执行:
ln -s /home/work/app/mysql/bin/mysql_config /usr/local/bin/mysql_config
这个时候再安装 pip install mysql-python 结果成功安装;
上面是用pip包管理工具在线安装,也可以将MySQL-python包下载下来后用源码安装:
如,我下载的是MySQL-python-1.2.5.tar.gz 版本;
然后放到源码目录,然后解压:
tar xzf MySQL-python-1.2.5.tar.gz
然后 cdMySQL-python-1.2.5
然后执行python setup.py build 进行安装:
可能也会报mysql_config not found 的错误,还是因找不到mysql_config而报错,因为这个时候是源码安装,
解决办法就是打开当前目录下的地址列表文件,见site.cfg
然后找到mysql_config相关的行,见下:
mysql_config = /usr/local/bin/mysql_config
这个时候怎么解决,自然不用说了。
1.要么修改site_cfg中的mysql_config的路径;
2.要么ln -s 下 路径;
》》》环境安装成功后,可以写一个python 连接mysql的程序并运行,结果又报错如下:
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory
发现是python找不到mysql的一个动态扩展库libmysqlclient.so
于是可以ln -s 把mysql安装目录下的此库链接到/usr/lib64下(因为是64位的操作系统)
ln -s /home/work/app/mysql/lib/libmysqlclient.so.20 /usr/lib64/libmysqlclient.so.20
运行我自己的mysql.py程序,内容如下:
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
conn = MySQLdb.connect(
host='localhost',
port=3306,
user='root',
passwd='root',
db='study',
)
cur = conn.cursor()
cur.execute("create table student(id int,name varchar(20),class varchar(30),age varchar(10))")
cur.close()
conn.commit()
conn.close()
然后登陆数据库查看,结果发现student表新建成功:
继续用程序往表里添加数据:
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
conn = MySQLdb.connect(
host='localhost',
port=3306,
user='root',
passwd='root',
db='study',
)
cur = conn.cursor()
cur.execute("insert into student values('2','Tom','3 year 2 class','9')")
cur.close()
conn.commit()
conn.close()
运行python mysql.py
然后等库查看:
mysql> select * from student;
+------+------+----------------+------+
| id | name | class | age |
+------+------+----------------+------+
| 2 | Tom | 3 year 2 class | 9 |
+------+------+----------------+------+
1 row in set (0.01 sec)
ok 已经成功的插入一行;
>>插入多条数据:
sqli="insert into student values(%s,%s,%s,%s)"
cur.executemany(sqli,[
('3','Tom','1 year 1 class','6'),
('3','Jack','2 year 1 class','7'),
('3','Yaheng','2 year 2 class','7'),
])
>>查询数据:
count = cur.execute("select * from student")
data = cur.fetchmany(count)
for stu in data:
print st
运行结果:
(2L, 'Tom', '3 year 2 class', '9')
更多示例,请参考:
http://www.runoob.com/python/python-mysql.html
更多推荐
所有评论(0)