博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python模块之'MySQLdb'
阅读量:7120 次
发布时间:2019-06-28

本文共 1468 字,大约阅读时间需要 4 分钟。

hot3.png

1、导入MySQL模块

Python 2.7.5 (default, Jun 17 2014, 18:11:42) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import MySQLdbTraceback (most recent call last):  File "
", line 1, in 
ImportError: No module named MySQLdb>>>

     安装MySQL模块

[root@python ~]# wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip[root@python ~]# unzip MySQL-python-1.2.5.zip[root@python MySQL-python-1.2.5]# python setup.py build[root@python MySQL-python-1.2.5]# python setup.py install# 或者这样(centos/redhat)[root@python ~]# yum install MySQL-python        # 提前配置好yum源# 再次导入成功[root@python ~]# pythonPython 2.7.5 (default, Jun 24 2015, 00:41:19) [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import MySQLdb

2、MySQLdb模块的使用

import MySQLdbconn = MySQLdb.connect(host,user,passwd,dbname,port)cur = conn.cursor()cur.execute('select * from mysql.user where user="shaw"')print cur.fetchone()                            # 打印输出cur.close()conn.close()
import MySQLdbtry:    conn = MySQLdb.connect(host,user,passwd,dbname,port)    cur = conn.cursor()    cur.execute('select * from mysql.user where user="shaw"')    print cur.fetchone()                            # 打印输出    cur.close()    conn.close()except MySQLDB.Error,e:    print 'MySQL Error Msg:' ,e

转载于:https://my.oschina.net/u/2428313/blog/490709

你可能感兴趣的文章
Linux 下编译安装OpenCV【转】
查看>>
platform_get_resource的分析
查看>>
底部导航栏使用BottomNavigationBar
查看>>
math对象
查看>>
Storm介绍及核心组件和编程模型
查看>>
Eclipse添加maven之后报错 Eclipse is running in a JRE, but a JDK is required 解决方法
查看>>
Sql Server的艺术(二) SQL复杂条件搜索
查看>>
C#执行osql脚本时部分脚本未执行
查看>>
IIS服务命令
查看>>
Java基础之中间件的初识
查看>>
下载软件的临时文件思路和实现
查看>>
修改Linux系统默认编辑器
查看>>
C语言博客作业05--指针
查看>>
LeetCode 504. Base 7 (C++)
查看>>
QT多线程简单例子
查看>>
郑继明等《数值分析》习题解答
查看>>
----uni-app之APP更新及静默更新----
查看>>
关于内置函数
查看>>
Union的一个妙法
查看>>
找回密码forget_password
查看>>