国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python3.4用函數操作mysql5.7數據庫

2020-02-16 01:45:37
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python3.4函數操作mysql數據庫的具體代碼,供大家參考,具體內容如下

#!/usr/bin/env python# -*- coding:utf-8 -*-# __author__ = "blzhu""""python studyDate:2017"""# -*- coding: utf-8 -*-__author__ = 'djstava@gmail.com'import loggingimport pymysqlclass MySQLCommand(object): def __init__(self, host, port, user, passwd, db, table, charset): self.host = host self.port = port self.user = user self.password = passwd self.db = db self.table = table self.charset = charset def connectMysql(self): try: self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password,   db=self.db, charset=self.charset) self.cursor = self.conn.cursor() print('connect ' + self.table + ' correctly!') except: print('connect mysql error.') def queryMysql(self): sql = "SELECT * FROM " + self.table try: print("query Mysql:") self.cursor.execute(sql) #row = self.cursor.fetchone() for d in self.cursor: print(str(d[0]), str(d[1]), str(d[2])) # print(row) except: print(sql + ' execute failed.') def insertMysql(self, id, name, sex): sql = "INSERT INTO " + self.table + " VALUES(" + id + "," + "'" + name + "'," + "'" + sex + "')" try: print("insert Mysql:") self.cursor.execute(sql) print(sql) except: print("insert failed.") def updateMysqlSN(self, name, sex): sql = "UPDATE " + self.table + " SET sex='" + sex + "'" + " WHERE name='" + name + "'" print("update sn:" + sql) try: self.cursor.execute(sql) self.conn.commit() except: self.conn.rollback() def deleteMysql(self, id): # 刪除 sql = "DELETE FROM %s WHERE id='%s'" % (self.table,id) #"delete from student where zid='%s'" % (id) try: self.cursor.execute(sql) print(sql) self.conn.commit() print("delete the " + id + "th row successfully!") except: print("delete failed!") self.conn.rollback() def closeMysql(self): self.conn.commit() # 不執行此句,所作的操作不會寫入到數據庫中 self.cursor.close() self.conn.close()if __name__ == '__main__': zblmysql = MySQLCommand(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, table='student2',  charset='utf8') zblmysql.connectMysql() zblmysql.queryMysql() zblmysql.insertMysql('5', 'zbl5', 'man') zblmysql.queryMysql() zblmysql.deleteMysql(id=2) zblmysql.queryMysql() zblmysql.updateMysqlSN(name='zbl5',sex='woman') zblmysql.queryMysql() zblmysql.closeMysql()

參考:python3操作mysql數據庫的方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屯昌县| 哈巴河县| 东兴市| 宝清县| 乌兰察布市| 三河市| 新和县| 长泰县| 安西县| 广灵县| 怀远县| 高唐县| 汕头市| 重庆市| 屏东县| 邵东县| 恩施市| 玉环县| 牙克石市| 平果县| 锡林郭勒盟| 民丰县| 聊城市| 静安区| 麻江县| 房产| 鸡西市| 蚌埠市| 陈巴尔虎旗| 涿鹿县| 新绛县| 五华县| 黄浦区| 靖远县| 泸水县| 徐汇区| 黑龙江省| 额敏县| 万载县| 井研县| 道真|