循序渐进Python3(十)-- 1 -- pymysql
使用pymsql 模块操作数据库
#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = '无名小妖'
# ######################### 生产者 #########################
import pymysql
# 创建连接
conn = pymysql.connect(host='192.168.168.231', port=3306, user='root', passwd='123456', db='test')
# 创建游标
cursor = conn.cursor()
# 执行SQL,并返回收影响行数
# effect_row = cursor.execute("update hosts set host = '1.1.1.2'")
# 执行SQL,并返回受影响行数
# effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,))
# 执行SQL,并返回受影响行数
effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
------------------------注----------------------
# 获取最新自增IDnew_id=cursor.lastrowid
# 获取第一行数据,默认取到的数据是元组(只有数据,没有字段名称)row_1=cursor.fetchone()# 获取前n行数据row_2 = cursor.fetchmany(3)# 获取所有数据row_3 = cursor.fetchall()
# 在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如: cursor.scroll(1,mode='relative') # 相对当前位置移动 cursor.scroll(2,mode='absolute') # 相对绝对位置移动
# 创建游标时可指定以字典形式返回数据,这样就可以知道字段名称了
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
例子:
#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = '无名小妖'
# ######################### 生产者 #########################
import pymysql
# 创建连接
conn = pymysql.connect(host='192.168.168.231', port=3306, user='root', passwd='123456', db='test')
# 创建游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
effect_row = cursor.execute("select * from hosts")
print(effect_row)
row_3 = cursor.fetchall()
print(row_3)
# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
参考文档:
http://www.cnblogs.com/wupeiqi/articles/5699254.html
循序渐进Python3(十)-- 1 -- pymysql的更多相关文章
- Python3中使用PyMySQL连接Mysql
Python3中使用PyMySQL连接Mysql 在Python2中连接Mysql数据库用的是MySQLdb,在Python3中连接Mysql数据库用的是PyMySQL,因为MySQLdb不支持Pyt ...
- Python3.x使用PyMysql连接MySQL数据库
Python3.x使用PyMysql连接MySQL数据库 由于Python3.x不向前兼容,导致Python2.x中的很多库在Python3.x中无法使用,例如Mysqldb,我前几天写了一篇博客Py ...
- python学习 —— python3简单使用pymysql包操作数据库
python3只支持pymysql(cpython >= 2.6 or >= 3.3,mysql >= 4.1),python2支持mysqldb. 两个例子: import pym ...
- 循序渐进Python3(十)-- 2 -- SqlAlchemy
ORM 对象关系映射(英语:Object Relation Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类 ...
- 循序渐进Python3(十二) --2-- web框架之django简单实现oracle数据库操作
在 Django 中构建 Oracle 数据库支持的 Web 应用程序 了解如何配置 Django 以便与 Oracle 数据库交互,并使用 ORM 进行数据库连接. 产能在软 ...
- 循序渐进Python3(十二) --0-- web之框架
web框架的本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf ...
- 循序渐进Python3(十二) --1-- web框架之django
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为: 大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能 ...
- 循序渐进Python3(十)-- 3 -- SqlAlchemy
使用sqlalchemy 创建外键关联 ), ), ) host_user = Column(String(), ), ), ]).first()for item in works.workinfo: ...
- 循序渐进Python3(十)-- 4 -- paramiko
paramiko模块,基于SSH用于连接远程服务器并执行相关操作. 使用 (1)SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: , ))transport.connect( ...
随机推荐
- dede 二次开发系统迁移到 IIS 时会出现的问题及解决方案
1.IIS 会默认站点所有文件只读,故一开始请打开目录权限,注意赋予权限对象是 webuser 或 IIS user; 若是集群的话,则目录共享给IIS对应的用户 2.后台不能登陆 查看数据库配置文 ...
- super getClass()
首先看一段代码: import java.util.Date;public class Test extends Date{ public static void main(String[] args ...
- (Array)121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- div,li,span自适应宽度换行问题
<ul class="news"> <li><span class="lbl">右对齐,换行显示的解决方法</s ...
- Momo自定义DialogFragment
在Fragnment弹窗提示 XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- bottlepy template
bottle template usage 1 example 使用bottle模板,最简单的方法是使用template函数或view装饰器 1.1 template 函数 例子如下: from bo ...
- 编译安装带ssl 模块指定版本Python
出现这个或者fatal error: openssl/名单.h: No such file or directory.都是没有安装libssl-dev- libssl-dev包含libraries ...
- leetcode-【中等题】3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- IOS开发者账号申请记录
1. 准备好一个企业邮箱 .企业营业执照复印件.一张可以支付的VISA或者MasterCard 2. 2016/01/13 注册appid https://developer.apple.com/pr ...
- Android test---robotium----简单例子
1.首先新建一个要被测试的工程,命名为”robotium“:一个很简单的Android 应用程序:主页面只有个 TextView 控件: 2. 在建一个用于测试的工程 ,命名为”robotiumTes ...