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语句使用回顾

  1. create table students
  2. (
  3. id int not null auto_increment primary key,
  4. name char(8) not null,
  5. sex char(4) not null,
  6. age tinyint unsigned not null,
  7. tel char(13) null default "-"
  8. );

1、创建表

  1. insert into students(name,sex,age,tel) values('alex','man',18,'')
  2.  
  3. delete from students where id =2;
  4.  
  5. update students set name = 'sb' where id =1;
  6.  
  7. select * from students

2、原生SQL语句基本操作

  2、插入数据

  1. import MySQLdb
  2.  
  3. conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
  4.  
  5. cur = conn.cursor()
  6.  
  7. reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa'))
  8. # reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':12345,'name':'wupeiqi'})
  9.  
  10. conn.commit()
  11.  
  12. cur.close()
  13. conn.close()
  14.  
  15. print reCount

1、插入单条数据

  1. import MySQLdb
  2.  
  3. conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
  4.  
  5. cur = conn.cursor()
  6.  
  7. li =[
  8. ('alex','usa'),
  9. ('sb','usa'),
  10. ]
  11. reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li)
  12.  
  13. conn.commit()
  14. cur.close()
  15. conn.close()
  16.  
  17. print reCount

2、批量插入

  3、删除数据

  1. import MySQLdb
  2.  
  3. conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
  4.  
  5. cur = conn.cursor()
  6.  
  7. reCount = cur.execute('delete from UserInfo')
  8.  
  9. conn.commit()
  10.  
  11. cur.close()
  12. conn.close()
  13.  
  14. print reCount

删除数据

  4、修改数据

  1. import MySQLdb
  2.  
  3. conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
  4.  
  5. cur = conn.cursor()
  6.  
  7. reCount = cur.execute('update UserInfo set Name = %s',('alin',))
  8.  
  9. conn.commit()
  10. cur.close()
  11. conn.close()
  12.  
  13. print reCount

修改数据

  5、查询数据

  1. # ############################## fetchone/fetchmany(num) ##############################
  2.  
  3. import MySQLdb
  4.  
  5. conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
  6. cur = conn.cursor()
  7.  
  8. reCount = cur.execute('select * from UserInfo')
  9.  
  10. print cur.fetchone()
  11. print cur.fetchone()
  12. cur.scroll(-1,mode='relative')
  13. print cur.fetchone()
  14. print cur.fetchone()
  15. cur.scroll(0,mode='absolute')
  16. print cur.fetchone()
  17. print cur.fetchone()
  18.  
  19. cur.close()
  20. conn.close()
  21.  
  22. print reCount
  23.  
  24. # ############################## fetchall ##############################
  25.  
  26. import MySQLdb
  27.  
  28. conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
  29. #cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
  30. cur = conn.cursor()
  31.  
  32. reCount = cur.execute('select Name,Address from UserInfo')
  33.  
  34. nRet = cur.fetchall()
  35.  
  36. cur.close()
  37. conn.close()
  38.  
  39. print reCount
  40. print nRet
  41. for i in nRet:
  42. print i[0],i[1]

查询数据

