python3.4用函数操作mysql5.7数据库
#!/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('', 'zbl5', 'man')
zblmysql.queryMysql()
zblmysql.deleteMysql(id=2)
zblmysql.queryMysql()
zblmysql.updateMysqlSN(name='zbl5',sex='woman')
zblmysql.queryMysql()
zblmysql.closeMysql()
参考:http://www.cnblogs.com/tkinter/p/5632312.html
很好玩!
python3.4用函数操作mysql5.7数据库的更多相关文章
- Python3.x:SQLAlchemy操作数据库
Python3.x:SQLAlchemy操作数据库 前言 SQLAlchemy是一个ORM框架(Object Rational Mapping,对象关系映射),它可以帮助我们更加优雅.更加高效的实现数 ...
- 【简说Python WEB】视图函数操作数据库
目录 [简说Python WEB]视图函数操作数据库 系统环境:Ubuntu 18.04.1 LTS Python使用的是虚拟环境:virutalenv Python的版本:Python 3.6.9 ...
- SQL Server学习之路(七):Python3操作SQL Server数据库
0.目录 1.前言 2.准备工作 3.简单测试语句 4.提交与回滚 5.封装成类的写法 1.前言 前面学完了SQL Server的基本语法,接下来学习如何在程序中使用sql,毕竟不能在程序中使用的话, ...
- 数据库MySQL之 视图、触发器、存储过程、函数、事务、数据库锁、数据库备份、事件
数据库MySQL之 视图.触发器.存储过程.函数.事务.数据库锁.数据库备份.事件 浏览目录 视图 触发器 存储过程 函数 事务 数据库锁 数据库备份 事件 一.视图 1.视图概念 视图是一个虚拟表, ...
- centos环境下使用percona-xtrabackup对mysql5.6数据库innodb和myisam进行快速备份及恢复
centos环境下使用percona-xtrabackup对mysql5.6数据库innodb和myisam进行快速备份及恢复 有时候我们会碰到这样的业务场景: 1.将大的数据库恢复到本地进行业务测试 ...
- MySQL 之 视图、触发器、存储过程、函数、事物与数据库锁
浏览目录: 1.视图 2.触发器 3.存储过程 4.函数 5.事物 6.数据库锁 7.数据库备份 1.视图 视图:是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据 视 ...
- python3内置函数大全(顺序排列)
python3内置函数大全 内置函数 (1)abs(), 绝对值或复数的模 1 print(abs(-6))#>>>>6 (2)all() 接受一个迭代器,如果迭代器的所有 ...
- python3内置函数大全
由于面试的时候有时候会问到python的几个基本内置函数,由于记不太清,就比较难受,于是呕心沥血总结了一下python3的基本内置函数 Github源码: https://github. ...
- mysql5.6数据库双机热备、主从备份
主题:mysql5.6数据库双机热备.主从备份 缘由: 在Web应用系统中,数据库性能是导致系统性能瓶颈最主要的原因之一.尤其是在大规模系统中,数据库集群已经成为必备的配置之一.集群的好处主要有:查询 ...
随机推荐
- MySQL-查缺补漏
MySQL show create table tablename #查看表设计代码数据 show create table tablename/G #同上 desc tablename # 查看表数 ...
- ubuntu系列-安装google浏览器
转载:http://www.linuxidc.com/Linux/2013-10/91857.htm 对于刚刚开始使用Ubuntu并想安装谷歌Chrome浏览器的新用户来说,本文所介绍的方法是最快捷的 ...
- LWP::UserAgent的用法
LWP::UserAgent是一个模拟用户浏览器的类,在使用的时候需要遵守以下几步: 1.引入模块 2.创建一个LWP::UserAgent的对象 3.设置这个对象的相关参数 4.创建HTTP::Re ...
- 同种类型不同名字的变量在for循环中操作
InfoViewController * info = [[InfoViewController alloc] init]; HeroViewController * hero = [[HeroVie ...
- (转载)Ubuntu下安装Qt
转载自:http://blog.csdn.net/timidsmile/article/details/7716201 Ubuntu下安装Qt 1: sudo apt-get install qt4- ...
- Debian 中文环境设置
编辑 /etc/apt/sources.list 添加163镜像源 apt-get update 进行更新 dpkg-reconfigure locales 选择 en_US.utf-8 utf-8 ...
- 故障处理分析:华为5885v3 cable/ Interconnect (LEFT Panel)
故障现象: 处理结果: 1.重新把插左前面板,重启,故障消失.
- C# 解决SharpSvn启动窗口报错 Unable to connect to a repository at URL 'svn://....'
在远程机打开sharpsvn客户端测试,结果报错 Svn启动窗口报错 Unable to connect to a repository at URL 'svn://...' 咋整,我在win10我的 ...
- (转)JavaScript escape() 函数(该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。)
JavaScript escape() 函数 JavaScript 全局对象参考手册 定义和用法 escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串. 语法 escape ...
- hdu 4717(三分) The Moving Points
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 n个点,给出初始坐标和移动的速度和移动的方向,求在哪一时刻任意两点之间的距离的最大值的最小. 对于最 ...