一.安装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()
评论