Python pymysql的使用

import pymysql

# 打开数据库连接
db = pymysql.connect(localhost, user, pwd, "orders")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# SQL 查询语句
eventId = '123456'
sql = "select * from so where doorEventId='%s'" % eventId
try:
    # 执行SQL语句
    cursor.execute(sql)
    # 获取所有记录列表
    results = cursor.fetchall()
    if len(results) > 0:
        for row in results:
           print(row[0])
    else:
        print("no result")
    # 提交事务(查询操作不需要提交)
    # db.commit()
except Exception as e:
    print("Error: unable to fetch data", e)
    # 回滚(查询操作不需要回滚)
    # db.rollback()
#关闭游标
cursor.close()
# 关闭数据库连接
db.close()

# 增
sql_insert =  "INSERT INTO operation(deviceCode,deviceName) VALUES(%s,%s)"
cursor.execute(sql_insert, ('1234', 'testDevice'))
或者
sql_insert = "INSERT INTO operation(deviceCode,deviceName) VALUES ('1234', 'testDevice')"
cursor.execute(sql_insert)
# 删
sql_delete = 'DELETE FROM info WHERE age > 28'
# 改
sql_update = "UPDATE device_operation SET status = %s WHERE ID = %s" % (status, operationId)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值