python - 无法使用pythons mysqldb执行mysql插入

标签 python mysql mysql-python

这是我在 stackoverflow 上的第一个问题! 我目前正在编写一个 Steam 社区爬虫程序,它会爬取 Steam 配置文件并保存诸如拥有的游戏、Steam 名称、 friend 等内容...... 但是当我尝试运行插入命令时,它什么也没做。

 if not self.check_user('games', id):
  print "insert"
  self.cursor.execute("INSERT INTO games (userid) VALUES(%s);" % id)

程序显示“插入”,但执行命令既没有抛出异常,也没有向数据库插入内容。 此外,不会捕获最终发生的异常。例如,当我将查询更改为“INSERT INTO games (useridd)”时,程序退出并且我看到异常。

该应用程序是多线程的,但在执行前获取,所以我看不出有任何问题。

最佳答案

  1. 在命令末尾添加“COMMIT”

    例如:

    cursor.execute("INSERT INTO games (userid) VALUES(%s);COMMIT;" % id)
    
  2. 添加第二行设置自动提交

    db = MySQLdb.connect(user='username', db=dbName,) # exmaple
    db.autocommit(1) # enable auto commit
    
  3. 在事务后添加commit()

    db.commit()
    

关于python - 无法使用pythons mysqldb执行mysql插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726521/

相关文章:

mysql - MySQL 中的方括号 LIKE

mysql - Mysql默认的服务器端取数是多少

python - zip 列表太多项目无法解压

python - Azure CLI 在 `az component update` 上失败

python - 如何将 'markersize' 参数添加到 DataFrame 图中?

python - httplib2在python中下载进度条

python - MySQL 选择产品的所有组件

python - 允许用户在服务器上执行python代码

MySQL 合并 2 行中的金额

php - Codeigniter Ajax请求与mysql一样查询编码字符串为字符