python2.0_s12_day9_mysql操作
mysql的基本语法:
1.数据库操作
show databases;
create database 数据库名;如果想允许数据库可以写中文create database 数据库名 charset utf8
use 数据库名;
show tables; 2.数据表操作
create table 表名
(
id int not null auto_increment primary key, # 主键 指每一行的唯一标示符
name char(9) not null,
sex char(4) not null,
age tinyint unsigned not null, # unsigned
tel char(13) null default "_"
);
desc 表名;查看表结构
show create table 表名; 查看这个表是通过什么语句创建的
alter table students add column 字段名 char(30); 给表插入一个字段
InnoDB 数据引擎,是支持事务性操作,比如ATM银行转帐,拿现金转账,当你现金存入,开始转账的时候断电,那么转账失败,同时数据库会把存入成功的记录也会回滚,变得不成功,随后把钱给你退出来. 3.数据操作
insert into 表名(字段1,字段2,字段3) values('值1','值2','值3') ; 数据插入
delete from 表名 where 字段1 = '值'; 删除行记录
update 表名 set 字段2 = 'sb' where 字段1 = '值'; 更新表中某条记录的某个字段值
select * from 表名 ; 查寻表所有记录 4.其他
主键
外键
左右连接 python连接mysql的模块 python连接mysql的模块很多,我们使用MySQLdb模块,需要下载。
一、插入数据
import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb') cur = conn.cursor() reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa'))
# reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':,'name':'wupeiqi'}) conn.commit() cur.close()
conn.close() print reCount
上面使用cur.execute()方法插入一条记录,那么怎样批量插入数据记录呢.可以使用cur.executemany()
import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb') cur = conn.cursor() li =[
('alex','usa'),
('sb','usa'),
]
reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li) conn.commit()
cur.close()
conn.close() print reCount
二、删除数据
import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb') cur = conn.cursor() reCount = cur.execute('delete from UserInfo') conn.commit() cur.close()
conn.close() print reCount
三、修改数据
import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb') cur = conn.cursor() reCount = cur.execute('update UserInfo set Name = %s',('alin',)) conn.commit()
cur.close()
conn.close() print reCount
四、查数据
# ############################## fetchone/fetchmany(num) ############################## import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
cur = conn.cursor() reCount = cur.execute('select * from UserInfo') print cur.fetchone()
print cur.fetchone()
cur.scroll(-,mode='relative')
print cur.fetchone()
print cur.fetchone()
cur.scroll(,mode='absolute')
print cur.fetchone()
print cur.fetchone() cur.close()
conn.close() print reCount
# ############################## fetchall ############################## import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
cur = conn.cursor() reCount = cur.execute('select Name,Address from UserInfo') nRet = cur.fetchall() cur.close()
conn.close() print reCount
print nRet
for i in nRet:
print i[],i[]
python2.0_s12_day9_mysql操作的更多相关文章
- python2.7 操作ceph-cluster S3对象接口 实现: 上传 下载 查询 删除 顺便使用Docker装个owncloud 实现UI管理
python version: python2.7 需要安装得轮子: botofilechunkio command: yum install python-pip&& pip ...
- 使用python2连接操作db2
在python2.6下连接db2,步骤: 1.安装python2.6. (注:目前db2的驱动还不支持2.7) 2.安装setuptools,下载地址http://pypi.python.org/py ...
- Python使用MySQLConnector/Python操作MySQL、MariaDB数据库
使用MySQL Connector/Python操作MySQL.MariaDB数据库 by:授客 QQ:1033553122 因目前MySQLdb并不支持python3.x,而MySQL官方已经提 ...
- Python多版本情况下四种快速进入交互式命令行的操作技巧
因为工作需求或者学习需要等原因,部分小伙伴的电脑中同时安装了Python2和Python3,相信在Python多版本的切换中常常会遇到Python傻傻分不清楚的情况,今天小编整理了四个操作技巧,以帮助 ...
- Inception使用详解
一.Inception简介一款用于MySQL语句的审核的开源工具,不但具备自动化审核功能,同时还具备执行.生成对影响数据的回滚语句功能. 基本架构: 二.Inception安装 1.软件下载 下载链接 ...
- python MySQLdb pymsql
参考文档 https://www.python.org/dev/peps/pep-0249/#nextset 本节内容 MySQLdb pymysql MySQLdb和pymysql分别为Pytho ...
- Linux 远程工具Screen 的应用
挂断原理参考:https://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 要求,python2 常用操作: 创建screen screen -L ...
- 2.python知识点总结
1.什么是对象?什么是类? 对象是对类的具体表达,类是对象的抽象表达. 类只是为所有的对象定义了抽象的属性与行为. —————————————————————————————————————————— ...
- 关于Struts漏洞工具的使用
最新struts-scan资源: https://www.cesafe.com/3486.html 一,将资源下载后,放入liunx系统中,并且需要具备python2的操作环境 二,打开终端使用如下命 ...
随机推荐
- C#集合概述
C#集合概述 2016-11-29 集合 顺序排列 连顺存储 直接访问方式 访问时间 操作时间 备注 Dictionary 是 Key Key:O(1) O(1) 访问性能最快,不支持排序 Sorte ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- dp之二维背包hdu3496
题意:给你n张电影门票,但一次只可以买m张,并且你最多可以看L分钟,接下来是n场电影,每一场电影a分钟,b价值,要求恰好看m场电影所得到的最大价值,要是看不到m场电影,输出0: 思路:这个题目可以很明 ...
- JAVA-JSP表达式
相关资料: <21天学通Java Web开发> 结果总结: 1.<%= %>JSP表达式中的代码会首先执行,然后转换成字符串显示到网页上.2.JSP表达式标签不必也不能使用分号 ...
- ngApp指令,也就是ng-app属性
翻译:https://docs.angularjs.org/api/ng/directive/ngApp 使用这个指令去 自动引导 一个AngularJS 应用程序. ngApp 指令规定了html ...
- javascript-删除节点
任务目的: 每点击一下按钮删除一个标签. 掌握到了: 这里是主要学习到了parentNote以及removeChild两个DOM属性. parentNote:parent英译为父亲,Note英译为节点 ...
- 工作队列workqueue应用
工作队列是另一种将工作推后执行的形式,它可以把工作交给一个内核线程去执行,这个下半部是在进程上下文中执行的,因此,它可以重新调度还有睡眠. 区分使用软中断/tasklet还是工作队列比较简单,如果推后 ...
- java依赖的外部文件路径的获取
在开发阶段一直使用以下方式调试没有问题: String path = KStream104.class.getResource("/").getFile().toString(); ...
- The declared package "com.dao" does not match the expected package "src.com.dao"
今天把项目代码上传到svn后出现例如以下错误:The declared package "com.dao" does not match the expected package ...
- 基于wsdl2java訪问外来service服务
一.wsdl2java介绍 Wsdl2java是cxf提供的一个用于生成client代码的工具,它的功能跟wsimport差点儿相同. 可是wsdl2java工具仅仅能生成訪问基于cxf公布的服务的代 ...