一、导入  

        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的更多相关文章

  1. Python—>Mysql—>Dbvisualizer

    MySQLdb: https://pypi.python.org/pypi/MySQL-python/1.2.4 import MySQLdb 1.Download Connector/Python: ...

  2. Python Mysql 篇

    Python 操作 Mysql 模块的安装 linux: yum install MySQL-python window: http://files.cnblogs.com/files/wupeiqi ...

  3. Python MySQL ORM QuickORM hacking

    # coding: utf-8 # # Python MySQL ORM QuickORM hacking # 说明: # 以前仅仅是知道有ORM的存在,但是对ORM这个东西内部工作原理不是很清楚, ...

  4. python 之路,Day11(上) - python mysql and ORM

    python 之路,Day11 - python mysql and ORM   本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...

  5. 树莓派安装ubuntu-server,配置镜像,安装python/mysql/samba记录

    目标: 1/在raspberrypi 3B上安装ubuntu-server 2/配置好python/mysql/samba等服务,实现爬虫稳定运行我的硬件准备: 1/raspberrypi 3B 2/ ...

  6. Python/ MySQL练习题(一)

    Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...

  7. python/MySQL练习题(二)

    python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...

  8. Python/MySQL(一、基础)

    Python/MySQL(一.基础) mysql: MYSQL : 是用于管理文件的一个软件 -socket服务端 (先启动) -本地文件操作 -解析 指令[SQL语句] -客户端软件 (各种各样的客 ...

  9. Python/MySQL(二、表操作以及连接)

    Python/MySQL(二.表操作以及连接) mysql表操作: 主键:一个表只能有一个主键.主键可以由多列组成. 外键 :可以进行联合外键,操作. mysql> create table y ...

  10. Python/MySQL(三、pymysql使用)

    Python/MySQL(三.pymysql使用) 所谓pymysql就是通过pycharm导入pymysql模块进行远程连接mysql服务端进行数据管理操作. 一.在pycharm中导入pymysq ...

随机推荐

  1. npm run dev 启动错误:Module build failed: Error: No PostCSS Config found in:xxxxxxxxxxxxxx

    解决办法:在根目录新建postcss.config.js module.exports = { plugins: { 'autoprefixer': {browsers: 'last 5 versio ...

  2. HTML和CSS前端教程03-CSS选择器

    目录 1. CSS定义 2. 创建CSS的三种方法 2.1. 元素内嵌(权重最高) 2.2. 文档内嵌 2.3. 外部引用 3. CSS层叠和继承 3.1. 浏览器样式 3.2. 样式表层叠 3.3. ...

  3. 【20190405】JavaScript-正则式匹配与替换结果解析

    在正则式的应用中有三个函数使用得最多:exec().test()与字符串的replace(reg, options).其中test()最简单,只要字符串与正则式可以匹配,就返回true,否则返回fal ...

  4. Dynamics 365中自定义工作流活动获取的上下文分析及注意事项

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复244或者20170306可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  5. webmagic 基本的方法

    WebMagic的结构分为Downloader.PageProcessor.Scheduler.Pipeline四大组件,并由Spider将它们彼此组织起来.这四大组件对应爬虫生命周期中的下载.处理. ...

  6. ASP.NET Zero--解决方案结构(层)

    解决方案结构(层) 创建和下载项目后,您将具有如下所示的解决方案结构: 解决方案有8个项目: Core项目包含域层类(如 实体 和 域服务). Application项目包含应用程序逻辑(如应用程序服 ...

  7. jQuery如何制作动画

    下面为一组图片(四张)展示 1 2 3 4 页面代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  8. SQLServer数据库差异备份

    差异备份 (differential backup)定义 一种数据备份,基于完整数据库或部分数据库或一组数据文件或文件组(差异基准)的最新完整备份,并且仅包含自确定差异基准以来发生更改的数据. 使用S ...

  9. php分页实现

    <?php header("content-type:text/html;charset=utf8"); include 'conn.php'; //每页显示的数据条数 $p ...

  10. ASP.NET MVC学习系列(4)——MVC过滤器FilterAttribute

    1.概括 MVC提供的几种过滤器其实也是一种特性(Attribute),MVC支持的过滤器类型有四种,分别是:AuthorizationFilter(授权),ActionFilter(行为),Resu ...