Python学习系列(七)( 数据库编程)
import MySQLdb
#connect to a database 'test'
conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
cursor=conn.cursor()
#create a table
cursor.execute('create table \
test(ID int primary key auto_increment,Name char(25))')
#Closing database
cursor.close()
conn.close()
import MySQLdb
#connect to a database 'test'
conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
cursor=conn.cursor()
#fetch datas
n=cursor.execute('select * from test;')
r=cursor.fetchall()
print n,r
#Closing database
cursor.close()
conn.close() >>> ================================ RESTART ================================
>>>
3 ((4L, 'zhangbc'), (5L, 'lis08'), (6L, 'wangw'))
>>>

import MySQLdb
#connect to a database 'test'
conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
cursor=conn.cursor()
#insert data into table 'test'
mysql='''insert into test(id,sname) values(4,'zhanghua')'''
cursor.execute(mysql)
conn.commit()#below mysql5.0 needed
#fetch datas
n=cursor.execute('select * from test;')
r=cursor.fetchall()
print n,r
#Closing database
cursor.close()
conn.close() >>> ================================ RESTART ================================
>>>
4 ((1L, 'zhangbc'), (2L, 'lis'), (3L, 'wangw'), (4L, 'zhanghua'))


import MySQLdb
#connect to a database 'test'
conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
cursor=conn.cursor()
#update data of the table 'test'
mysql='''update test set sname='Lisi08' where id=2'''
cursor.execute(mysql)
conn.commit()#below mysql5.0 needed
#fetch datas
n=cursor.execute('select * from test;')
r=cursor.fetchall()
print n,r
#Closing database
cursor.close()
conn.close() >>> ================================ RESTART ================================
>>>
4 ((1L, 'zhangbc'), (2L, 'Lisi08'), (3L, 'wangw'), (4L, 'zhanghua'))
import MySQLdb
#connect to a database 'test'
conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
cursor=conn.cursor()
#delete data of the table 'test'
mysql='''delete from test where id=4'''
cursor.execute(mysql)
conn.commit()#below mysql5.0 needed
#fetch datas
n=cursor.execute('select * from test;')
r=cursor.fetchall()
print n,r
#Closing database
cursor.close()
conn.close() >>> ================================ RESTART ================================
>>>
3 ((1L, 'zhangbc'), (2L, 'Lisi08'), (3L, 'wangw'))
import MySQLdb
#connect to a database 'test'
conn=MySQLdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
cursor=conn.cursor()
#fetch datas
cursor.execute('select * from test;')
#获得结果集的记录
numrows=int(cursor.rowcount)
#循环,取行数据
for i in range(numrows):
row=cursor.fetchone()
print row[0],row[1]
#Closing database
cursor.close()
conn.close() >>> ================================ RESTART ================================
>>>
4 zhangbc
5 lis08
6 wangw
ii)使用字典cursor
#-*- coding:UTF-8 -*-
import MySQLdb as mdb
#connect to a database 'test'
conn=mdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
with conn:
#获取连接上的字典cursor,每一个cursor其实都是cursor的子类
cur=conn.cursor(mdb.cursors.DictCursor)
#fetch datas
cur.execute('select * from test;')
#获得结果集
rows=cur.fetchall()
#循环,取行数据
for row in rows:
print '%s %s'%(row['ID'],row['Name'])
#Closing database
cur.close()
conn.close() >>> ================================ RESTART ================================
>>>
4 zhangbc
5 lis08
6 wangw
iii)获取单个表的字段名及其信息
#-*- coding:UTF-8 -*-
import MySQLdb as mdb
#connect to a database 'test'
conn=mdb.connect(host='localhost',user='root',passwd='zbc123',db='test')
with conn:
#获取连接上的字典cursor,每一个cursor其实都是cursor的子类
cur=conn.cursor()
#fetch datas
cur.execute('select * from test;')
#获得结果集
rows=cur.fetchall()
#获得链接对象的描述信息
desc=cur.description
print 'cur.description:',desc
#打印表头
print '%2s %3s'%(desc[0][0],desc[1][0])
#循环,取行数据
for row in rows:
print '%2s %3s'%row
#Closing database
cur.close()
conn.close() >>> ================================ RESTART ================================
>>>
cur.description: (('ID', 3, 1, 11, 11, 0, 0), ('Name', 254, 7, 25, 25, 0, 1))
ID Name
4 zhangbc
5 lis08
6 wangw
Python学习系列(七)( 数据库编程)的更多相关文章
- python学习笔记(七):面向对象编程、类
一.面向对象编程 面向对象--Object Oriented Programming,简称oop,是一种程序设计思想.在说面向对象之前,先说一下什么是编程范式,编程范式你按照什么方式来去编程,去实现一 ...
- Python学习系列(八)( 面向对象基础)
Python学习系列(八)( 面向对象基础) Python学习系列(七)( 数据库编程) 一,面向对象 1,域:属于一个对象或类的变量.有两种类型,即实例变量—属于每个实例/类的对象:类变量—属于类 ...
- Python学习系列(六)(模块)
Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...
- Python学习系列(三)(字符串)
Python学习系列(三)(字符串) Python学习系列(一)(基础入门) Python学习系列(二)(基础知识) 一个月没有更新博客了,最近工作上有点小忙,实在是没有坚持住,丢久又有感觉写的必要了 ...
- Python学习系列(二)(基础知识)
Python基础语法 Python学习系列(一)(基础入门) 对于任何一门语言的学习,学语法是最枯燥无味的,但又不得不学,基础概念较繁琐,本文将不多涉及概念解释,用例子进行相关解析,适当与C语言对比, ...
- python学习之路网络编程篇(第四篇)
python学习之路网络编程篇(第四篇) 内容待补充
- python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍
目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...
- Python学习第七课
Python学习第七课 'Alex' "Alex"print('hello'*5) #重复输出字符串 print('hellowold'[2:]) #类似于切片操作:会取出 llo ...
- Python学习系列(九)(IO与异常处理)
Python学习系列(九)(IO与异常处理) Python学习系列(八)( 面向对象基础) 一,存储器 1,Python提供一个标准的模块,称为pickle,使用它既可以在一个文件中存储任何Pytho ...
随机推荐
- React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发
React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发 2016/09/23 | React Native技术文章 | Sky丶清| 4 条评论 | 1 ...
- [CF489D]Unbearable Controversy of Being
题目大意:求有向图中这种图的数量 从分层图来考虑,这是一个层数为3的图 枚举第一个点能到达的所有点,对他们进行BFS求第三层的点(假装它是BFS其实直接枚举效果一样) 代码: #include< ...
- Oracle中清除BIN$开头的垃圾表的解决办法 [转]
oracle drop table的时候,不会彻底删除该表,它将drop的表放到了自己的回收站里,放到回收站的表就是我们看到的形如bin$/rt62vkdt5wmrjfcz28eja==$0的表,其中 ...
- cmd下进入oracle sqlplus
1.sqlplus /nolog 2.connect sys/orcl@ORCL as sysdba 3.select sysdate from dual exit;
- postgresql centos6.5安装以及常用命令
今天在centos6.5下安装postgresql数据库,现在整理自己操作步骤. 一. Centos6.5 下安装postgresql9.4 1.1. 显示所有的有关postgresql安装包 yum ...
- mysql 出现Host 'localhost' is not allowed to connect to this MySQL server 错误
MySql数据库:Host 'localhost' is not allowed to connect to this MySQL server 修改mysql的root密码后,出现Host 'loc ...
- 如何让pycharm以py.test方式运行
第一步:进入File—Settings—Python Integrated Tools 发现设置中Default test runner是Unittests 将其改为py.test,点击OK保存 如果 ...
- LightOJ - 1265 概率
题意:有t头老虎,d头鹿,每天五种情况,虎虎,虎鹿,鹿鹿,鹿人,人虎,求生存的概率 题意:鹿就是来迷惑你的(结果我就陷进坑了),无论怎么选最后一定只剩下虎虎,虎人两种情况对结果有影响,那么如果有n只虎 ...
- Swagger实践和总结
Swagger学习和实践 最近安装并使用了一下Swagger-ui.Swagger-editor和Swagger-codegen,感觉还不错. Swagger 是一个规范和完整的框架,用于生成.描述. ...
- input预览上传图片
html代码 <input type="file" name="file" id="file" > <img src=&q ...