05: MySQLdb 原生SQL语句操作数据库
1.1 MySQLdb安装与简介
1、MySQLdb 模块的安装(python3中目前这个模块还不可用)参考博客
1. linux:
yum install MySQL-python
2. window:
http://files.cnblogs.com/files/wupeiqi/py-mysql-win.zip
1.2 MySQLdb基本使用
1、原生SQL语句使用回顾
create table students
(
id int not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
1、创建表
insert into students(name,sex,age,tel) values('alex','man',18,'')
delete from students where id =2;
update students set name = 'sb' where id =1;
select * from students
2、原生SQL语句基本操作
2、插入数据
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':12345,'name':'wupeiqi'})
conn.commit()
cur.close()
conn.close()
print reCount
1、插入单条数据
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
2、批量插入
3、删除数据
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
删除数据
4、修改数据
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
修改数据
5、查询数据
# ############################## 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(-1,mode='relative')
print cur.fetchone()
print cur.fetchone()
cur.scroll(0,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[0],i[1]
查询数据
05: MySQLdb 原生SQL语句操作数据库的更多相关文章
- 043:Django使用原生SQL语句操作数据库
Django使用原生SQL语句操作数据库 Django配置连接数据库: 在操作数据库之前,首先先要连接数据库.这里我们以配置 MySQL 为例来讲解. Django 连接数据库,不需要单独的创建一个连 ...
- Django中使用mysql数据库并使用原生sql语句操作
Django自身默认使用sqlite3这个轻量级的数据库,但是当我们开发网站时,sqlite3就没有mysql好,sqlite3适合一些手机上开发使用的数据库. 准备的软件mysql数据库,版本5.7 ...
- mysql 操作sql语句 操作数据库
sql语句 #1. 操作文件夹 创建数据库:create database db1 charset utf8; 查数据库:show databases; mysql> create databa ...
- mysql:SQL语句操作数据库中表和字段的COMMENT值
转载:http://blog.163.com/inflexible_simple/blog/static/167694684201182601221758/ 参考文档不太给力啊,表注释和字段注释的资料 ...
- 用SQL语句操作数据库
—―有一天,当你发觉日子特别的艰难,那可能是这次的收获将特别的巨大.—―致那些懈怠的岁月 本章任务: 学生数据库中数据的增加.修改和删除 目标: 1:使用T-SQL向表中插入数据 2:使用T-SQL更 ...
- 编写SQL语句操作数据库(慕课SQLite笔记)
安卓常用数据存储方式之一SQLite学习及操作笔记 0.视频地址:http://www.imooc.com/video/3382 1.每个程序都有自己的数据库 默认情况下是各自互不干扰 1)创建一个数 ...
- Django&,Flask&pyrthon原生sql语句 基本操作
Django框架 ,Flask框架 ORM 以及pyrthon原生sql语句操作数据库 WHAT IS ORM? ORM( Object Relational Mapping) 对象关系映射 , 即通 ...
- Flask使用原生sql语句
安装pip install flask-sqlalchemy 创建数据库对象 # 设置数据库连接地址app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://r ...
- SQL之T-sql 语句操作数据库
用SQL语句操作数据库 在上一次的话题中我们谈到了怎么使用数据库,说到了数据库的基本用法. 不过只是仅限于一些简单的操作,so 如果你不想被人说--"你们只是动动鼠标操作就可以了! 没什么技 ...
随机推荐
- PHP Architecture
http://www.laruence.com/2008/08/12/180.html
- 懂点PS技巧,你会减少很多痛苦
UI设计 不像平面设计那样随性, 期间可以用点技巧来减少痛苦. 1. 设置网格线 保持像素完美 不在1:1分辨率下也能保持像素完美,可以通过创建网格线来避免虚边的出现. 编辑 > 首选项 > ...
- dedecms提取某栏目及子栏目名称到首页怎么弄
我们建网站时有不同的需求,例如为页面创建一个栏目导航,用dedecms如何提取某栏目及子栏目名称和链接呢?如下图所示,先列出指定的顶级栏目,在下方再列出此栏目的所有子栏目. 之前ytkah说过dede ...
- 划分LUN
划分LUN http://mp.weixin.qq.com/s?__biz=MzAwNzU3NzQ0MA==&mid=209842199&idx=1&sn=7d77fdf7a8 ...
- (c++) int 转 string,char*,const char*和string的相互转换
一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...
- mysql 常用命令 常用SQL语句
维护命令 数据库 ##创建数据库 mysql> create database test; Query OK, 1 row affected ##删除数据库 mysql> drop dat ...
- [LeetCode] 694. Number of Distinct Islands
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- loadrunner 接口性能脚本编写(Get请求和Post请求)
前段时间接触了一下loadrunner的接口性能测试,然后尝试了一下手动编写脚本,毕竟录制这种东西,不是每次都能通的,而且录制下来的脚本,通常是有很多其他杂七杂八的请求夹杂在中间,没有达到真正的压测接 ...
- Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数
Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...
- testng入门教程5TestNG套件测试
TestNG套件测试 测试套件的测试是为了测试软件程序的行为或一系列行为的情况下,是一个集合.在TestNG,我们不能定义一套测试源代码,但它代表的套件是一个XML文件执行特征.这也允许灵活的配置要运 ...