【发布时间】:2015-01-20 18:59:19
【问题描述】:
我正在开发一个 Web 应用程序,使用 tornado 框架和 mysql-python 作为数据库连接器。
需求是这样的 - 当用户登录并说购买产品时,数据库有一个包含用户ID的表,以及所有与产品相关的列,应该更新。
所以有一个 usertable,productsbought 表。假设有 5 个用户 ID 为 1,2,3,4,5。 因此,当用户 3 购买产品时,productsbought 表将更新为用户 id 3 以及他购买的产品的所有详细信息。
解决问题。
username = self.current_user
print username # prints correct username say thirduser with id 3
db = MySQLdb.connect(host='localhost', user='root', db= 'db')
uid = None
cur = db.cursor()
uid = cur.execute("""Select userid from user_table where username = %s """, (username,))
print uid # should display 3, but displays 1
cur.execute("""INSERT INTO productsbought (userid, pid, pname, model) VALUES ( %s, %s, %s, %s)""", (uid, pid, pname, model,))
# The insert is working fine but for the wrong user.
现在这个 uid 应该理想地打印 3(对应于第三个用户的 id,他已经登录到应用程序)。但它的印刷 1.
因此,无论谁登录 - 他们的用户名都会正确显示,但他们的用户 ID 被视为 1,并且 productsbought 表正在为 firstuser 更新。
在mysql中我查询了
select userid from user_table where username = "thirduser"
并且正确显示 3。
所以一切看起来都很好,但有问题! 这让我发疯了。请帮帮我!!
【问题讨论】:
标签: python mysql tornado mysql-python data-retrieval