Python如何连接Mysql及基本操作

Python连mysql

1.为什么要做python连接mysql,一般是解决什么问题的

  做自动化测试时候,注册了一个新用户,产生了多余的数据,下次同一个账号就无法注册了,这种情况怎么办呢?自动化测试都有数据准备和数据清理的操作,如果因此用例产生了多余数据,就需要清理数据,可以用Pyhthon连接Mysql直接删除多余的数据就可以了。

  Python3如何连接Mysql呢?PyMySQL是在Py3版本用于连接Mysql.

2.python连接mysql的模块安装

第一种方法:在Pycharm---点击--Terminal---输入pip install PyMySQL等待完装完毕即可,如图所示



第二种方法:
离线安装 点击查看

3.如何连接MySql

import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "root", "111223", "study_date")

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()

print("Database version : %s " % data)

# 关闭数据库连接
db.close()

4.如何写查询语句,查询一行数据

# 导入模块,固定写法
import pymysql

# 打开数据库连接     数据库地址
db = pymysql.connect("localhost", "root", "111223", "study_date")

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
cursor.execute("select * from studys")

# 使用 fetchone() 方法获取一行数据.
data = cursor.fetchone()
print(data)

# 关闭数据库连接
db.close()

5.查询语句返回结果是多行的,如何实现。

# 导入模块,固定写法
import pymysql

# 打开数据库连接     数据库地址
db = pymysql.connect("localhost", "root", "111223", "study_date")

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
cursor.execute("select * from studys")

# 使用 fetchall() 方法获取所有数据.以元组形式返回
data = cursor.fetchall()
print(data)

# 关闭数据库连接
db.close()

6.如何做增、删、改操作

增加数据:

import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "root", "111223", "study_date")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

check_sql = 'select * from studys'
insert_sql = """insert into studys(id, name, age) values(6, '骑着乌龟赶猪', 30)"""

try:
    # 执行sql语句
    cursor.execute(insert_sql)
    # 提交到数据库执行
    db.commit()
    cursor.execute(check_sql)
    # 查看表里所有数据
    data = cursor.fetchall()
    print(data)
except:
    # 如果发生错误则回滚
    db.rollback()


# 关闭数据库连接
db.close()

删除数据:

import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "root", "111223", "study_date")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

check_sql = 'select * from studys'
# SQL 删除数据
del_sql = """delete from studys where id=6"""
try:
    # 执行sql语句
    cursor.execute(del_sql)
    # 提交到数据库执行
    db.commit()
    cursor.execute(check_sql)
    # 查看表里所有数据
    data = cursor.fetchall()
    print(data)
except:
    # 如果发生错误则回滚
    db.rollback()

# 关闭数据库连接
db.close()

修改数据:

import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "root", "111223", "study_date")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

check_sql = 'select * from studys'
# SQL 修改数据
updata_sql = """update studys set age=35 where id=4"""
try:
    # 执行sql语句
    cursor.execute(updata_sql)
    # 提交到数据库执行
    db.commit()
    cursor.execute(check_sql)
    # 查看表里所有数据
    data = cursor.fetchall()
    print(data)
except:
    # 如果发生错误则回滚
    db.rollback()

# 关闭数据库连接
db.close()

 如果对软件测试、接口测试、自动化测试、技术同行、持续集成、面试经验交流。感兴趣可以进到902061117,群内会有不定期的分享测试资料。
 如果文章对你有帮助,麻烦伸出发财小手点个赞,感谢您的支持,你的点赞是我持续更新的动力。
 

猜你喜欢

转载自www.cnblogs.com/zzpython/p/13365535.html