最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫(kù)

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 14:23:50
文檔

python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫(kù)

python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫(kù): 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # __author__ = blzhu 4 5 python study 6 Date:2017 7 8 # -*- coding: utf-8 -*- 9 __author__ = 'djstava@gmail.
推薦度:
導(dǎo)讀python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫(kù): 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # __author__ = blzhu 4 5 python study 6 Date:2017 7 8 # -*- coding: utf-8 -*- 9 __author__ = 'djstava@gmail.

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # __author__ = "blzhu"
 4 """
 5 python study
 6 Date:2017
 7 """
 8 # -*- coding: utf-8 -*-
 9 __author__ = 'djstava@gmail.com'
10 
11 import logging
12 import pymysql
13 
14 
15 class MySQLCommand(object):
16 def __init__(self, host, port, user, passwd, db, table, charset):
17 self.host = host
18 self.port = port
19 self.user = user
20 self.password = passwd
21 self.db = db
22 self.table = table
23 self.charset = charset
24 
25 def connectMysql(self):
26 try:
27 self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password,
28 db=self.db, charset=self.charset)
29 self.cursor = self.conn.cursor()
30 print('connect ' + self.table + ' correctly!')
31 except:
32 print('connect mysql error.')
33 
34 def queryMysql(self):
35 sql = "SELECT * FROM " + self.table
36 
37 try:
38 print("query Mysql:")
39 self.cursor.execute(sql)
40 #row = self.cursor.fetchone()
41 for d in self.cursor:
42 print(str(d[0]), str(d[1]), str(d[2]))
43 # print(row)
44 
45 except:
46 print(sql + ' execute failed.')
47 
48 def insertMysql(self, id, name, sex):
49 sql = "INSERT INTO " + self.table + " VALUES(" + id + "," + "'" + name + "'," + "'" + sex + "')"
50 try:
51 print("insert Mysql:")
52 self.cursor.execute(sql)
53 print(sql)
54 except:
55 print("insert failed.")
56 
57 def updateMysqlSN(self, name, sex):
58 sql = "UPDATE " + self.table + " SET sex='" + sex + "'" + " WHERE name='" + name + "'"
59 print("update sn:" + sql)
60 
61 try:
62 self.cursor.execute(sql)
63 self.conn.commit()
64 except:
65 self.conn.rollback()
66 
67 def deleteMysql(self, id): # 刪除
68 sql = "DELETE FROM %s WHERE id='%s'" % (self.table,id)
69 #"delete from student where zid='%s'" % (id)
70 try:
71 self.cursor.execute(sql)
72 print(sql)
73 self.conn.commit()
74 print("delete the " + id + "th row successfully!")
75 except:
76 print("delete failed!")
77 self.conn.rollback()
78 
79 def closeMysql(self):
80 self.conn.commit() # 不執(zhí)行此句,所作的操作不會(huì)寫(xiě)入到數(shù)據(jù)庫(kù)中
81 self.cursor.close()
82 self.conn.close()
83 
84 
85 if __name__ == '__main__':
86 zblmysql = MySQLCommand(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, table='student2',
87 charset='utf8')
88 zblmysql.connectMysql()
89 zblmysql.queryMysql()
90 zblmysql.insertMysql('5', 'zbl5', 'man')
91 zblmysql.queryMysql()
92 zblmysql.deleteMysql(id=2)
93 zblmysql.queryMysql()
94 zblmysql.updateMysqlSN(name='zbl5',sex='woman')
95 zblmysql.queryMysql()
96 zblmysql.closeMysql()

參考:

很好玩!

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

文檔

python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫(kù)

python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫(kù): 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # __author__ = blzhu 4 5 python study 6 Date:2017 7 8 # -*- coding: utf-8 -*- 9 __author__ = 'djstava@gmail.
推薦度:
  • 熱門(mén)焦點(diǎn)

热门图文

猜你喜歡

專(zhuān)題
Top
fffffffffffff

抖音扫码关注

手机端二维码

每天分享百科知识!