python3.4用函数操作mysql5.7数据库

yipeiwu_com6年前Python基础

本文实例为大家分享了python3.4函数操作mysql数据库的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "blzhu"
"""
python study
Date:2017
"""
# -*- coding: utf-8 -*-
__author__ = 'djstava@gmail.com'

import logging
import pymysql


class 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数据库的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python tkinter label 更新方法

Python tkinter label 更新方法

网上看的两个例子关于tkinter界面更新的,简单易懂,分享一下。 例子_1: 代码_1: from tkinter import Tk, Checkbutton, Label f...

django rest framework vue 实现用户登录详解

django rest framework vue 实现用户登录详解

后端代码就不介绍了,可以参考 django rest framework 实现用户登录认证 这里介绍一下前端代码,和前后端的联调过程 在components下新建login.vue 文件...

跟老齐学Python之集合(set)

回顾一下已经了解的数据类型:int/str/bool/list/dict/tuple 还真的不少了. 不过,python是一个发展的语言,没准以后还出别的呢.看官可能有疑问了,出了这么多...

Python cookbook(数据结构与算法)通过公共键对字典列表排序算法示例

本文实例讲述了Python通过公共键对字典列表排序算法。分享给大家供大家参考,具体如下: 问题:想根据一个或多个字典中的值来对列表排序 解决方案:利用operator模块中的itemge...

使用Python实现从各个子文件夹中复制指定文件的方法

之前用来整理图片的小程序,拿来备忘,算是使用Python复制文件的一个例子。 # -*- coding: utf-8 -*- #程序用来拷贝文件并输出图片采集日期等其他信息到Exce...