Python中pymysql基本使用

Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作

  优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚

  缺点:操作繁琐,代码量多

1. pymysql的基本使用

# -*- coding:utf-8 -*-
# Author:Wong Du

import pymysql

# 创建链接,相当于建立一个socket
conn = pymysql.Connection(host=‘10.0.0.100‘, port=3306, user=‘root‘, passwd=‘123456‘, db=‘testdb‘)

# 建立游标,相当于进入 mysql> 命令操作界面
cursor = conn.cursor()

# 建表,和mysql命令行操作一样
try:
    create_table = cursor.execute(‘‘‘create table student(
                                  id int not null primary key auto_increment,
                                  name char(32) not null,
                                  register_date date not null DEFAULT "2018-05-09" );
                                  ‘‘‘)
except pymysql.err.InternalError as e:
    # print(type(e))
    print("\033[31;1m%s; Do nothing...\033[0m" %e)

# 插入数据
insert = cursor.execute(‘insert into student (name,register_date) values("junry", "2017-03-14");‘)
insert2 = cursor.execute(‘insert into student (name,register_date) values("hongfa", "2015-03-14");‘)
insert3 = cursor.execute(‘insert into student (name,register_date) values("jinglin", "2016-03-14");‘)

# 查看表数据
select = cursor.execute(‘select * from student;‘)
for line in cursor.fetchall():
    print(line)

# 修改表数据
update = cursor.execute(‘update student set name="junwei" where id=1‘)
select2 = cursor.execute(‘select * from student;‘)
print(cursor.fetchone())

# 删除表数据
delete = cursor.execute(‘delete from student;‘)
select3 = cursor.execute(‘select * from student;‘)
if cursor.fetchall():
    print(cursor.fetchall())
else:
    print("This is a empty table...")

# 提交
conn.commit()

cursor.close()  # 关闭游标
conn.close()    # 关闭连接

# 等等 等等。。。

循环插入数据

# -*- coding:utf-8 -*-
# Author:Wong Du

import pymysql

# 建立连接
conn = pymysql.Connect(host=‘10.0.0.100‘, port=3306, user=‘root‘, passwd=‘123456‘, db=‘testdb‘)

# 创建游标
cursor = conn.cursor()

# 循环插入列表
many_list = [
    (‘zhangsan‘, ‘2011-11-11‘),
    (‘lisi‘, ‘2012-11-11‘),
    (‘wangwu‘, ‘2022-10-09‘),
]

# 循环插入(插入多条内容)
cursor.executemany("insert into student (name, register_date) VALUE(%s, %s);", many_list)

# 修改游标位置
cursor.scroll(1, mode=‘relative‘)   # 相对移动,默认为relative
cursor.scroll(1, mode=‘absolute‘)   # 绝对移动

# fetchone()获取一行数据、fetchmany(num)获取指定行数据、fetchall()获取所有行数据
cursor.execute("select * from student;")
for line in cursor.fetchall():
    print(line)

# 清楚student表的数据
cursor.execute("delete from student;")

# 提交
conn.commit()

cursor.close()  # 关闭游标
conn.close()    # 关闭连接

原文地址:https://www.cnblogs.com/Caiyundo/p/9578925.html

时间: 2024-08-28 18:34:55

Python中pymysql基本使用的相关文章

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

【转】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中Virtualenv和pip如何使用?

本文和大家分享的主要是python 中Virtualenv 和 pip的使用相关内容,一起来看看吧,希望对大家 学习python有所帮助. 0X00 安装环境 我们在Python 开发和学习过程中需要用到各种库,然后在各个不同的项目和作品里可能用的版本还不一样,正因为有这种问题的存在才催生了  virtualenv  的诞生.virtualenv 可以在电脑上创建一个虚拟环境,可以针对每一个项目创建一个虚拟环境,这样就不用担心各个不同的项目用不同版本的库的时候出现的冲突了.  下面的内容只适用于

python中MySQLdb模块用法实例

篇文章主要介绍了python中MySQLdb模块用法,以实例形式详细讲述了MySQLdb模块针对MySQL数据库的各种常见操作方法,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了python中MySQLdb模块用法.分享给大家供大家参考.具体用法分析如下: MySQLdb其实有点像php或asp中连接数据库的一个模式了,只是MySQLdb是针对mysql连接了接口,我们可以在python中连接MySQLdb来实现数据的各种操作. python连接mysql的方案有oursql.PyMyS

python的pymysql使用方法【转】

前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.6.6.mysql版本:5.7.17 一.安装 pip install pymysql 二.使用操作 创建测试环境 mysql> create database zst; Query OK, 1 row affected (0.03 sec) mysql> use zst Database changed

mysql数据库在Python中的操作方法

from pymysql import connect def check_data(cur): sql = "select * from student;" # sql语句 cur.execute(sql) # 执行sql语句 # 获取查询的所有记录 result = cur.fetchall() print(result) for value in result: print(value) def insert_data(cur, conn): sql = ""

mysql数据库在Python中的简单操作

from pymysql import connect def check_data(cur): sql = "select * from student;" # sql语句 cur.execute(sql) # 执行sql语句 # 获取查询的所有记录 result = cur.fetchall() print(result) for value in result: print(value) def insert_data(cur, conn): sql = ""

Python 使用 PyMysql、DBUtils 创建连接池提升性能

Python 使用 PyMysql.DBUtils 创建连接池提升性能 Python 编程中可以使用 PyMysql 进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接 MySQL 数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一定数量时,对 mysql 的性能会产生较大的影响.因此,实际使用中,通常会使用数据库的连接池技术,来访问数据库达到资源复用的目的. 解决方案:DBUtils DBUtils 是一套 Python 数据库连接池包,并允许对非线程安全的数据库接口