老王的python筆記之pymysql

2021-09-27 12:40:47 字數 4462 閱讀 9829

pymysql連線資料庫的基礎封裝類。

class

conndb

:def

__init__

(self, dbinfo)

: self.db, self.user, self.pwd, self.host, port = dbinfo

self.port =

int(port)

def__getconnect

(self):if

not self.db:

raise

(nameerror,

"沒有設定資料庫資訊"

) self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.pwd, database=self.db,

charset=

"utf8"

, connect_timeout=30)

cur = self.conn.cursor()if

not cur:

raise

(nameerror,

"連線資料庫失敗"

)else

:return cur

defexecquery

(self, sql)

: cur = self.__getconnect(

) cur.execute(sql.encode(

"utf8"))

res = cur.fetchall(

) re = cur.description

self.conn.close(

) a =

for b in re:0]

) c =

[a, res]

return c

defexecnonquery

(self, sql)

: cur = self.__getconnect(

) res = cur.execute(sql.encode(

"utf8"))

self.conn.commit(

) self.conn.close(

)return res

defexecinsert

(self, sql)

: cur = self.__getconnect(

) cur.execute(sql.encode(

"utf8"))

res = self.conn.insert_id(

) self.conn.commit(

) self.conn.close(

)return res

defmanyexecnonquery

(self, sql, args)

: cur = self.__getconnect(

)try

: cur.executemany(sql.encode(

"utf8"

), args)

self.conn.commit(

)except exception as e:

print

'執行sql:%s時出錯:%s'

%(sql, e)

self.conn.close(

)def

execqueryforpd

(self,sql)

: conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.pwd, database=self.db,

charset=

"utf8"

, connect_timeout=30)

df = pd.read_sql(sql, con=conn)

conn.close(

)return df

使用資料庫的方法

class

laowangdb

:def

__init__

(self, dbinfo)

: self.ms = conndb(dbinfo)

self.dbinfo = dbinfo

defquery

(self, sql)

:#查詢

return self.ms.execquery(sql)

defupdate

(self, sql)

:#修改

return self.ms.execnonquery(sql)

definsert

(self, sql)

:#插入

return self.ms.execinsert(sql)

defmanyupdate

(self, sql, arg)

:#批量更新

return self.ms.manyexecnonquery(sql, arg)

defdata2dict

(self, res)

:#查詢資料格式化,元組轉字典

dicts =

title, data = res

if data:

len_title =

len(title)

for dat in data:

tem =

for i in

range

(len_title)

: tem[title[i]

]= dat[i]

return dicts

else

:return

defbig_manyupdate

(self, sql, arg)

:#大批量更新,使用多執行緒的形式

per_block =

1000

arg_list =

[arg[i:i + per_block]

for i in

range(0

,len

(arg)

, per_block)

] names =

locals()

for i, a in

enumerate

(arg_list)

: conn = laowangdb(self.dbinfo)

names[

't'+

str(i)

]= threading.thread(target=conn.manyupdate, args=

(sql, a)

) names[

't'+

str(i)

].start(

)def

big_manyupdate_wait

(self, sql, arg)

:#大批量更新,阻塞執行緒

per_block =

1000

arg_list =

[arg[i:i + per_block]

for i in

range(0

,len

(arg)

, per_block)

] names =

locals()

for i, a in

enumerate

(arg_list)

: conn = laowangdb(self.dbinfo)

names[

't'+

str(i)

]= threading.thread(target=conn.manyupdate, args=

(sql, a)

) names[

't'+

str(i)

].start(

)for i, a in

enumerate

(arg_list)

: names[

't'+

str(i)

].join(

)

最終的呼叫:

dbinfo=

['資料庫名稱'

,'使用者名稱'

,'密碼'

,'伺服器位址'

,埠號]

db=laowangdb(db_info)

sql=

'''select * from test;'''

res=db.query(sql)

print res

Python 之 老王開槍 練習

知識點 這篇部落格是對前面python基礎的乙個複習,更應該學到的是如何設計一系列的動作,或者說是設計程式 1.分析有哪些類,並且建立 人 槍 彈夾 子彈 2.物件的建立 3.各個類的屬性設定 4.類的方法設計,由里向外 1 人拿槍 eg 1彈夾裝子彈 2 槍裝彈夾 3.人拿槍 2 人開槍 eg.1...

python基礎練習之老王開槍

class people object def init self,name self.name name self.gun none self.hp 100 def load zidan self,dianjia temp,zidan temp 把子彈裝進彈夾 dianjia temp.anzhu...

Python單繼承的使用 豐富老王開槍例子

class ren def init self self.xueliang 100 self.qiang none def str self msg 當前血量為 format self.xueliang return msg defanzidan self,danjia,zidan danjia.b...