mysql 使用模板

2021-08-28 07:12:04 字數 3525 閱讀 1900

將資料庫封裝成類,單獨儲存為乙個檔案,檔名為mysql_template.py

import pymysql

class sqlhelper():

def __init__(self,host,port,db,user,password):

self.host=host

self.port=port

self.db=db

self.user=user

self.password=password

self.charset="utf8"

self.__connect()

# 建立連線和得到游標

def __connect(self):

self.conn=pymysql.connect(host=self.host,port=self.port,db=self.db,

user=self.user,password=self.password,charset=self.charset)

self.cursor=self.conn.cursor()

# 查詢一條記錄

def fetchone(self,sql,*params):

try:

self.cursor.execute(sql,*params)

# 獲取返回的結果(查詢有返回結果,增刪改沒有返回結果)

data=self.cursor.fetchone()

return data

except exception as e:

print("錯誤資訊是====》",e)

finally:

self.close()

# 查詢所有資訊

def fetchall(self,sql,*params):

try:

self.cursor.execute(sql,*params)

data =self.cursor.fetchall()

return data

except exception as e:

print("錯誤資訊是===》",e)

finally:

self.close()

# 增刪改的操作

def update1(self,sql,*params):

try:

count=self.cursor.execute(sql,*params)

self.conn.commit()

return count

except exception as e:

print("錯誤資訊為===》",e)

# rollback回滾的意思。 就是資料庫裡做修改後 ( update, insert, delete)未commit

# 之前使用rollback可以恢復資料到修改之前。

self.conn.rollback()

finally:self.close()

# 關閉資源方法

def close(self):

if self.cursor !=none:

self.cursor.close()

if self.conn !=none:

self.conn.close()

之後,隨時可以呼叫函式來使用mysql了。例如下面**,裡面有點亂,但知道如何使用就好:

# 引入函式

from mysql_template import sqlhelper

# 建立物件

per=sqlhelper("localhost",3306,"stocks","root","123456")

# 建立表,沒有返回結果,如果表已經存在,會報錯

# res = per.cursor.execute('create table stock_' + 'mmm' + ' (date varchar(32),open varchar(32),close varchar(32),high varchar(32),low varchar(32),volume varchar(32),p_change varchar(32),unique(date))')

# print(res)

# 如果資料表已經存在使用 execute() 方法刪除表。

per.cursor.execute("drop table if exists employee")

# 建立資料表sql語句

sql = """create table employee (

first_name char(20) not null,

last_name char(20),

age int,

*** char(1),

income float )"""

per.cursor.execute(sql)

# 獲取一條資訊

data=per.fetchone("select * from stock_002930")

print(data)

# 獲取所有的物件

data1=per.fetchall("select *from emp")

print(data1)

#增加使用者

one="insert into allstock value (%s,%s,%s,%s)"

one1=["難兄","男","9999","2"]

count=per.update1(one,one1)

print(count)

#刪除使用者

two="delete from emp where deptid=%s"

two1=(3)

count=per.update1(two,two1)

print(count)

#查詢總數量

sql = "select count(*) from emp "

data = per.fetchall(sql)

print(data)

#將資料庫的記錄輸出

class user(object):

def __init__(self,id,name,age,***,tel,deptid):

self.id = id

self.name = name

self.age = age

self.*** = ***

self.tel = tel

self.deptid = deptid

def __str__(self):

return "使用者名稱%s,使用者**%s,使用者性別%s,使用者年齡%s" %(self.name,self.tel,

self.***,self.age)

data2=per.fetchall("select *from emp")

print(data2)

list=

for x in data2:

u=user(x[0],x[1],x[2],x[3],x[4],x[5])

for i in list:

print(i)

zabbix使用自帶模板監控MySQL

監控mysql不能直接使用zabbix自帶模板,還需要到被監控的mysql客戶端做配置。2 設定完帳戶之後在被監控端新建 etc zabbix my.cnf以提供zabbix agent訪問資料庫,內容類似如下 vim etc zabbix my.cnf mysql host localhost u...

在ABP模板工程中使用MySQL

2 在windows上安裝mysql,這裡不多說,我用的是mysql installer 5.5.21.0 3 開啟模板專案,還原nuget包先 開啟專案後,在 程式包管理器控制平台 中選擇 entityframwork以及 web專案 號為你的專案名稱字首 install package mysq...

php模板如何使用,PHP View,使用模板

好吧,我的問題很簡單,但是很難接受該解決方案,但是無論如何.接下來,我有乙個 微型框架 可以編寫單個方案,對我有很大幫助,可以加速但是,在某種程度上,問題仍然存在,在某種程度上,使用模板方案非常容易,也非常有趣,因為當您必須更改與視覺化相關的任何內容時,模板只會更改,但是,及時渲染此模板,這是最好的...