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

作者:猪冰龙 时间:2024-01-13 01:08:40 

本文实例为大家分享了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,mysql,mysql5.7
0
投稿

猜你喜欢

  • 使用Python编写Linux系统守护进程实例

    2022-06-09 16:50:52
  • seo网站如何实现301跳转?

    2010-01-15 12:59:00
  • 利用JavaScript做数独的完整实现过程

    2024-02-24 02:10:47
  • python中栈的原理及实现方法示例

    2023-05-01 02:54:33
  • python 爬取英雄联盟皮肤并下载的示例

    2023-07-22 09:57:45
  • python turtle绘图命令及案例

    2022-04-29 10:26:58
  • golang 调用c语言动态库方式实现

    2024-05-22 17:51:02
  • pytorch中如何设置随机种子

    2021-10-24 06:43:51
  • Python就将所有的英文单词首字母变成大写

    2023-09-21 10:44:35
  • Linux环境下安装mysql5.7.36数据库教程

    2024-01-19 15:42:02
  • MySQL窗口函数实现榜单排名

    2024-01-16 20:22:22
  • Oracle计算时间差常用函数

    2024-01-22 11:08:32
  • Pytorch 图像变换函数集合小结

    2022-06-14 08:52:09
  • Python基于csv模块实现读取与写入csv数据的方法

    2023-04-12 23:14:34
  • 20分钟成功编写bootstrap响应式页面 就这么简单

    2023-08-12 06:12:13
  • PHP set_time_limit(0)长连接的实现分析

    2023-11-06 11:46:20
  • Python基础之值传递和引用传递详解

    2023-06-16 08:53:35
  • 出现“不能执行已释放的Script代码”错误的原因及解决办法

    2024-04-19 10:02:13
  • 对Python subprocess.Popen子进程管道阻塞详解

    2022-10-30 07:59:49
  • 彻底搞懂python 迭代器和生成器

    2021-09-14 05:00:26
  • asp之家 网络编程 m.aspxhome.com