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的操作环境 二,打开终端使用如下命 ...
随机推荐
- 在Eclipse中给JRE-Library添加本地Javadoc
Eclipse中的JRE-Library的Javadoc默认是一个URL,指向oracle的一个web-page,那你在离线的时候就无法使用了,为了解决这个问题,你可以从oracle下载JDK-Spe ...
- DelegatingFilterProxy(委派过滤器代理类)使用
本文转自:http://blog.csdn.net/flyingfalcon/article/details/8543898 DelegatingFilterProxy就是一个对于servlet fi ...
- nginx 405 not allowed问题的解决
转载自: http://www.linuxidc.com/Linux/2012-07/66761.htm Apache.IIS.Nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否 ...
- kubernetes deployment
deployment是k8s中部署应用最常见的一种方式.如果不需要被访问,那么只需要定义deployment即可.如果需要被其他服务访问,那么可以创建一个service与其绑定,通过访问service ...
- git不能上传空目录和设备文件
git不能上传空目录和设备文件:目录和设备文件不能完成校验. 使用命令校验: sha1sum console
- CentOS 6编译安装yum和配置常用的yum源
安装环境:VPS,CentOS 6 + devel包 一.安装相应的软件 1.安装python 下载Python源码包 [root@akinlau ~]# wget http://www.python ...
- 【Mac双系统设置系统默认启动系统】解决方式
解决方式1: 开机时长按option键,进入系统选择界面: 用左右方向键选择到你要设置为默认启动的盘, 然后同一时候按下ctrl+enter键.就可以将其设置为默认启动的系统. 解决方式2: 选择ma ...
- Cracking the coding interview--Q2.5
题目 原文: Given a circular linked list, implement an algorithm which returns node at the beginning of t ...
- rails下mysql出错问题mysql_api,blog/text
问题一:提示出错:cannot load such file -- mysql/mysql_api (LoadError) 此时我们回来看gem install mysql 时提示 At the ti ...
- ASP.NET实现类似Excel的数据透视表
代码: /Files/zhuqil/Pivot.zip 数据透视表提供的数据三维视图效果,在Microsoft Excel能创建数据透视表,但是,它并不会总是很方便使用Excel.您可能希望在Web应 ...