【问题标题】:Retrieving column value, using python-mysql, tornado framework检索列值,使用python-mysql,tornado框架
【发布时间】: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


    【解决方案1】:

    问题是您试图从.execute 方法中获取您的uid 值。请参考the Python Database API Specification v2.0 (PEP 249) section describing the .execute method,上面写着:

    返回值未定义。

    换句话说,不要使用这个方法的返回值——至少,除非你对模块足够熟悉,确切理解它是如何存在的实施。

    相反,您需要使用另一种方法从光标“获取”结果集The example section in the MySQL-Python User's Guide 展示了使用其中一种方法 .fetchone 来获取单行:

    要执行查询,首先需要一个游标,然后可以执行 对它的查询:

    c=db.cursor()
    max_price=5
    c.execute("""SELECT spam, eggs, sausage FROM breakfast
              WHERE price < %s""", (max_price,))
    

    ...

    现在,结果:

    >>> c.fetchone()
    (3L, 2L, 0L)
    

    如果您希望只能从特定查询中返回一行(例如,在聚合整个表时),使用 .fetchone 是合理的。如果行数较多,可以反复调用此方法,直到行数用完。然而,在许多情况下,您会希望使用.fetchall 来存储整个集合:

    >>> import MySQLdb
    >>> conn = MySQLdb.connect(user='foo', passwd='bar', db='test')
    >>> curs = conn.cursor()
    >>> curs.execute('create temporary table t (a int);')
    0L
    >>> curs.executemany('insert into t values (%s);', xrange(10))
    10L
    >>> curs.execute('select count(*) from t;')
    1L
    >>> curs.fetchall()
    ((10L,),)
    >>> curs.execute('select a from t where a % 2;')
    5L
    >>> curs.fetchall()
    ((1L,), (3L,), (5L,), (7L,), (9L,))
    >>> curs.fetchall()
    ()
    

    请注意,行只获取一次;如果您第二次调用.fetchall 而不执行另一个查询,您会得到一个空结果集(0 个元组的元组)。这意味着您应该存储 fetch 方法的返回值,以便以后访问它们。

    因此,要将其应用于您的示例,请替换以下行:

    uid = cur.execute("""Select userid from user_table where username = %s """, (username,))
    

    更多类似这样的东西:

    cur.execute("""Select userid from user_table where username = %s """, (username,))
    result = cur.fetchone()  # get a single row
    uid = result[0]  # get the first value from that row
    

    或者,使用.fetchall:

    cur.execute("""Select userid from user_table where username = %s """, (username,))
    result = cur.fetchall()  # get any/all rows
    uid = result[0][0]  # get the first value from the first row
    

    您使用哪种模式取决于查询、环境和您的个人品味。无论哪种情况,如果在表中找不到用户名,您可能还需要处理获取空集的可能性。

    【讨论】:

      【解决方案2】:

      您是否尝试将 %s 用引号括起来?另外,self.currentuser 周围是否可能有一些空白?

      我会尝试:

      uid = cur.execute("""SELECT userid FROM user_table WHERE username='%s'""", (username,))
      

      【讨论】:

      • 是的,我尝试在“”和“”中包含 %s,它将用户 ID 显示为 0,而不是 3
      猜你喜欢
      • 2010-12-27
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 2022-08-18
      相关资源
      最近更新 更多