慕课 python 操作数据库
test_connection
import MySQLdb conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = '*****',
passwd = '*****',
db = 'czy',
charset = 'utf8'
) cursor = conn.cursor() print conn
print cursor cursor.close()
conn.close()
结果:
<_mysql.connection open to '127.0.0.1' at 21f0980>
<MySQLdb.cursors.Cursor object at 0x0228BA30>
test_coursor:
import MySQLdb conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = '****',
passwd = '****',
db = 'czy',
charset = 'utf8'
) cursor = conn.cursor() sql = "select * from user"
cursor.execute(sql) print cursor.rowcount rs = cursor.fetchone()
print rs rs = cursor.fetchmany(3)
print rs rs = cursor.fetchall()
print rs cursor.close()
conn.close()
结果:
9
(1L, u'name1')
((2L, u'name2'), (3L, u'name3'), (4L, u'name4'))
((5L, u'name5'), (6L, u'name6'), (7L, u'name7'), (8L, u'name8'), (9L, u'name9'))
test_select:
import MySQLdb conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = '**',
passwd = '**',
db = 'czy',
charset = 'utf8'
) cursor = conn.cursor() sql = "select * from user"
cursor.execute(sql) rs = cursor.fetchall() for row in rs:
print "userid = %s,username = %s" % row cursor.close()
conn.close()
结果:
userid = 1,username = name1
userid = 2,username = name2
userid = 3,username = name3
userid = 4,username = name4
userid = 5,username = name5
userid = 6,username = name6
userid = 7,username = name7
userid = 8,username = name8
userid = 9,username = name9

test_iud:
import MySQLdb conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = '',
passwd = '',
db = 'czy',
charset = 'utf8'
) cursor = conn.cursor() sql_insert = "insert into user(userid,username) values(10,'name10')"
sql_update = "update user set username='name91' where userid=9"
sql_delete = "delete from user where userd<3" try:
cursor.execute(sql_insert)
print cursor.rowcount cursor.execute(sql_update)
print cursor.rowcount cursor.execute(sql_delete)
print cursor.rowcount conn.commit()
except Exception, e:
print e
conn.rollback() cursor.close()
conn.close()
结果(没有影响数据库,因为第三条语句失败了):
1
1
(1054, "Unknown column 'userd' in 'where clause'")
慕课 python 操作数据库的更多相关文章
- 慕课 python 操作数据库2 银行转账实例
CREATE TABLE `account` ( `acctid` ) DEFAULT NULL COMMENT '账户ID', `) DEFAULT NULL COMMENT '余额' ) ENGI ...
- python操作数据库
一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的l ...
- python操作数据库之批量导入
python操作数据库之批量导入 Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进. Python具有丰富和强大的库.它常被昵称为胶水语言,能够把用其他语言制作的 ...
- 零基础学Python--------第11章 使用Python操作数据库
第11章 使用Python操作数据库 11.1 数据库编程接口 在项目开发中,数据库应用必不可少.虽然数据库的种类有很多,如SQLite.MySQL.Oracle等,但是它们的功能基本都是一样的,为了 ...
- python操作数据库PostgreSQL
1.简述 python可以操作多种数据库,诸如SQLite.MySql.PostgreSQL等,这里不对所有的数据库操作方法进行赘述,只针对目前项目中用到的PostgreSQL做一下简单介绍,主要包括 ...
- Python操作数据库之 MySQL
Python操作数据库之MySQL 一.安装Python-MySQLdb模块 Python-MySQLdb是一个操作数据库的模块,Python 通过它对 mysql 数据实现各种操作. 如果要源码安装 ...
- Python接口测试实战3(上)- Python操作数据库
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- Python学习笔记 - day11 - Python操作数据库
MySQL的事务 MySQL的事务支持不是绑定在MySQL服务器本身,而是与存储引擎相关,MySQL的两种引擎如下: 1.MyISAM:不支持事务,用于只读程序提高性能 2.InnoDB:支持ACID ...
- 孤荷凌寒自学python第四十四天Python操作 数据库之准备工作
孤荷凌寒自学python第四十四天Python操作数据库之准备工作 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天非常激动地开始接触Python的数据库操作的学习了,数据库是系统化设计 ...
随机推荐
- mount命令的用法,以及技巧光盘镜像文件、移动硬盘及U盘的方法
本文介绍mount命令的用法,以及技巧光盘镜像文件.移动硬盘及U盘的方法. 一,挂接命令(mount) 挂接(mount)命令的使用方法. 命令格式: 复制代码 代码如下: mount [-t vfs ...
- python列表之append与extend方法比较
append和extend是列表的两种添加元素方式,但这两种方式却又有些不同之处.那么不同之处在哪里呢,我们通过对二者的定义和实例来看一看. list.append() 1.定义:L.append(o ...
- 【php】expose_php 作用
php.ini expose_php = On expose_php = Off
- Pyhon从入门到致命
第一章 基础 1.python2和python3的区别 2.数据类型 2.1 int 整型 2.2 str 字符串不可变类型 2.3 bool 布尔类型 2.4 list 列表 2.5 tuple 元 ...
- 学习pwn的一些指导
使用ret2libc攻击方法绕过数据执行保护 http://blog.csdn.net/linyt/article/details/43643499 格式化字符串利用小结 http://www.cnb ...
- zigbee 中 OSAL 事件传递机制和消息传递机制
一.概述 OSAL (Operating System Abstraction Layer) ,翻译为"操作系统抽象层". OSAL 就是一种支持多任务运行的系统资源分配机制.OS ...
- Leetcode 145. 二叉树的后序遍历
题目链接 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/ 题目描述 给定一个二叉树,返回它的 ...
- socketserver源码剖析
作者:人世间链接:https://www.jianshu.com/p/357e436936bf來源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处 BaseServer 和 B ...
- MyString的简单实现
MyString.h 文件 #ifndef _STRING_H_ #define _STRING_H_ #include <iostream> using namespace std; c ...
- day18-socket 编程
1.Socket是网络上的使用的交互信息得方法,也叫套接字 用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 通讯原理 Soc ...