十四 python資料庫的封裝

2022-07-13 17:24:10 字數 3581 閱讀 8002

import

pymysql

#查 所資料

defget_all(sql):

conn = pymysql.connect(host="

localhost

", user="

root

", password="

root

", database="

db6"

) cur = conn.cursor(cursor=pymysql.cursors.dictcursor)

cur.execute(sql)

res =cur.fetchall()

cur.close()

conn.close()

return

res#

查 一行資料

defget_one(sql,args):

conn = pymysql.connect(host="

localhost

", user="

root

", password="

root

", database="

db6"

) cur = conn.cursor(cursor=pymysql.cursors.dictcursor)

cur.execute(sql,args)

res =cur.fetchone()

cur.close()

conn.close()

return

res#

增 刪 改 都要提交 commit

defget_mif(sql,args):

conn = pymysql.connect(host="

localhost

", user="

root

", password="

root

", database="

db6"

) cur = conn.cursor(cursor=pymysql.cursors.dictcursor)

cur.execute(sql,args)

conn.commit()

cur.close()

conn.close()

#增 刪 改 都要提交 commit

#新增並且帶返回值

defget_create(sql,args):

conn = pymysql.connect(host="

localhost

", user="

root

", password="

root

", database="

db6"

) cur = conn.cursor(cursor=pymysql.cursors.dictcursor)

cur.execute(sql,args)

conn.commit()

cur.close()

conn.close()

return

cur.lastrowid

#python插入記錄後取得主鍵id的方法(cursor.lastrowid和conn.insert_id())

#增 刪 改 都要提交 commit

#批量加入 以元祖的形式傳引數 就是新增幾次次提交一次

defmul_mode(sql, args):

conn = pymysql.connect(host="

localhost

", user="

root

", password="

root

", database="

db6"

) cur = conn.cursor(cursor=pymysql.cursors.dictcursor)

#self.cursor.executemany("insert into user (id,name) values (%s,%s)",[(1,"aaa"),(2,"bbb"),(3,"ccc")]) 傳參方式

cur.executemany(sql, args)

conn.commit()

cur.close()

conn.close()

import

pymysql

#注意 args 引數可以傳空值

class

mysqls(object):

def__init__

(self):

#讀取配置檔案

self.connect()

defconnect(self):

self.conn = pymysql.connect(host="

localhost

", user="

root

", password="

root

", database="

db6") #

可以把主機連線等寫入配置檔案 等

self.cursor=self.conn.cursor(cursor=pymysql.cursors.dictcursor)

#獲取所以資料

defget_all(self,sql,args):

self.cursor.execute(sql,args)

res =self.cursor.fetchall()

return

res

#獲取一行資料

defget_one(self,sql,args):

self.cursor.execute(sql, args)

res =self.cursor.fetchone()

return

res

#新增 就是新增一次提交多次

defget_mode (self,sql,args):

self.cursor.execute(sql, args)

self.conn.commit()

#新增並且帶返回值

defget_create(self,sql,args):

self.cursor.execute(sql,args)

self.conn.commit()

return

self.cursor.lastrowid

#python插入記錄後取得主鍵id的方法(cursor.lastrowid和conn.insert_id())

#批量加入 以元祖的形式傳引數 就是新增一次提交一次

defmul_mode(self, sql, args):

#self.cursor.executemany("insert into user (id,name) values (%s,%s)",[(1,"aaa"),(2,"bbb"),(3,"ccc")]) 傳參方式

self.cursor.executemany(sql, args)

self.conn.commit()

defget_close(self):

self.cursor.close()

self.conn.close()

python 資料庫的封裝

coding utf 8 import os import sqlite3 defjudge sql sql 判斷sql型別 sql sql.strip lower ret fetch if sql.startswith select select pass elif sql.startswith ...

python資料庫封裝的辦法

匯入pymysql 框架,可以用 pipimport pymysql 資料庫封裝方法 class mysqlhelp object config def init self self.connection none self.cursor none 該函式用來從資料庫表中查詢一行資料 def get...

python 對ACCESS資料庫操作封裝模組

coding utf 8 by adengou 2016 01 02 programe python3.4.4 import os import json import win32com.client access資料模組 class access model def init self,datau...