python连接mysql

example

#coding=UTF-8

import sys
import MySQLdb
import time

reload(sys)
sys.setdefaultencoding('utf-8')

def connectDemo():
    return MySQLdb.Connection("127.0.0.1","root","root","demo",3306,charset="utf8")


if __name__ == '__main__':
    begin=time.time()

    conn=connectDemo()
    cursor = conn.cursor()
    sql="""
        show tables
        """
    count = cursor.execute(sql)
    rows = cursor.fetchall()
    cursor.close()
    conn.close()
    print "========demo库共:%s 张表============" % (count)

    print '耗时:%s 秒' % (time.time()-begin)
    原文作者:codecraft
    原文地址: https://segmentfault.com/a/1190000007060349
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