Python數(shù)據(jù)操作方法封裝類實(shí)例
本文實(shí)例講述了Python數(shù)據(jù)操作方法封裝類。分享給大家供大家參考,具體如下:
工作中經(jīng)常會(huì)用到數(shù)據(jù)的插敘、單條數(shù)據(jù)插入和批量數(shù)據(jù)插入,以下是本人封裝的一個(gè)類,推薦給各位:
#!/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常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
編寫(xiě)Python腳本來(lái)獲取Google搜索結(jié)果的示例
這篇文章主要介紹了編寫(xiě)Python腳本來(lái)獲取Google搜索結(jié)果的示例,也是利用Python編寫(xiě)爬蟲(chóng)的一個(gè)簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下2015-05-05
python實(shí)現(xiàn)文件分片上傳的接口自動(dòng)化
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)文件分片上傳的接口自動(dòng)化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Python PyWebIO提升團(tuán)隊(duì)效率使用介紹
這篇文章主要為大家介紹了Python PyWebIO提升團(tuán)隊(duì)效率使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
python中把嵌套的列表合并成一個(gè)列表方法總結(jié)
python中l(wèi)ist這種數(shù)據(jù)結(jié)構(gòu)很常用到,下面這篇文章主要給大家介紹了關(guān)于python中把嵌套的列表合并成一個(gè)列表方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
python中類變量與成員變量的使用注意點(diǎn)總結(jié)
python 的類中主要會(huì)使用的兩種變量:類變量與成員變量。類變量是類所有實(shí)例化對(duì)象共有的,而成員變量是每個(gè)實(shí)例化對(duì)象自身特有的。下面這篇文章主要給大家介紹了在python中類變量與成員變量的一些使用注意點(diǎn),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04
使用Python實(shí)現(xiàn)插入100萬(wàn)條數(shù)據(jù)到MySQL數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)插入100萬(wàn)條數(shù)據(jù)到MySQL數(shù)據(jù)庫(kù),文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2024-04-04

