python mysql工具类_Python工具类(一)—— 操作Mysql数据库
如何調(diào)用直接看__main__函數(shù)里如何調(diào)用此工具類就闊以啦!
# encoding=utf-8
import pymysql
# 導(dǎo)入所有Mysql配置常量,請(qǐng)自行指定文件
from conf.settings import *
class MysqlConnection(object):
"""
mysql操作類,對(duì)mysql數(shù)據(jù)庫進(jìn)行增刪改查
"""
def __init__(self, config):
# Connect to the database
self.connection = pymysql.connect(**config)
self.connection.autocommit(True)
self.cursor = self.connection.cursor()
def QueryAll(self, sql):
"""
查詢所有數(shù)據(jù)
:param sql:
:return:
"""
# 數(shù)據(jù)庫若斷開即重連
self.reConnect()
self.cursor.execute(sql)
return self.cursor.fetchall()
def QueryMany(self, sql, n):
"""
查詢某幾條數(shù)據(jù)數(shù)據(jù)
:param sql:
:return:
"""
# 數(shù)據(jù)庫若斷開即重連
self.reConnect()
self.cursor.execute(sql)
return self.cursor.fetchmany(n)
def QueryOne(self, sql):
"""
查詢某幾條數(shù)據(jù)數(shù)據(jù)
:param sql:
:return:
"""
# 數(shù)據(jù)庫若斷開即重連
self.reConnect()
self.cursor.execute(sql)
return self.cursor.fetchone()
# return self.cursor.fetchone()
def reConnect(self):
"""
重連機(jī)制
:return:
"""
try:
self.connection.ping()
except:
self.connection()
def Operate(self, sql, params=None, DML=True):
"""
數(shù)據(jù)庫操作:增刪改查
DML: insert / update / delete
DDL: CREATE TABLE/VIEW/INDEX/SYN/CLUSTER
"""
try:
# 數(shù)據(jù)庫若斷開即重連
self.reConnect()
with self.connection.cursor() as cursor:
cursor.execute(sql, params)
self.connection.commit()
except Exception as e:
if DML:
# 涉及DML操作時(shí),若拋異常需要回滾
self.connection.rollback()
print(e)
def __del__(self):
"""
MysqlConnection實(shí)例對(duì)象被釋放時(shí)調(diào)用此方法,用于關(guān)閉cursor和connection連接
"""
self.cursor.close()
self.connection.close()
if __name__ == "__main__":
# 初始化MysqlConnection實(shí)例對(duì)象需要傳Mysql配置信息的字典
config = {'host': MYSQL_HOST, 'charset': CHARSET, 'db': DB, 'user': USER, 'port': MYSQL_PORT, 'password': PASSWORD}
msc = MysqlConnection(config)
sql = "delete from users where username ='%s'" % ""
print(msc.Operate(sql))
Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實(shí)例_python
mysql是一個(gè)優(yōu)秀的開源數(shù)據(jù)庫,它現(xiàn)在的應(yīng)用非常的廣泛,因此很有必要簡(jiǎn)單的介紹一下用python操作mysql數(shù)據(jù)庫的方法.python操作數(shù)據(jù)庫需要安裝一個(gè)第三方的模塊,在http://mysql ...
python學(xué)習(xí)筆記之——操作mysql數(shù)據(jù)庫
Python 標(biāo)準(zhǔn)數(shù)據(jù)庫接口為 Python DB-API,Python DB-API為開發(fā)人員提供了數(shù)據(jù)庫應(yīng)用編程接口. Python 數(shù)據(jù)庫接口支持非常多的數(shù)據(jù)庫,你可以選擇適合你項(xiàng)目的數(shù)據(jù)庫: ...
python 通過 pymysql模塊 操作 mysql 數(shù)據(jù)庫
Python 中操作 MySQL 步驟 安裝模塊 pip install pymysql 引入模塊 在py文件中引入pymysql模塊 from pymysql import * Connection ...
python 3.6 +pyMysql 操作mysql數(shù)據(jù)庫
版本信息:python:3.6 mysql:5.7 pyMysql:0.7.11 ########################################################### ...
python【第十二篇下】操作MySQL數(shù)據(jù)庫以及ORM之 sqlalchemy
內(nèi)容一覽: 1.Python操作MySQL數(shù)據(jù)庫 2.ORM sqlalchemy學(xué)習(xí) 1.Python操作MySQL數(shù)據(jù)庫 2. ORM sqlachemy 2.1 ORM簡(jiǎn)介 對(duì)象關(guān)系映射(英語: ...
使用python操作mysql數(shù)據(jù)庫
這是我之前使用mysql時(shí)用到的一些庫及開發(fā)的工具,這里記錄下,也方便我查閱. python版本: 2.7.13 mysql版本: 5.5.36 幾個(gè)python庫 1.mysql-connector ...
python操作mysql數(shù)據(jù)庫增刪改查的dbutils實(shí)例
python操作mysql數(shù)據(jù)庫增刪改查的dbutils實(shí)例 # 數(shù)據(jù)庫配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用單引 ...
【Python】使用python操作mysql數(shù)據(jù)庫
這是我之前使用mysql時(shí)用到的一些庫及開發(fā)的工具,這里記錄下,也方便我查閱. python版本: 2.7.13 mysql版本: 5.5.36 幾個(gè)python庫 1.mysql-connector ...
python + docker, 實(shí)現(xiàn)天氣數(shù)據(jù) 從FTP獲取以及持久化(二)-- python操作MySQL數(shù)據(jù)庫
前言 在這一節(jié)中,我們主要介紹如何使用python操作MySQL數(shù)據(jù)庫. 準(zhǔn)備 MySQL數(shù)據(jù)庫使用的是上一節(jié)中的docker容器 “test-mysql”. Python 操作 MySQL 我們使用 ...
隨機(jī)推薦
樹莓派pppoe
連接的網(wǎng)絡(luò)是移動(dòng)(鐵通)的寬帶,不同的寬帶的dns需要修改. 1.首先安裝pppoe包 apt-get install pppoe 2.然后,復(fù)制conf文件/etc/ppp/pppoe.conf: ...
利用Queue隊(duì)列實(shí)現(xiàn)FIFO的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <
TopFreeTheme精選免費(fèi)模板【20130701.特別版】
今天我們整理了16款WordPress和Joomla的最新主題.它們都是來自Themeforest,RocketTheme,YooTheme以及TemPlaza的高質(zhì)量主題,趕快收藏起來吧. Este ...
u-boot和linux的機(jī)器碼
先看u-boot的機(jī)器碼和linux的機(jī)器碼是在什么地方?jīng)Q定的. 1. u-boot的機(jī)器碼是在u-boot的board/fs2410/fs2410.c文件里決定的: ??? /* arch numb ...
MongoDB學(xué)習(xí)筆記02
MongoDB中使用find來進(jìn)行查詢,查詢就是返回一個(gè)集合中文檔的子集,子集合的范圍從0個(gè)文檔到整個(gè)集合.find的第一個(gè)參數(shù)決定了要返回哪些文檔.空的查詢文檔{}會(huì)匹配集合的全部?jī)?nèi)容,要是不指定查 ...
android中 MediaStore提取縮略圖和原始圖像
android中 MediaStore提取縮略圖和原始圖像?. 歡迎轉(zhuǎn)載:http://blog.csdn.net/djy1992/article/details/10005767 提取圖像的Thum ...
Frame框架
框架 frameset?? ?框架集?? ?如果使用框架集,當(dāng)前頁面不能有body?? ?cols="300,*" :左右拆分,左邊寬300,右邊寬剩余??? rows=" ...
淺嘗Java(二、代碼折疊插件的使用)
主題:eclipse代碼折疊插件的使用. 工作中在使用eclipse開發(fā)Java項(xiàng)目時(shí),我們會(huì)寫很多if,for循環(huán)啊什么的,這使得我們的項(xiàng)目代碼會(huì)有很多很多行.寫完后要想檢查或者查看,就要從頭一行一 ...
Linux命令:pushd
語法 pushd [-n] [+N | -N | dir] 更改新目錄并(或)壓棧,或者把棧里的某個(gè)目錄推到棧頂. 說明 pushd dir # 切換到目標(biāo)目錄dir,并將dir壓棧. pushd # ...
selenium自動(dòng)測(cè)試
import requestsimport sysimport iofrom selenium import webdriverfrom selenium.webdriver.common.actio ...
總結(jié)
以上是生活随笔為你收集整理的python mysql工具类_Python工具类(一)—— 操作Mysql数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浙江大学计算机科学与技术学院分数线,浙江
- 下一篇: mysql large_【转】mysql