Python實現(xiàn)一個簡單的MySQL類
更新時間:2015年01月07日 15:22:09 投稿:shichen2014
這篇文章主要介紹了Python實現(xiàn)一個簡單的MySQL類,可實現(xiàn)基本的初始化連接及查詢、刪除等功能,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)一個簡單的MySQL類。分享給大家供大家參考。
具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
#!/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]
# -*- 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程序設(shè)計有所幫助。
相關(guān)文章
Python中requests、aiohttp、httpx性能比拼
本文主要介紹了Python中requests、aiohttp、httpx性能比拼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06關(guān)于Python下的Matlab函數(shù)對應(yīng)關(guān)系(Numpy)
這篇文章主要介紹了關(guān)于Python下的Matlab函數(shù)對應(yīng)關(guān)系(Numpy),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07python實現(xiàn)批量nii文件轉(zhuǎn)換為png圖像
這篇文章主要介紹了python實現(xiàn)批量nii文件轉(zhuǎn)換為png圖像,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python利用Diagrams繪制漂亮的系統(tǒng)架構(gòu)圖
Diagrams 是一個基于Python繪制云系統(tǒng)架構(gòu)的模塊,它能夠通過非常簡單的描述就能可視化架構(gòu)。本文將利用它繪制漂亮的系統(tǒng)架構(gòu)圖,感興趣的可以了解一下2023-01-01Python3.6+Django2.0以上 xadmin站點的配置和使用教程圖解
django自帶的admin站點雖然功能強大,但是界面不是很好看。這篇文章主要介紹了Python3.6+Django2.0以上 xadmin站點的配置和使用 ,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06