//test.py

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import MySQLdb

# 打开数据库连接

db = MySQLdb.connect("localhost","root","11111111","TESTDB" )

# 使用cursor()方法获取操作游标

cursor = db.cursor()

# 使用execute方法执行SQL语句

cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取一条数据

data = cursor.fetchone()

print "Database version : %s " % data

cursor.execute('DROP TABLE IF EXISTS EMPLOYEE')

sql = """CREATE TABLE EMPLOYEE (

FIRST_NAME  CHAR(20) NOT NULL,

LAST_NAME  CHAR(20),

AGE INT,

SEX CHAR(1),

INCOME FLOAT )"""

cursor.execute(sql)

sql1 = """INSERT INTO EMPLOYEE(FIRST_NAME,

LAST_NAME, AGE, SEX, INCOME)

VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""

sql2 = """INSERT INTO EMPLOYEE(FIRST_NAME,

LAST_NAME, AGE, SEX, INCOME)

VALUES ('Jack', 'dansor', 25, 'F', 38000)"""

try:

cursor.execute(sql1)

cursor.execute(sql2)

db.commit()

except:

print "Rollback in case there is any error"

db.rollback()

sql3 = "SELECT * FROM EMPLOYEE \

WHERE INCOME>'%d'" % (1000)

try:

cursor.execute(sql3)

results = cursor.fetchall()

for row in results:

fname = row[0]

lname = row[1]

age = row[2]

sex = row[3]

income = row[4]

print 'fname:%s,lname=%s,age=%d,sex=%s,income=%d' \

% (fname,lname,age,sex,income)

except:

print 'Error: unable to fecth data'

sql4 = "UPDATE EMPLOYEE SET AGE=AGE+1 WHERE SEX='M'"

try:

cursor.execute(sql4)

db.commit()

except Exception, tup:

print 'Update failure: ', tup

db.rollback()

sql5 = "DELETE FROM EMPLOYEE WHERE SEX='F'"

try:

cursor.execute(sql5)

db.commit()

except Exception, tup:

print "Delete failure: ", tup

db.rollback()

try:

cursor.execute(sql3)

results = cursor.fetchall()

for row in results:

fname = row[0]

lname = row[1]

age = row[2]

sex = row[3]

income = row[4]

print 'fname:%s,lname=%s,age=%d,sex=%s,income=%d' \

% (fname,lname,age,sex,income)

except:

print 'Error: unable to fecth data'

# 关闭数据库连接

db.close()

//result

# python test.py
Database version : 5.5.58-0ubuntu0.14.04.1
fname:Mac,lname=Mohan,age=20,sex=M,income=2000
fname:Jack,lname=dansor,age=25,sex=F,income=38000
fname:Mac,lname=Mohan,age=21,sex=M,income=2000

Finally:

mysql数据库在python这样的脚本里非常容易使用,值得看看!

python mysql program的更多相关文章

  1. python selenium 测试环境的搭建及python mysql的连接

    又来一篇傻瓜教程啦,防止在学习的小伙伴们走弯路. 1.python 环境搭建 python官网:https://www.python.org/downloads/  选择最新版本python下载(如果 ...

  2. [MySQL Reference Manual] 4 MYSQL Program

    4 MYSQL Program 目录 4 MYSQL Program 4.3 MySQL Server和Server启动程序 4.3.1 mysqld 4.3.2 mysqld_safe 4.3.3 ...

  3. Python—>Mysql—>Dbvisualizer

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

  4. Python Mysql 篇

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

  5. Python MySQL ORM QuickORM hacking

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

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

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

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

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

  8. Python/ MySQL练习题(一)

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

  9. python/MySQL练习题(二)

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

随机推荐

  1. db2 reorg(转)

    DB2 reorg RUNSTATS: db2 connect to rmdb11 user rmadmin using rmadmin 对所有用户表执行runstats(reorgchk加updat ...

  2. nvidia-smi failed because it couldn't communicate with the nvidia driver

    Ubuntu装好CUDA之后过段时间提示NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. NV ...

  3. Some untracked working tree files would be overwritten by checkout. Please move or remove them before you can checkout. View them

    Some untracked working tree files would be overwritten by checkout. Please move or remove them befor ...

  4. 【每日一题】 UVA - 340 阅读理解+模拟

    https://cn.vjudge.net/problem/UVA-340 题目很难读,差不多读了两天 意思是给你一个n个数的数列,然后有m个询问,每个询问也是一个n个数的数列,让你输出两个数:一个是 ...

  5. Hive日志(Hive Logging)--hive GettingStarted翻译

    Hive uses log4j for logging. By default logs are not emitted to the console by the CLI. The default ...

  6. git add详解

    git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容修改(modified)以及新文件(new),但不包括被删除的文件. git add -u :他仅监控 ...

  7. create table test_create_table_CreateAs as select * from test_create_table; 表结构的破坏 复制字段结构 复制表结构 LIKE

    案例中: 索引丢失.分区丢失 实际测试 Target Server Type : MYSQLTarget Server Version : 50616File Encoding : 65001 Dat ...

  8. Java向服务端转身 系统平台所对应的机器语言 虚拟CPU的机器语言字节码 bytecode

    小结: 1.虚拟CPU的模拟器:java虚拟机 JVM Java将虚拟机(VM)作为插件集成到浏览器中,将编译后的Java程序(Applet)在虚拟机上运行,这种技术 当初是为了增强浏览器的功能. J ...

  9. python-多线程等概念

    并发 & 并行 并发:是指系统具有处理多个任务的能力 并行:是指系统具有 同时 处理多个任务的能力 并行 是  并发的一个子集 同步 & 异步 同步:当进程执行到一个I/O(等待外部数 ...

  10. [daily] 主机间目录共享

    1. 安装 nfs工具 [root@D128 j]# yum install nfs-utils 2. 修改配置文件: 1.  查看一下语法. [root@D128 j]# man exports 2 ...