CentOS Python操作MySQL的脚本简录

yum -y install MySQL-python

cat >/tmp/test.py<<EOF
#!/usr/bin/python
# coding:utf-8
# 连接MySQL测试
import MySQLdb
try:
    conn = MySQLdb.connect( host='127.0.0.1',
                            user='root',
                            passwd='vincent',
                            db='mysql',
                            port=3306,
                            charset='utf8')
    cur=conn.cursor()
    cur.execute('select Host,User from user')
    # 将指针置0,取出所有记录
    # cur.scroll(0,mode='absolute')
    results=cur.fetchall()
    print '-' * 23
    for r in results:
        print "| %-9s | %-7s |" % (r[0],r[1])
    print '-' * 23
    cur.close()
    conn.close()
except MySQLdb.Error,e:
    print "Mysql Error %d: %s" % (e.args[0], e.args[1])
EOF

python test.py

这里写图片描述

[TOC]

猜你喜欢

转载自blog.csdn.net/zwjzqqb/article/details/80767782
今日推荐