Python數(shù)據(jù)操作方法封裝類實例
本文實例講述了Python數(shù)據(jù)操作方法封裝類。分享給大家供大家參考,具體如下:
工作中經(jīng)常會用到數(shù)據(jù)的插敘、單條數(shù)據(jù)插入和批量數(shù)據(jù)插入,以下是本人封裝的一個類,推薦給各位:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Eric.yue
import logging
import MySQLdb
class _MySQL(object):
def __init__(self,host, port, user, passwd, db):
self.conn = MySQLdb.connect(
host = host,
port = port,
user = user,
passwd = passwd,
db = db,
charset='utf8'
)
def get_cursor(self):
return self.conn.cursor()
def query(self, sql):
cursor = self.get_cursor()
try:
cursor.execute(sql, None)
result = cursor.fetchall()
except Exception, e:
logging.error("mysql query error: %s", e)
return None
finally:
cursor.close()
return result
def execute(self, sql, param=None):
cursor = self.get_cursor()
try:
cursor.execute(sql, param)
self.conn.commit()
affected_row = cursor.rowcount
except Exception, e:
logging.error("mysql execute error: %s", e)
return 0
finally:
cursor.close()
return affected_row
def executemany(self, sql, params=None):
cursor = self.get_cursor()
try:
cursor.executemany(sql, params)
self.conn.commit()
affected_rows = cursor.rowcount
except Exception, e:
logging.error("mysql executemany error: %s", e)
return 0
finally:
cursor.close()
return affected_rows
def close(self):
try:
self.conn.close()
except:
pass
def __del__(self):
self.close()
mysql = _MySQL('127.0.0.1', 3306, 'root', '123456', 'test')
def create_table():
table = """
CREATE TABLE IF NOT EXISTS `watchdog`(
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(100),
`price` int(11) NOT NULL DEFAULT 0
) ENGINE=InnoDB charset=utf8;
"""
print mysql.execute(table)
def insert_data():
params = [('dog_%d' % i, i) for i in xrange(12)]
sql = "INSERT INTO `watchdog`(`name`,`price`) VALUES(%s,%s);"
print mysql.executemany(sql, params)
if __name__ == '__main__':
create_table()
insert_data()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python如何telnet到網(wǎng)絡(luò)設(shè)備
這篇文章主要介紹了Python如何telnet到網(wǎng)絡(luò)設(shè)備,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02
python通過cookie模擬已登錄狀態(tài)的初步研究
對于那些需要在登錄環(huán)境下進(jìn)行的爬蟲操作,模擬登陸或偽裝已登錄狀態(tài)是一個剛性需求。這篇文章主要介紹了python通過cookie模擬已登錄狀態(tài)的相關(guān)資料,需要的朋友可以參考下2016-11-11
Pytorch之tensorboard無法啟動和顯示問題及解決
這篇文章主要介紹了Pytorch之tensorboard無法啟動和顯示問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
提升Python Scrapy庫數(shù)據(jù)采集速度實現(xiàn)高效爬蟲
Scrapy是一個強(qiáng)大而靈活的Python爬蟲框架,被廣泛用于數(shù)據(jù)采集、網(wǎng)站抓取和網(wǎng)絡(luò)爬蟲開發(fā),本文將深入介紹Scrapy的功能和用法,并提供豐富的示例代碼,幫助更好地理解和應(yīng)用2023-11-11
Python爬蟲運用正則表達(dá)式的方法和優(yōu)缺點
這篇文章主要給大家介紹了關(guān)于Python爬蟲運用正則表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
基于python-opencv3實現(xiàn)圖像顯示和保存操作
這篇文章主要介紹了基于python opencv3的圖像顯示和保存操作方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06

