Python——Mysql
一、导入
import pymysql
二、连接
def connect_wxremit_db():
return pymysql.connect(host='10.123.5.28',
port=3306,
user='root',
password='root1234',
database='db_name',
charset='latin1')
'''
hots:主机地址
port:端口号
user:用户
password:密码
database:数据库
charset:表
'''
三、查询
def query_country_name(cc2):
sql_str = ("SELECT Fcountry_name_zh"
+ " FROM t_country_code"
+ " WHERE Fcountry_2code='%s'" % (cc2))
logging.info(sql_str) #将内容写入日志 con = mysql_api.connect_wxremit_db() #执行连接数据库
cur = con.cursor() #使用cursor()方法获取操作游标
cur.execute(sql_str) #使用execute方法执行SQL语句
rows = cur.fetchall()#接收全部的返回结果行.
cur.close()
con.close() assert len(rows) == 1, 'Fatal error: country_code does not exists!'
return rows[0][0]
四、插入
def insert_file_rec(self, file_name, file_md5):
#连接数据库
con = mysql_api.connect_wxremit_db()
#获取操作游标
cur = con.cursor()
try:
#SQL语句
sql_str = ("INSERT INTO t_forward_file (Ffile_name, Ffile_md5)",
+ " VALUES ('%s', '%s')" % (file_name, file_md5))
#执行SQL语句
cur.execute(sql_str)
#提交到数据库
con.commit()
except:
#插入数据失败
con.rollback()
#写入日志
logging.exception('Insert operation error')
#当程序出现错误,通过raise显示地引发异常。后面的语句将不能执行
raise
finally:
cur.close()
con.close()
五、批量插入
remit_ids = [('', 'CAD'), ('', 'HKD')]
con = mysql_api.connect_wxremit_db()
cur = con.cursor()
try:
cur.executemany("INSERT INTO t_order (Fremit_id, Fcur_type, Fcreate_time"
+ " VALUES (%s, %s, now())", new_items)
assert cur.rowcount == len(remit_ids), 'my error message'
con.commit()
except Exception as e:
con.rollback()
logging.exception('Insert operation error')
finally:
cur.close()
con.close()
六、更新
def update_refund_trans(self, remit_id):
con = mysql_api.connect_wxremit_db()
cur = con.cursor()
try:
sql_str = ("SELECT Fremit_id"
+ " FROM t_wxrefund_trans"
+ " WHERE Fremit_id='%s'" % remit_id
+ " FOR UPDATE")
logging.info(sql_str) cur.execute(sql_str)
assert cur.rowcount == 1, 'Fatal error: The wx-refund record be deleted!' sql_str = ("UPDATE t_wxrefund_trans"
+ " SET Fcheck_amount_flag=1"
+ ", Fmodify_time=now()"
+ " WHERE Fremit_id='%s'" % remit_id
logging.info(sql_str)
cur.execute(sql_str) assert cur.rowcount == 1, 'The number of affected rows not equal to 1'
con.commit()
except:
con.rollback()
logging.exception('Update operation error')
raise
finally:
cur.close()
con.close()
七、删除
sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20)
Python——Mysql的更多相关文章
- Python—>Mysql—>Dbvisualizer
MySQLdb: https://pypi.python.org/pypi/MySQL-python/1.2.4 import MySQLdb 1.Download Connector/Python: ...
- Python Mysql 篇
Python 操作 Mysql 模块的安装 linux: yum install MySQL-python window: http://files.cnblogs.com/files/wupeiqi ...
- Python MySQL ORM QuickORM hacking
# coding: utf-8 # # Python MySQL ORM QuickORM hacking # 说明: # 以前仅仅是知道有ORM的存在,但是对ORM这个东西内部工作原理不是很清楚, ...
- python 之路,Day11(上) - python mysql and ORM
python 之路,Day11 - python mysql and ORM 本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...
- 树莓派安装ubuntu-server,配置镜像,安装python/mysql/samba记录
目标: 1/在raspberrypi 3B上安装ubuntu-server 2/配置好python/mysql/samba等服务,实现爬虫稳定运行我的硬件准备: 1/raspberrypi 3B 2/ ...
- Python/ MySQL练习题(一)
Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...
- python/MySQL练习题(二)
python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...
- Python/MySQL(一、基础)
Python/MySQL(一.基础) mysql: MYSQL : 是用于管理文件的一个软件 -socket服务端 (先启动) -本地文件操作 -解析 指令[SQL语句] -客户端软件 (各种各样的客 ...
- Python/MySQL(二、表操作以及连接)
Python/MySQL(二.表操作以及连接) mysql表操作: 主键:一个表只能有一个主键.主键可以由多列组成. 外键 :可以进行联合外键,操作. mysql> create table y ...
- Python/MySQL(三、pymysql使用)
Python/MySQL(三.pymysql使用) 所谓pymysql就是通过pycharm导入pymysql模块进行远程连接mysql服务端进行数据管理操作. 一.在pycharm中导入pymysq ...
随机推荐
- HTML中特殊符号
- java程序员学C#
因为工作需要,我要学习C#,其实我觉得不错,我喜欢了解更多的语言,因为这对我今后的发展很有帮助,毕竟技多不压身,下面是我今天学习后总结C#的基本语法: 我曾经学过C,而且又是java程序员,所以对我来 ...
- Selenium自动化测试 Verify
Selenium 找了很久,没有发现有verify的方法,可是,有个时候要的是确认,而不是直接断言, 当然要在运行结束后能够得到确认的结果,于是写了下面的代码,给大家分享. import ja ...
- vue HTTP 请求(vue-resource)
来自:https://www.cnblogs.com/lhl66/p/8022423.html 侵删 //初始化页面需要做什么事情 //点击后需要做什么事情 //鼠标.键盘.冒泡.默认行为等事件 // ...
- JHipster生成微服务架构的应用栈(一)- 准备工作
本系列文章演示如何用JHipster生成一个微服务架构风格的应用栈. 环境需求:安装好JHipster开发环境的CentOS 7.4(参考这里) 应用栈名称:appstack 认证微服务: uaa 业 ...
- 容器化系列 - Zookeeper启动和配置 on Docker
本文简要说明了如何在Docker容器中启动和配置Zookeeper. 1 准备工作 1.1 下载zookeeper镜像 $ docker pull zookeeper:3.4 1.2 单点模式 安装D ...
- 建立第一个SpringBoot小列子(碰到的错误)
当加入@SpringBootApplication注解时,无法得到解析 错误提示:SpringBootApplication cannot be resolved to a type 错误原因是因为s ...
- netstat Recv-Q和Send-Q
通过netstat -anp可以查看机器的当前连接状态: Active Internet connections (servers and established) Proto Recv-Q Se ...
- 前端请求参数MD5加密发送后台
最近在项目开发中遇到前端发送参数加密的问题,网上查找半天也是很乱,小编自己在项目开发中总结了一下,写到博客中,希望能够帮助大家. 查看所有代码可到我的github上查看源文件,下载后在控制台查看结果即 ...
- 【题解】P1171 售货员的难题
Tags 搜索,状压. 裸的旅行商问题 #include <stdio.h> #include <string.h> #define re register #define ...