网上好多的帖子感觉比较老了,而且千篇一律。我到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操作的更多相关文章

  1. python连接mysql操作(1)

    python连接mysql操作(1) import pymysql import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host='10 ...

  2. python对Mysql操作和使用ORM框架(SQLAlchemy)

    python对mysql的操作 Mysql 常见操作 数据库操作 创建数据库 create database fuzjtest 删除数据库 drop database fuzjtest 查询数据库 s ...

  3. Ubuntu中python的mysql操作

    1.在已经安装了python和MySQL数据库的前提下使用pip3 install PyMySQL命令 2. 建立链接: (1)首先使用命令python 进入编程模式,再导入包: import pym ...

  4. python数据库(mysql)操作

    一.软件环境 python环境默认安装了sqlite3,如果需要使用sqlite3我们直接可以在python代码模块的顶部使用import sqlite3来导入该模块.本篇文章我是记录了python操 ...

  5. Python 之 MySQL 操作库 lazy_mysql

    TOC Intro Installation Tutorial API Engine Pool Column Table Intro lazy_mysql 是一个非常简单易用,用来操作 MySQL 的 ...

  6. Python之MySQL操作及Paramiko模块操作

    一.MySQL简介   MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQ ...

  7. Python之MySql操作

    1.安装驱动 输入命令:pip install MySQL-python 2.直接使用驱动 #coding=utf-8 import MySQLdb conn= MySQLdb.connect( ho ...

  8. python学习道路(day12note)(mysql操作,python链接mysql,redis)

    1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...

  9. Python 第九篇:队列Queue、生产者消费者模型、(IO/异步IP/Select/Poll/Epool)、Mysql操作

    Mysql操作: grant select,insert,update,delete on *.* to root@"%" Identified by "123456&q ...

随机推荐

  1. git 删除追踪状态

    当不小心添加一个不想被git记录等文件时,这个时候就算将该文件记录在了.gitignore里也是没有用的,因为那个文件已经被git记录过了,只有那些从来没有被git记录过的文件(即:自添加进项目后,从 ...

  2. POJ2653判断线段相交

    POJ2653 题目大意:按顺序放木棒,问最后所有的木棒中上面没有木棒的木棒的索引是…… 思路:按理说线段相交的题目做的听多了,这个应该不算新鲜,但是这个题,还是让我学到了认真读题,面对这个题很容易想 ...

  3. node API assert

    1.assert.throws(block, [error], [message]): assert.throws( function(){ throw new Error('wrong'); }, ...

  4. jquery ui widgets-datepicker

    jquery ui的用法就不在此讲述,直接进入jquery ui的窗体小部件(widgets)——datepicker. 相信很多像我这样子的菜鸟少年,如果同一个页面上有两个input文本输入框是用来 ...

  5. R12_专题知识总结提炼-AP模块

    应付模块业务操作流程 供应商管理 供应商概述 在您使用 Oracle Purchasing 之前,需要定义供应商.供应商site,以及供应商联系人,  供应商主数据(SUPPLIER MASTER D ...

  6. DelphiXE10.2怎么安装文本转语音(TTS)语音转文本(SR)控件(XE10.2+WIN764)

    关资料: http://edn.embarcadero.com/article/29583 http://blog.sina.com.cn/s/blog_53866d7501017r1o.html 问 ...

  7. matlab 降维工具箱

    Matlab Toolbox for Dimensionality Reduction   降维方法包括: Principal Component Analysis (PCA) • Probabili ...

  8. Mina Session

    Chapter 4 - Session The Session is at the heart of MINA : every time a client connects to the server ...

  9. linux系统编程:setjmp和longjmp函数用法

    #include <stdio.h> #include <setjmp.h> //jmp_buf:数组,保存栈信息即运行环境 jmp_buf buf; double Divid ...

  10. 【Kindeditor编辑器】 文件上传、空间管理

    包括图片上传.文件上传.Flash上传.多媒体上传.空间管理(图片空间.文件空间等等) 一.编辑器相关参数 二.简单的封装类 这里只是做了简单的封装,欢迎大家指点改正. public class Ki ...