美文网首页
python中mysql的增删改查

python中mysql的增删改查

作者: 杜大个 | 来源:发表于2018-09-06 09:19 被阅读0次

增删改

from pymysql import *

def main():
# 创建Connection连接
conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8')
# 获得Cursor对象
cs1 = conn.cursor()
# 执行insert语句,并返回受影响的行数:添加一条数据
# 增加
count = cs1.execute('insert into goods_cates(name) values("硬盘")')
#打印受影响的行数
print(count)

count = cs1.execute('insert into goods_cates(name) values("光盘")')
print(count)

# # 更新
# count = cs1.execute('update goods_cates set name="机械硬盘" where name="硬盘"')
# # 删除
# count = cs1.execute('delete from goods_cates where id=6')

# 提交之前的操作,如果之前已经之执行过多次的execute,那么就都进行提交
conn.commit()

# 关闭Cursor对象
cs1.close()
# 关闭Connection对象
conn.close()

if name == 'main':
main()
查询一行数据
from pymysql import *

def main():
# 创建Connection连接
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 获得Cursor对象
cs1 = conn.cursor()
# 执行select语句,并返回受影响的行数:查询一条数据
count = cs1.execute('select id,name from goods where id>=4')
# 打印受影响的行数
print("查询到%d条数据:" % count)

for i in range(count):
    # 获取查询的结果
    result = cs1.fetchone()
    # 打印查询的结果
    print(result)
    # 获取查询的结果

# 关闭Cursor对象
cs1.close()
conn.close()

if name == 'main':
main()

查询多行数据

from pymysql import *

def main():
# 创建Connection连接
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 获得Cursor对象
cs1 = conn.cursor()
# 执行select语句,并返回受影响的行数:查询一条数据
count = cs1.execute('select id,name from goods where id>=4')
# 打印受影响的行数
print("查询到%d条数据:" % count)

# for i in range(count):
#     # 获取查询的结果
#     result = cs1.fetchone()
#     # 打印查询的结果
#     print(result)
#     # 获取查询的结果

result = cs1.fetchall()
print(result)

# 关闭Cursor对象
cs1.close()
conn.close()

if name == 'main':
main()

相关文章

  • mysql的插入语句

    MySQL增删改查之增insert、replace

  • MYSQL数据库的增删改查

    MYSQL数据库的增删改查 一.对于库的增删改查 增create database 库名称;create data...

  • 关于python的list的增查删改

    说到增查删改,想起了数据库,我们在关系型数据库当中就会对表进行增查删改。 在python当中我们也可以对list进...

  • python连接Mysql使用连接池

    python连接Mysql使用连接池 1、问题 当我们在Python中连接Mysql时,每次增、删、改、查如果都申...

  • node.js操作mysql学习笔记

    普通连接的增删改查 查 改 增 连接池 Pool options //mysql的github直接拷贝的,创建po...

  • mysql与python交互之封装

    封装 '''python操作mysql进行增删改查的封装 1、增删改,代码类似 2、查询 代码分析 1、获取连接对...

  • python基础-03

    python操作mysql数据库 创建数据表 数据表的 增 删 查 改 增 查 改 删

  • python中mysql的增删改查

    增删改 from pymysql import * def main():# 创建Connection连接conn...

  • Python实现MySQL的增删改查

    Python实现MySQL的增删改查 本文是我们《手把手教你用Python实现接口自动化测试》系列文章中的支线文章...

  • 2018-07-04

    python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作 python3使用pym...

网友评论

      本文标题:python中mysql的增删改查

      本文链接:https://www.haomeiwen.com/subject/cbytgftx.html