python下操作mysql 之 pymsql

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=‘123‘, 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=‘123‘, 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=‘123‘, 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=‘123‘, 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()

 

原文地址:https://www.cnblogs.com/konghui/p/10013066.html

时间: 2024-08-02 14:46:47

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

【转】Python中操作mysql的pymysql模块详解

Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11.mysql版本:5.6.24 一.安装 1 pip3 install pymysql 二.使用操作 1.执行SQL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Python中操作mysql的pymysql模块详解

Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,pymysql支持python3.x. 一.安装 pip install pymysql 二.使用操作 1.执行SQL #!/usr/bin/env pytho # -*- coding:utf-8 -*- importpymysql # 创建连接 conn =pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd

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

原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python 3 进阶 -- 使用 PyMySQL 操作 MySQL 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11.mysql版本:5.6.24 一.安装 1

linux下操作mysql,支持重连

(一)code #include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h> #include <mysql/errmsg.h> #include <sys/time.h> #define MAX_QUERY_LEN 1024 #define MAX_QUERY_COUNT 2 #define PRINT_MYSQL_ERROR(

二十三、python中操作MySQL步骤

python中操作mysql步骤 1.引入模块 在py文件中引入pymysql模块 from pymysql import * 2.connection对象 用于建立与数据库的连接 创建对象:调用connect()方法 conn = connect(参数列表) (1)参数host:连接的mysql主机,如果为本机,则是"localhost" (2)参数port:连接的mysql主机的端口,默认是3306 (3)参数database:数据库的名称 (4)参数user:连接的用户名 (5)

最新用python来操作mysql完全解析

1.此处通过MySQLdb来操作mysql,首先 sudo apt-get install libmysqlclient-dev,如何出现 Encountered a section with no Package: header E: Problem with MergeList /var/lib/apt/lists/dl.google.com_linux_chrome_deb_dists_stable_main_i18n_Translation-en%5fUS E: The package

python简单操作mysql

引: 要想使用python操作mysql需要安装mysql-python插件,可以到mysql官网http://dev.mysql.com/downloads/connector/python/进行下载 我所使用的版本为MySQL-python-1.2.5.win32-py2.7,代表只支持使用python2.7. 相关的mysql操作请参考mysql 基本操作 下面为进行mysql增.删.改.查等相应操作: 查: #!/usr/bin/env python # --*-- coding:utf

python 之操作mysql 数据库实例

对于python操作mysql 数据库,具体的步骤应为: 1. 连接上mysql host 端口号 数据库 账号 密码2. 建立游标3. 执行sql(注意,如果是update,insert,delete 需要进行提交才能生效.)4. 获取结果5. 关闭连接.关闭游标 一.默认获取的结果是元祖 1 conn = pymysql.connect(host='localhost',user='root',passwd='123456',port=3306,db='sakila',charset='ut

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

1,在shell提示符下查看mysql指定数据库下的表等数据 2.批量删除mysql下指定数据库下的所有表,Ruiy完成绝对测试---通过Pass for tb in `echo "use se;show tables" | mysql -p321`;do mysql -e "drop table se.$tb" -p321;done 再进一步的完整语句为,过滤掉那个Tables_in_DBName for tb in `echo "use se;show