Python 操作MySQL数据库执行增删改查

Python 操作MySQL数据库之新建数据库

"""
 * @Date : 2021-10-26 15:11
 * @Auth : xiaoshuai.zhu
 * @File :Python 操作MySQL数据库之新建数据库.py
 * @IDE :PyCharm
 * @Version 1.0
"""

# 导入pymysql模块
import pymysql

# 连接database
conn = pymysql.connect(host="192.168.9.11", user="root",password="eZo$LA$WOp422AV5",database="db1")

# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cur=conn.cursor()

# 定义要执行的SQL语句
cur.execute("CREATE DATABASE IF NOT EXISTS db0 DEFAULT CHARSET utf8;")

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

Python 操作MySQL数据库之插入表

# -*- coding: utf-8 -*-
"""
 * @Date : 2021-10-26 15:19
 * @Auth : xiaoshuai.zhu
 * @File :Python 操作MySQL数据库之插入表.py
 * @IDE :PyCharm
 * @Version 1.0
"""

# 导入pymysql模块
import pymysql

# 连接database
conn = pymysql.connect(host="192.168.9.11", user="root",password="eZo$LA$WOp422AV5",database="db0")

# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 定义要执行的SQL语句
sql = """
CREATE TABLE Users (
id INT auto_increment PRIMARY KEY ,
username CHAR(10) NOT NULL UNIQUE,
Phone_number varchar(11) NOT NULL,
user_age TINYINT NOT NULL,
user_email varchar(32) NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8;
"""
# 执行SQL语句
cursor.execute(sql)

# 关闭光标对象
cursor.close()

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

Python 操作MySQL数据库之执行单条Insert语句

# -*- coding: utf-8 -*-
"""
 * @Date : 2021-10-26 16:22
 * @Auth : xiaoshuai.zhu
 * @File :Python 操作MySQL数据库之执行单条Insert语句.py
 * @IDE :PyCharm
 * @Version 1.0
"""

# 导入pymysql模块
import pymysql

# 连接database
conn = pymysql.connect(host="192.168.9.11", user="root",password="eZo$LA$WOp422AV5",database="db0")

# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 定义要执行的SQL语句
insert=cursor.execute("insert into Users values(1,'PonfeyZhu1','156********','25','PonfeyZhu1@ponfey.com')")
print ('执行insert语句后受影响的行数为:',insert)

#另一种插入数据的方式,通过字符串传入值
#sql="insert into Users values(%s,%s,%s,%s,%s)"
#cursor.execute(sql,(1,'PonfeyZhu1',156********,25,'PonfeyZhu1@ponfey.com'))

#查询上一条insert的数据
cursor.execute('select * from Users where username="PonfeyZhu1";')
print(cursor.fetchone())

# 提交事务
conn.commit()

# 关闭光标对象

cursor.close()
print('SQL 语句执行成功')

Python 操作MySQL数据库之执行多条Insert语句

# -*- coding: utf-8 -*-
"""
 * @Date : 2021-10-26 16:37
 * @Auth : xiaoshuai.zhu
 * @File :Python 操作MySQL数据库之执行多条Insert语句.py
 * @IDE :PyCharm
 * @Version 1.0
"""

# 导入pymysql模块
import pymysql

# 连接database
conn = pymysql.connect(host="192.168.9.11", user="root",password="eZo$LA$WOp422AV5",database="db0")

# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

#另一种插入数据的方式,通过字符串传入值
sql="insert into Users values(%s,%s,%s,%s,%s)"
insert=cursor.executemany(sql,[(2,'PonfeyZhu2',156********,25,'PonfeyZhu2@ponfey.com'),(3,'PonfeyZhu3',156********,25,'PonfeyZhu3@ponfey.com')])
print ('执行insert语句后受影响的行数为:',insert)

#查询上一条insert的数据
cursor.execute('select * from Users where id="2";')
print(cursor.fetchone())

# 提交事务
conn.commit()

Python 操作MySQL数据库之执行delete语句

# -*- coding: utf-8 -*-
"""
 * @Date : 2021-10-26 16:45
 * @Auth : xiaoshuai.zhu
 * @File :Python 操作MySQL数据库之执行delete语句.py
 * @IDE :PyCharm
 * @Version 1.0
"""

# 导入pymysql模块
import pymysql

# 连接database
conn = pymysql.connect(host="192.168.9.11", user="root",password="eZo$LA$WOp422AV5",database="db0")

# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 定义要执行的SQL语句
delete=cursor.execute("delete from Users where id=3")
print ('执行insert语句后受影响的行数为:',delete)

#查询上一条insert的数据
cursor.execute('select * from Users where id="3";')
print(cursor.fetchone())

# 提交事务
conn.commit()

# 关闭光标对象

cursor.close()
print('SQL 语句执行成功')

Python 操作MySQL数据库之执行update语句

# -*- coding: utf-8 -*-
"""
 * @Date : 2021-10-26 16:41
 * @Auth : xiaoshuai.zhu
 * @File :Python 操作MySQL数据库之执行update语句.py
 * @IDE :PyCharm
 * @Version 1.0
"""

# 导入pymysql模块
import pymysql

# 连接database
conn = pymysql.connect(host="192.168.9.11", user="root",password="eZo$LA$WOp422AV5",database="db0")

# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 定义要执行的SQL语句
update=cursor.execute("update Users set user_age=26 where username='PonfeyZhu1'")
print ('执行insert语句后受影响的行数为:',update)

#查询上一条insert的数据
cursor.execute('select * from Users where username="PonfeyZhu1";')
print(cursor.fetchone())

# 提交事务
conn.commit()

# 关闭光标对象

cursor.close()
print('SQL 语句执行成功')

Python 操作MySQL数据库之数据库查询

# -*- coding: utf-8 -*-
"""
 * @Date : 2021-10-26 14:53
 * @Auth : xiaoshuai.zhu
 * @File :Python 操作MySQL数据库之数据库查询.py
 * @IDE :PyCharm
 * @Version 1.0
"""

# 导入pymysql模块
import cur as cur
import pymysql
# 连接database
conn = pymysql.connect(host="192.168.9.11", user="root",password="eZo$LA$WOp422AV5",database="db1")

# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cur=conn.cursor()

# 定义要执行的SQL语句
cur.execute("select * from Users;")

while 1:
    result=cur.fetchone()
    if result is None:
        #表示已经取完结果集
        break
    print (result)

# 关闭数据库连接
conn.close()
上一篇
下一篇