Python的Mysql操作
网上好多的帖子感觉比较老了,而且千篇一律。我到mysql看了一下官网上python驱动的操作,发现与大部分网站说的都不一样。
首先安装的驱动是:
pip install mysql-connector-python
上面是在ubuntu上的命令。
安装之后,开发的样例代码如下:
from __future__ import print_function
from decimal import Decimal
from datetime import datetime, date, timedelta
import mysql.connector
# Connect with the MySQL Server
cnx = mysql.connector.connect(user='scott', database='employees')
# Get two buffered cursors
curA = cnx.cursor(buffered=True)
curB = cnx.cursor(buffered=True)
# Query to get employees who joined in a period defined by two dates
query = (
"SELECT s.emp_no, salary, from_date, to_date FROM employees AS e "
"LEFT JOIN salaries AS s USING (emp_no) "
"WHERE to_date = DATE('9999-01-01')"
"AND e.hire_date BETWEEN DATE(%s) AND DATE(%s)")
# UPDATE and INSERT statements for the old and new salary
update_old_salary = (
"UPDATE salaries SET to_date = %s "
"WHERE emp_no = %s AND from_date = %s")
insert_new_salary = (
"INSERT INTO salaries (emp_no, from_date, to_date, salary) "
"VALUES (%s, %s, %s, %s)")
# Select the employees getting a raise
curA.execute(query, (date(2000, 1, 1), date(2000, 12, 31)))
# Iterate through the result of curA
for (emp_no, salary, from_date, to_date) in curA:
# Update the old and insert the new salary
new_salary = int(round(salary * Decimal('1.15')))
curB.execute(update_old_salary, (tomorrow, emp_no, from_date))
curB.execute(insert_new_salary,
(emp_no, tomorrow, date(9999, 1, 1,), new_salary))
# Commit the changes
cnx.commit()
cnx.close()
Python的Mysql操作的更多相关文章
- python连接mysql操作(1)
python连接mysql操作(1) import pymysql import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host='10 ...
- python对Mysql操作和使用ORM框架(SQLAlchemy)
python对mysql的操作 Mysql 常见操作 数据库操作 创建数据库 create database fuzjtest 删除数据库 drop database fuzjtest 查询数据库 s ...
- Ubuntu中python的mysql操作
1.在已经安装了python和MySQL数据库的前提下使用pip3 install PyMySQL命令 2. 建立链接: (1)首先使用命令python 进入编程模式,再导入包: import pym ...
- python数据库(mysql)操作
一.软件环境 python环境默认安装了sqlite3,如果需要使用sqlite3我们直接可以在python代码模块的顶部使用import sqlite3来导入该模块.本篇文章我是记录了python操 ...
- Python 之 MySQL 操作库 lazy_mysql
TOC Intro Installation Tutorial API Engine Pool Column Table Intro lazy_mysql 是一个非常简单易用,用来操作 MySQL 的 ...
- Python之MySQL操作及Paramiko模块操作
一.MySQL简介 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQ ...
- Python之MySql操作
1.安装驱动 输入命令:pip install MySQL-python 2.直接使用驱动 #coding=utf-8 import MySQLdb conn= MySQLdb.connect( ho ...
- python学习道路(day12note)(mysql操作,python链接mysql,redis)
1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...
- Python 第九篇:队列Queue、生产者消费者模型、(IO/异步IP/Select/Poll/Epool)、Mysql操作
Mysql操作: grant select,insert,update,delete on *.* to root@"%" Identified by "123456&q ...
随机推荐
- OpenGl 坐标转换 (转载)
OpenGl 坐标转换 (转载) 1. OpenGL 渲染管线 OpenGL渲染管线分为两大部分,模型观测变换(ModelView Transformation)和投影变换(Projection Tr ...
- 读《深入理解Windows Phone 8.1 UI控件编程》1.4.3 框架的应用示例:自定义弹出窗口有感
前些天买了园子里林政老师的两本 WP8.1 的书籍.毕竟想要学得深入的话,还是得弄本书跟着前辈走的. 今天读到 1.4.3 节——框架的应用示例:自定义弹出窗口这一小节.总的来说,就是弄一个像 Mes ...
- Jersey构建Restful风格的Webserivces(三)
一.总体说明 通过jersey-client接口,创建客户端程序,来调用Jersey实现的RESTful服务,实现增.删.改.查等操作. 服务端主要是通过内存的方式,来模拟用户的增加.删除.修改.查询 ...
- linux系统编程之信号(一):中断与信号
一,什么是中断? 1.中断的基本概念 中断是指计算机在执行期间,系统内发生任何非寻常的或非预期的急需处理事件,使得CPU暂时中断当前正在执行的程序而转去执行相应的事件处理程序,待处理完毕后又返回原来被 ...
- BitAdminCore框架应用篇:(一)使用Cookiecutter创建应用项目
框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookiecutter-bitadmin-core 一.简介 1.Coo ...
- Cordova - Windows版本图形界面管理工具,告别命令行输入方式!
Cordova本身提供的是命令行管理工具,并没有提供图形界面管理工具,虽然命令行管理工具可以完成所有Cordova管理,但是对于我这种懒蛋,可真不希望每次都输入命令,而且我更担心一旦输错一个字符,命令 ...
- C# __arglist 关键字
using System.Runtime.InteropServices; namespace Alpha { class Beta { [DllImport("msvcrt.dll&quo ...
- 902. Numbers At Most N Given Digit Set
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- Trie-648. Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- ng的点滴记录
1,directive http://damoqiongqiu.iteye.com/blog/1917971/ 2,constructor https://segmentfault.com/q/10 ...