在Ubuntu上安装MySQLdb

来源:互联网 发布:淘宝男装店铺便宜 编辑:程序博客网 时间:2024/04/30 05:24

Ubuntu下python安装mysqldb(驱动)

05.14.2010 · Posted in Python
之前有写过《Windows+Python2.6+MySQL驱动安装》,今天呢是Ubuntu下给python安装mysql驱动,方法如下:
在终端中输入:sudo apt-get install python-mysqldb
OK,搞定,简单吧?来测试下
安装完成之后可以在Python解释器中测试一下
输入 import MySQLdb #注意大小写
如果不报错,就证明安装成功了。

 

上面好简单呀!

程序实例:

 

  1. from MySQLdb import *
  2. def conn():
  3. cn=Connection('127.0.0.1','root','','test')
  4. #Connection()函数的参数依次为
  5. #     host(string, host to connect);
  6. #     user(string, user to connect as);
  7. #     passwd(string, password to use);
  8. #     db(string, database to use)
  9. #也可以这样选择数据库
  10. #cn.select_db('test')
  11. cur=cn.cursor()
  12. cur.execute('select * from user')
  13. #设置游标的位置,不设置默认为0
  14. #cur.scroll(0)
  15. row=cur.fetchone()
  16. #查询游标位置的一条记录,返回值为元组
  17. print row[0] #输出第一个字段内容
  18. print row[1]
  19.  
  20. if __name__=='__main__':
  21. conn()

4。查询时也可执行fetchall(),返回所有记录为一个元组(tuple),元组的每个元素为每行记录;还有fetchmany(size)

5。增删改的例子
insert:

cur.execute('insert into user (name,passwd) values('sparezgw','asdfasdf')')
cur.insert_id()

update:

cur.execute('update user set passwd='asdfasdf' where name='sparezgw'')

delete:

cur.execute('delete from user where id=2')

 

 

 

 

 

 

 

 

 

 

在Ubuntu上安装MySQLdb


作者:NinGoo | 【转载须以超链接形式标明文章原始出处和作者信息】

准备用Python写点脚本练练手,于是在Ubuntu上安装Python的MySQLdb,本以为很简单的事,没想到还碰到几个小波折,因此记录一下以备忘。

 

首先需要安装Python-dev,否则后面编译MySQLdb的时候会报错,找不到头文件:

 

building '_mysql' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC
 -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3
-I/u01/mysql/include/mysql -I/usr/include/python2.6 -c _mysql.c
-o build/temp.linux-i686-2.6/_mysql.o -DUNIV_LINUX
In file included from _mysql.c:29:
pymemcompat.h:10: fatal error: Python.h: 没有那个文件或目录
compilation terminated.
error: command 'gcc' failed with exit status 1

sudo apt-get install python-dev

 

其次需要先安装setuptools,否则MySQLdb无法编译

 

ImportError: No module named setuptools

 

setuptools从这里下载

 

python setup.py build


sudo python setup.py install

 

从这里下载MySQLdb


修改site.cfg将mysql_config指向正确的位置

 

python setup.py build


sudo python setup.py install

 

最后还需要安装libmysqlclient-dev,否则import模块的时候会出错

 

ImportError: libmysqlclient_r.so.16: cannot open shared object file:


 No such file or directory

 

sudo apt-get install libmysqlclient-dev

 

装完以后,来个hello world式的简单查询

 

#!/usr/bin/env python


import MySQLdb

db=MySQLdb.connect(host="host_name",db="mysql",user="ningoo",passwd="password")
c=db.cursor()
n=c.execute("select user,host from user")
for row in c.fetchall():
        for col in row:
                print col

 

 

 

 

UBUNTU 下安装MYSQLDB 遇到的问题
2009-04-09 10:04

在我的笔记本上,直接安装,无任何问题

 

昨天想在服务器上需要运行MYSQLDB,sudo apt-get install python-mysqldb提示安装成功,可是import MySQLdb 提示找不到此模块

sudo apt-get source python-mysqldb,想编译一下,总也不成功,总报错,google,了半天,

估计是库不全,

sudo apt-get install python-all-dev

sudo apt-get install libmysqlclient15-dev

sudo apt-get install zlib1g-dev

再重新编译 ,OK,记录在此,以免下次有经验

原创粉丝点击