MySQLdb安装和使用2
http://blog.chinaunix.net/uid-8487640-id-3183185.html
- 首先要下载下载:请到官方网站http://sourceforge.net/projects/mysql-python/或者点击链接下载http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz?use_mirror=nchc
- 解压:tar zxvf MySQL-python*
- 进入文件目录,运行以下命令:
python setup.py install - 安装完成,到你的python安装目录下的site-packages目录里检查以下文件是否存在,如果存在即代表安装成功了
Linux:MySQL_python-1.2.3c1-py2.6-linux-i686.egg
Mac OS X:MySQL_python-1.2.3c1-py2.6-macosx-10.4-x86_64.egg
注:如果碰到mysql_config not found的问题,有两种方法解决:
1)ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config
将mysql_confi从你的安装目录链接到/usr/local/bin目录下,这样就可以在任意目录下访问了(也可以放到/usr/bin)
2)编辑源码文件夹的site.cfg文件,去掉#mysql_config = /usr/local/bin/mysql_config前的注释#,修改后面的路径为你的mysql_config真正的目录就可以了。(如果不知道mysql_config在哪里,运行命令:whereis mysql_config)
注:如果碰到import error: libmysqlclient.so.18: cannot open shared object file: No such file or directory
解决方法: locate or find libmysqlclient.so.18
link path/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
vi /etc/ld.so.conf //加入libmysqlclient.so.18 所在的目录
插入: /usr/lib/
保存退出后执行/sbin/ldconfig生效
- 测试方法
1)运行命令python进入python运行环境
2)输入以下python代码进行测试
import MySQLdb - test=MySQLdb.connect(db='mydb',host='myhost',user='u',passwd='p')
- cur = test.cursor()
- cur.execute('show databases;')
- for data in cur.fetchall():
- print data
- 3)如果你在屏幕上看到了你几个数据库的库名的输出代表你安装成功了
- 可能碰到的问题
1)问题:ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
原因是python无法找到mysql目录下的libmysqlclient_r.so.16动态库,其实MySQLdb是调用mysql的c函数库.所以本机上首先得安装了mysql
然后: export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql:$LD_LIBRARY_PATH
并且将/usr/local/mysql5.1/lib/mysql 放入/etc/ld.so.conf中
/etc/ld.so.conf改后内容为:
include ld.so.conf.d/*.conf
/usr/local/mysql5.1/lib/mysql
最后重新再测试一下,就不会有上面的问题了
MySQLdb操作:
- #!/usr/bin/env python
- #coding=utf-8
- ###################################
- #MySQLdb 示例
- #
- ##################################
- import MySQLdb
- #建立和数据库系统的连接
- conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')
- #获取操作游标
- cursor = conn.cursor()
- #执行SQL,创建一个数据库.
- cursor.execute("""create database python """)
- #关闭连接,释放资源
- cursor.close();
#!/usr/bin/env python
#coding=utf-8
##################################
#MySQLdb 示例 #
##################################
import MySQLdb
#建立和数据库系统的连接
conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')
#获取操作游标
cursor = conn.cursor()
#执行SQL,创建一个数据库.
cursor.execute("""create database python """)
#关闭连接,释放资源
cursor.close();
创建数据库,创建表,插入数据,插入多条数据
- #!/usr/bin/env python
- #coding=utf-8
- ###################################
- #MySQLdb 示例
- #
- ##################################
- import MySQLdb
- #建立和数据库系统的连接
- conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')
- #获取操作游标
- cursor = conn.cursor()
- #执行SQL,创建一个数据库.
- cursor.execute("""create database if not exists python""")
- #选择数据库
- conn.select_db('python');
- #执行SQL,创建一个数据表.
- cursor.execute("""create table test(id int, info varchar(100)) """)
- value = [1,"inserted ?"];
- #插入一条记录
- cursor.execute("insert into test values(%s,%s)",value);
- values=[]
- #生成插入参数值
- for i in range(20):
- values.append((i,'Hello mysqldb, I am recoder ' + str(i)))
- #插入多条记录
- cursor.executemany("""insert into test values(%s,%s) """,values);
- #关闭连接,释放资源
- cursor.close();
#!/usr/bin/env python
#coding=utf-8
###################################
#MySQLdb 示例 #
##################################
import MySQLdb
#建立和数据库系统的连接
conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')
#获取操作游标
cursor = conn.cursor()
#执行SQL,创建一个数据库.
cursor.execute("""create database if not exists python""")
#选择数据库
conn.select_db('python');
#执行SQL,创建一个数据表.
cursor.execute("""create table test(id int, info varchar(100)) """)
value = [1,"inserted ?"];
#插入一条记录
cursor.execute("insert into test values(%s,%s)",value);
values=[]
#生成插入参数值
for i in range(20):
values.append((i,'Hello mysqldb, I am recoder ' + str(i)));
#插入多条记录
cursor.executemany("""insert into test values(%s,%s) """,values);
#关闭连接,释放资源
cursor.close();
查询和插入的流程差不多,只是多了一个得到查询结果的步骤
- #!/usr/bin/env python
- #coding=utf-8
- #
- # MySQLdb 查询
- #
- #######################################
- import MySQLdb
- conn = MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='python')
- cursor = conn.cursor()
- count = cursor.execute('select * from test')
- print '总共有 %s 条记录',count
- #获取一条记录,每条记录做为一个元组返回
- print "只获取一条记录:"
- result = cursor.fetchone();
- print result
- #print 'ID: %s info: %s' % (result[0],result[1])
- print 'ID: %s info: %s' % result
- #获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录
- print "只获取5条记录:"
- results = cursor.fetchmany(5)
- for r in results:
- print r
- print "获取所有结果:"
- #重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,
- cursor.scroll(0,mode='absolute')
- #获取所有结果
- results = cursor.fetchall()
- for r in results:
- print r
- conn.close()
默认mysqldb返回的是元组,这样对使用者不太友好,也不利于维护
下面是解决方法
- import MySQLdb
- import MySQLdb.cursors
- conn = MySQLdb.Connect (
- host = 'localhost', user = 'root' ,
- passwd = '', db = 'test', compress = 1,
- cursorclass = MySQLdb.cursors.DictCursor, charset='utf8') // <- important
- cursor = conn.cursor()
- cursor.execute ("SELECT name, txt FROM table")
- rows = cursor.fetchall()
- cursor.close()
- conn.close()
- for row in rows:
- print row ['name'], row ['txt'] # bingo!
- # another (even better) way is:
- conn = MySQLdb . Connect (
- host = ' localhost ', user = 'root' ,
- passwd = '', db = 'test' , compress = 1)
- cursor = conn.cursor (cursorclass = MySQLdb.cursors.DictCursor)
- # ...
- # results by field name
- cursor = conn.cursor()
- # ...
- # ...results by field number
MySQLdb安装和使用2的更多相关文章
- python MySQLdb安装和使用
MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 首先要下载下载:请到官方网站http://sourceforge.net/projects/mysql-py ...
- mysqldb 安装
MySQLdb是python的一个标准的连接和操纵mysql的模块. ubuntu下安装: sudo apt-get install python-mysqldb sudo apt-get insta ...
- win10 64位 python3.6 django1.11 MysqlDB No module named 'MySQLdb' 安装MysqlDB报错 Microsoft Visual C++ 14.0 is required
在python3.6中操作数据库,再按python2.7安装MySQLdb进行数据库连接已经不可用了,我使用的是另外一个方法:PyMySQL,安装好之后还是不能直接连接MySQL的,启动项目后报No ...
- PYTHON -MYSQLDB安装遇到的问题和解决办法
目前下载的mysqldb在window下没有exe安装包了,只有源码. 使用python setup.py install 命令安装, 报错如下: 异常信息如下: F:\devtools\MySQL- ...
- windows python MySQLdb 安装配置
一.环境 系统:windows10/7/8 软件: 1.python2.7.XX(https://www.python.org/downloads/或者https://www.python.org/f ...
- MySQLdb安装记
1 安装 python-devel 2. site.cfg 改mysql_config成实际位置 mysql_config = /mysqldata/mariadb530/bin/mysql_conf ...
- centos mysqldb 安装
yum install mysql-devel.x86_64 yum install python-devel pip install MySQL-python
- MySQLdb模块安装-win环境
原帖地址:http://blog.csdn.net/wklken/article/details/7253245 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 P ...
- windows(32位 64位)下python安装mysqldb模块
windows(32位 64位)下python安装mysqldb模块 www.111cn.net 编辑:mengchu9 来源:转载 本文章来给各位使用在此windows系统中的python来安装一个 ...
随机推荐
- 守望先锋overwatch美服外服设置方法
打开:C:\Users\你的用户名\AppData\Roaming\Battle.net\Battle.net.config 替换为下方内容: { "Client": { &quo ...
- ECMall关于数据查询缓存的问题
刚接触Ecmall的二次开发不久,接到一个任务.很常见的任务,主要是对数据库进行一些操作,其中查询的方法我写成这样: 01 function get_order_data($goods_id) 02 ...
- Java RMI远程方法调用
RMI(远程接口调用) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程引用层(Remote Reference Layer)和传 ...
- BitMap 内存使用优化
在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitmap时,分给虚拟机中的图片的堆栈大小只有8M,如果超出了,就会出现OutOfMemory异常.所以,对于图 ...
- WCF Concurrency (Single, Multiple, and Reentrant) and Throttling
http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and Introduc ...
- statspack系列5
原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-5/ 作者:Jonathan Lewis 前些天,有人给我发了 ...
- 知乎上关于c和c++的一场讨论_看看高手们的想法
为什么很多开源软件都用 C,而不是 C++ 写成? 余天升 开源社区一直都不怎么待见C++,自由软件基金会创始人Richard Stallman认为C++有语法歧义,这样子没有必要.非常琐碎还会和C不 ...
- 20个可以帮你简化iOS app开发流程的工具
这里推荐20个可以帮你简化iOS app开发流程的工具.很多开发者都使用过这些工具,涉及原型和设计.编程.测试以及最后的营销,基本上涵盖了整个开发过程. 原型和设计 有了一个很好的创意后,你要做的不是 ...
- 【转】Android4.4(MT8685)源码蓝牙解析--BLE搜索
原文网址:http://blog.csdn.net/u013467735/article/details/41962075 BLE:全称为Bluetooth Low Energy.蓝牙规范4.0最重要 ...
- Android4.0 -- UI控件之 Menu 菜单的的使用(四)
2. PopupMenu 菜单的使用 1) PopupMenu 介绍 创建弹出菜单,它一般会在view的上边或者下边弹出菜单[具体看是否有空间],注意弹出菜单是api在11或者更高版本中使用 ...