python下操作mysql  之  pymsql

                              pymsql是Python中操作MySQL的模块,

下载安装:

pip3 install pymysql

使用操作
  1, 执行SQL

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1')

# 创建游标

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()

  2, 获取新创建数据自增ID

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1')

cursor = conn.cursor()

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()

# 获取最新自增ID

new_id = cursor.lastrowid

  3, 获取查询数据

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1')

cursor = conn.cursor()

cursor.execute("select * from hosts")

# 获取第一行数据

row_1 = cursor.fetchone()

# 获取前n行数据

# row_2 = cursor.fetchmany(3)

# 获取所有数据

# row_3 = cursor.fetchall()

conn.commit()

cursor.close()

conn.close()

  注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:

  • cursor.scroll(1,mode='relative')  # 相对当前位置移动
  • cursor.scroll(2,mode='absolute') # 相对绝对位置移动

  4, fetch 数据类型

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1')

# 游标设置为字典类型

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

r = cursor.execute("call p1()")

result = cursor.fetchone()

conn.commit()

cursor.close()

conn.close()

  5,input 下执行

import pymysql
username = input('请输入用户名:') pwd = input('请输入密码:') # 1.连接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8') # 2.创建游标
cursor = conn.cursor() # 操作
# 增
# sql = "insert into userinfo(username,pwd) values (%s,%s)" # effect_row = cursor.execute(sql,(username,pwd))
#同时插入多条数据
#cursor.executemany(sql,[('李四','110'),('王五','119')]) # print(effect_row)# # 改
# sql = "update userinfo set username = %s where id = 2"
# effect_row = cursor.execute(sql,username)
# print(effect_row) # 删
sql = "delete from userinfo where id = 2"
effect_row = cursor.execute(sql)
print(effect_row) #一定记得commit
conn.commit() # 4.关闭游标
cursor.close() # 5.关闭连接
conn.close()

 

python下操作mysql 之 pymsql的更多相关文章

  1. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  2. (转)Python中操作mysql的pymysql模块详解

    原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python ...

  3. python数据库操作 - MySQL入门【转】

    python数据库操作 - MySQL入门 python学院 2017-02-05 16:22 PyMySQL是Python中操作MySQL的模块,和之前使用的MySQLdb模块基本功能一致,PyMy ...

  4. day06 python代码操作MySQL

    day06 python代码操作MySQL 今日内容 python代码操作MySQL 基于python与MySQL实现用户注册登录 python操作MySQL python 胶水语言.调包侠(贬义词& ...

  5. python 之操作mysql 数据库实例

    对于python操作mysql 数据库,具体的步骤应为: 1. 连接上mysql host 端口号 数据库 账号 密码2. 建立游标3. 执行sql(注意,如果是update,insert,delet ...

  6. python之操作mysql(一)

    使用python操作mysql的思路: 1. 连接数据库:ip,端口号,密码,账号,数据库 2. 建立游标 3.执行sql语句 4.获取执行结果 5.关闭游标,关闭连接 conn = pymysql. ...

  7. linux shell命令行下操作mysql 删除mysql指定数据库下的所有表--亲测成功百分百测试通过--绝对可靠

    1,在shell提示符下查看mysql指定数据库下的表等数据

  8. python下对mysql数据库的链接操作

    参考网址: https://blog.csdn.net/guofeng93/article/details/53994112 https://blog.csdn.net/Chen_Eris/artic ...

  9. python 安装操作 MySQL 数据库.

    以ubuntu和mysql为例 检查自己的机器上面有没有安装数据库 xpower@xpower-CW65S:~$ sudo service mysql start [sudo] xpower 的密码: ...

随机推荐

  1. 谈谈c++纯虚函数的意义!

    纯虚函数的存在有什么意义呢?相信大学假设有c++这么课程.在讲到纯虚函数时,必然会讲到纯虚函数是面向接口编程的基础. 如今和大家分享下纯虚函数设计的原由.目的.产生的效果. 现代软件project很庞 ...

  2. Codeforces Round #273 (Div. 2) B . Random Teams 贪心

    B. Random Teams   n participants of the competition were split into m teams in some manner so that e ...

  3. sql server的版本检查

    https://support.microsoft.com/en-ph/help/321185/how-to-determine-the-version-edition-and-update-leve ...

  4. 项目中如何使用NuGet添加类库

    在项目上右键-->Manage NuGet Packages Browse  可以去搜索想要添加到项目的类库 Installed  已经添加到项目的类库 Updates   需要更新的类库

  5. [luogu_U15116]珈百璃堕落的开始

    https://www.zybuluo.com/ysner/note/1239458 题面 给定\(n\)个二元组\((x,y)\),问有多少种方案,使得选出其中几个后,\(\sum x=\sum y ...

  6. openstack 杂记 备忘

  7. day24 03 多继承

    day24 03 多继承 正常的代码中  单继承==减少了代码的重复 继承表达的是一种 子类是父类的关系 1.简单的多继承关系 A,B,C,D四个类,其中D类继承A,B,C三个父类,因此也叫多继承,子 ...

  8. 自动保存草稿 asp+ajax自动存稿功能详解(转自影子)

    自动保存草稿功能的原理 我们都知道网页是一种无状态的,每次都需要请求,响应,当一次请求完成后就与服务器断开连接了,所以我们不能像网页一样实现实时的交互功能,但是为了满足更多的需求一个比较无敌的程序员还 ...

  9. 12.Nodes

    Nodes(节点) Animation(动画)       KeyframeAnimation 逐帧动画,该节点中包含了所有绑定属性的动画逻辑   Animation Group 逐帧动画分组   S ...

  10. fcc 响应式框架Bootstrap 练习2

    text-primary 属性值使标题直接变成了红色,text-center使标题直接居中 <h2 class="text-primary  text-center"> ...