05: MySQLdb 原生SQL语句操作数据库的更多相关文章

  1. 043:Django使用原生SQL语句操作数据库

    Django使用原生SQL语句操作数据库 Django配置连接数据库: 在操作数据库之前,首先先要连接数据库.这里我们以配置 MySQL 为例来讲解. Django 连接数据库,不需要单独的创建一个连 ...

  2. Django中使用mysql数据库并使用原生sql语句操作

    Django自身默认使用sqlite3这个轻量级的数据库,但是当我们开发网站时,sqlite3就没有mysql好,sqlite3适合一些手机上开发使用的数据库. 准备的软件mysql数据库,版本5.7 ...

  3. mysql 操作sql语句 操作数据库

    sql语句 #1. 操作文件夹 创建数据库:create database db1 charset utf8; 查数据库:show databases; mysql> create databa ...

  4. mysql:SQL语句操作数据库中表和字段的COMMENT值

    转载:http://blog.163.com/inflexible_simple/blog/static/167694684201182601221758/ 参考文档不太给力啊,表注释和字段注释的资料 ...

  5. 用SQL语句操作数据库

    —―有一天,当你发觉日子特别的艰难,那可能是这次的收获将特别的巨大.—―致那些懈怠的岁月 本章任务: 学生数据库中数据的增加.修改和删除 目标: 1:使用T-SQL向表中插入数据 2:使用T-SQL更 ...

  6. 编写SQL语句操作数据库(慕课SQLite笔记)

    安卓常用数据存储方式之一SQLite学习及操作笔记 0.视频地址:http://www.imooc.com/video/3382 1.每个程序都有自己的数据库 默认情况下是各自互不干扰 1)创建一个数 ...

  7. Django&,Flask&pyrthon原生sql语句 基本操作

    Django框架 ,Flask框架 ORM 以及pyrthon原生sql语句操作数据库 WHAT IS ORM? ORM( Object Relational Mapping) 对象关系映射 , 即通 ...

  8. Flask使用原生sql语句

    安装pip install flask-sqlalchemy 创建数据库对象 # 设置数据库连接地址app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://r ...

  9. SQL之T-sql 语句操作数据库

    用SQL语句操作数据库 在上一次的话题中我们谈到了怎么使用数据库,说到了数据库的基本用法. 不过只是仅限于一些简单的操作,so 如果你不想被人说--"你们只是动动鼠标操作就可以了! 没什么技 ...

随机推荐

  1. ubuntu下安装opencv3.1.0

    ubuntu14.04安装opencv3.1 1.下载opencv3.1源码http://opencv.org/releases.html 2.安装opencv3 2.1安装opencv3的依赖 1 ...

  2. ECNU 3247 - 铁路修复计划

    Time limit per test: 2.0 seconds Time limit all tests: 15.0 seconds Memory limit: 256 megabytes 在 A ...

  3. cordova 跨平台APP版本升级

    利用cordova+ionic开发好项目,之后就是打包发布,在这之前,还要做一个版本升级的小功能. 首先我们项目根目录里自然少不了配置:config.xml中 如图.version,我们以后每次升级A ...

  4. 优云软件助阵GOPS·2017全球运维大会北京站

    GOPS· 2017全球运维大会北京站于2017年7月28日-29日在北京隆重举办,汇聚国内一线运维专家和诸多运维同仁达800余名.作为长期致力于企业级高端运维市场软件开发和咨询服务的优云软件受邀参与 ...

  5. sql server内置存储过程、查看系统信息

    1.检索关键字:sql server内置存储过程,sql server查看系统信息 2.查看磁盘空间:EXEC master.dbo.xp_fixeddrives , --查看各个数据库所在磁盘情况S ...

  6. 浅析I/O处理过程与存储性能的关系

    浅析I/O处理过程与存储性能的关系 https://community.emc.com/docs/DOC-28653 性能”这个词可以说伴随着整个IT行业的发展,每次新的技术出现,从硬件到软件大多数情 ...

  7. hashcode()和equals()的作用、区别、联系

        介绍一. hashCode()方法和equal()方法的作用其实一样,在Java里都是用来对比两个对象是否相等一致,那么equal()既然已经能实现对比的功能了,为什么还要hashCode() ...

  8. 7.MQTT网页客户端连接MQTT服务器的问题WebSocket connection to 'ws://XXX:1883/' failed: Connection closed before receiving a handshake response

    问题描述:MQTT.js提供了连接MQTT的一套javascipt的方法,可供前端连接到MQTT服务器,也可以作为脚本测试.以脚本形式,用nodejs运行,是没有问题的,能够正常连接并且发送报文.但是 ...

  9. [sh]sh最佳实战(含grep)

    sh虐我千百遍,我待sh如初恋. sh复习资料 http://www.cnblogs.com/iiiiher/p/5385108.html http://blog.csdn.net/iiiiher/a ...

  10. [py]str list切片-去除字符串首尾空格-递归思想

    取出arr的前几项 #方法1 print([arr[0], arr[1]]) #方法2 arr2 = [] for i in range(2): arr2.append(arr[i]) print(a ...