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的操作环境 二,打开终端使用如下命 ...
随机推荐
- linux 查看文件占用的大小
du --max-depth=1 -h /usr/local/tomcat_bjtu2/bin/* 文件路径 du -sh * | sort -nr | head du -sh *
- ReportNG测试报告的定制修改(二)
上一篇文章修改了一些基本的ReportNG信息,链接:https://www.cnblogs.com/mrjade/p/9912073.html,本文将继续带大家进行修改,重点是添加饼图 1.修改测试 ...
- stock article
stock 征服星辰大海 http://www.tianya.cn/95789158 论坛_悟道股市_天涯社区 http://www.tianya.cn/108318593/bbs?t=post 当下 ...
- php phpmail发送邮件的效果
方法一: /* * 发送邮件 原 smtp ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 在Window下安装解压版的mysql 5.7.11
今天由于要在windows下学习Kettle,因此在Windows下安装了mysql 5.7.11,本来是没什么大问题的,但是在启动服务时还是出了点问题,服务老是启动不了: (一)解压到安装路径: ...
- Linux 网络子系统之网络协议接口层(一)
Linux 网络设备驱动之网络协议接口层介绍. 网络协议接口层最主要的功能是给上层协议提供透明的数据包发送和接收接口. 当上层ARP或IP需要发送数据包时,它将调用网络协议接口层的dev_queue_ ...
- git不能上传空目录和设备文件
git不能上传空目录和设备文件:目录和设备文件不能完成校验. 使用命令校验: sha1sum console
- git过期处理
删除git下的文件:C:\Users\Java\AppData\Roaming\syntevo\......\7\settings.xml删除settings.xml文件即可