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的操作环境 二,打开终端使用如下命 ...
随机推荐
- maven导入外部包pom.xml配置
<dependency> <groupId>com.hadoop</groupId> <artifactId>hadoop-lzo</artifa ...
- 推荐2个干净的PE
1,微PE(之前著名的通用PE.绝对PE都出自该作者) 官网:http://www.wepe.com.cn/ 2,金狐维护盘 官网:http://www.jinhu.me/
- encoding/path可能引起无数奇怪的问题
例如如下代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...
- sqlserver被锁的表以及如何解锁
查看sqlserver被锁的表以及如何解锁 查看被锁表: select request_session_id spid,OBJECT_NAME(resource_associated_en ...
- OK335xS 256M 512M nand flash make ubifs hacking
/********************************************************************************* * OK335xs 256M 51 ...
- error LNK2019: 无法解析的外部符号 WinMain,该符号在函数 "int __cdecl invoke_main(void)”中被引用
一,问题描述 MSVCRTD.lib(exe_winmain.obj) : error LNK2019: 无法解析的外部符号 WinMain,该符号在函数 "int __cdecl invo ...
- bootstrap-slider插件使用方法
bootstrap-slider例子地址:https://seiyria.com/bootstrap-slider/ bootstrap-slider github地址:https://github. ...
- 事件总线demo
经过几天的努力拜读大牛高手文章,终于对事件总线有所了解,特此记录下来,以免忘记 1.定义相关的接口: A 事件接口 public interface IDomainEvent { DateTime ...
- Linux 精确获取指定目录对应的块的剩余空间
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/statfs ...
- matplotlib中的legend()——用于显示图例
legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import matplotlib.pyplot as plt import numpy ...