国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現一個簡單的MySQL類

2019-11-25 18:02:39
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現一個簡單的MySQL類。分享給大家供大家參考。

具體實現方法如下:

復制代碼 代碼如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Created on 2011-2-19
# @author: xiaoxiao
import MySQLdb
import sys
__all__ = ['MySQL']
class MySQL(object):
    '''
    MySQL
    '''
    conn = ''
    cursor = ''
    def __init__(self,host='localhost',user='root',passwd='root',db='mysql',charset='utf8'):
      
        """MySQL Database initialization """
        try:
            self.conn = MySQLdb.connect(host,user,passwd,db)
        except MySQLdb.Error,e:
            errormsg = 'Cannot connect to server/nERROR (%s): %s' %(e.args[0],e.args[1])
            print errormsg
            sys.exit()
          
        self.cursor = self.conn.cursor()
      
    def query(self,sql):
        """  Execute SQL statement """
        return self.cursor.execute(sql)
  
    def show(self):
        """ Return the results after executing SQL statement """
        return self.cursor.fetchall()
             
    def __del__(self):
        """ Terminate the connection """
        self.conn.close()
        self.cursor.close()
      
#test
if __name__ == '__main__':
  
    mysql = MySQL(host=localhost,passwd='test',db='mysql')
    mysql.query('select * from users')
    result = mysql.show()
    print len(result)
    print result[1]

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中山市| 雅江县| 凤阳县| 石渠县| 建昌县| 东莞市| 江油市| 和田市| 崇阳县| 外汇| 香港| 龙游县| 平塘县| 江门市| 两当县| 西乡县| 磐石市| 新平| 琼海市| 山东省| 多伦县| 巧家县| 明溪县| 桐庐县| 济宁市| 乐山市| 谢通门县| 霸州市| 灵川县| 阿巴嘎旗| 宁安市| 乐至县| 濮阳市| 潞西市| 中江县| 井陉县| 宁都县| 广丰县| 柳林县| 淮安市| 北碚区|