Python | MySQLdb>我不能进入

2024-05-29 04:40:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个奇怪的问题。 有两个数据库。一个网上商店里有所有的产品。另一个有在线商店软件,只有活跃的产品。在

我想从db1获取值,在python中更改一些值并插入db2。在

db1.execute("SELECT * FROM table1")
for product in db1.fetchall():
    # ...
    db2.execute("INSERT INTO table2 ...")
    print "Check: " + str(db2.countrow)

因此,我可以通过select获得值,即使从db2中选择也没有问题。我的支票总是给我1,但表2中没有新行。自动增量值增加,但没有数据。 我甚至没有收到“无法插入”这样的错误 有人知道会出什么问题吗?(如果我通过phpmyadmin手动执行插入操作,它可以工作。。。如果我只是从脚本中提取sql并手动执行,sql语句也可以工作)

编辑:在此处找到答案How do I check if an insert was successful with MySQLdb in Python?

有没有一种方法可以让这些执行不需要每次都提交? 我有一个mysqldb的包装器,它对我来说非常适合,用commit改变行为,我需要做一些大的改变。在

EDIT2:好的,我发现db1是MyISAM(不需要提交就可以工作),db2是InnoDB(实际上似乎只适用于提交),我想我也必须将db2更改为MyISAM。在


Tags: infrom数据库executesql软件产品手动
1条回答
网友
1楼 · 发布于 2024-05-29 04:40:30

如果您使用的是InnoDB,请尝试在插入之后添加db2.commit()。在

Starting with 1.2.0, MySQLdb disables autocommit by default, as required by the DB-API standard (PEP-249). If you are using InnoDB tables or some other type of transactional table type, you'll need to do connection.commit() before closing the connection, or else none of your changes will be written to the database.

http://mysql-python.sourceforge.net/FAQ.html#my-data-disappeared-or-won-t-go-away

相关问题 更多 >

    热门问题