mysql cursor row_Python/MySQL:使用cursor.rowcount插入前后

我试图获取数据库表在插入新行之前和之后的行数。在基本级别上,我希望获取行数,插入一行,然后再次获取行数(即,应该再多1行)。但是,第二个电话cursor.rowcount始终返回与第一个相同的数字cursor.rowcount打电话。在

我的代码:from mysql.connector import MySQLConnection, Error

from single_api_insert import insert

def query_insert():

try:

con = MySQLConnection(host='localhost',

database='test',

user='root',

password='')

if con.is_connected():

print('Connected to database')

cursor = con.cursor()

cursor.execute("select * from testapi")

row = cursor.fetchall()

print(cursor.rowcount)

insert(2, 'mk', 'km')

print(cursor.rowcount)

except Error:

print(Error)

finally:

cursor.close()

con.close()

if __name__ == '__main__':

query_insert()

insert方法在我的表testapi中插入一行。如果表格为空,则应打印此内容

^{pr2}$

但行数不会增加到1。在

有没有更好的方法来完成这项工作,或者解决第二次rowcount不能正常工作的问题?在

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值