MySQL for Python(一)

默北 MySQLMySQL for Python(一)已关闭评论8,915字数 568阅读1分53秒阅读模式

一.安装mysql-python

1.suse,redhat,fedora系统文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

#yum install mysqldb文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

2.debian,ubuntu系统文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

# apt-get install python-mysqldb文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

3.源码安装文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

# http://sourceforge.net/projects/mysql-python/files/文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

二.import mysql-python文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> import MySQLdb文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

三.创建连接对象文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> mydb=MySQLdb.connect(host="hostname",user="username",passwd="password",db="database_name")文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

四.创建指针对象文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> cur=mydb.cursor()文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

五.执行语句文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> command=cur.execute('select cloumn_name from table_name where contions')文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

六.获取结果文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> results=command.fetchall()文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> print results文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

fetchall方法返回的结果是一串tuples(元组),对元组进行处理,得到一定格式的输出。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

七.关闭连接文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> mydb.commit()文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-for-python-1/

python> mysql.close()

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 21/11/2011 16:09:49
  • 转载请务必保留本文链接:https://www.ttlsa.com/mysql/mysql-for-python-1/