我一直试图从两个不同的表返回值,但似乎无法让c.execute(query)函数返回我想要的值。当前,我的代码将返回第一个c.fetchone()[0],但第二个fetchone()[5]给出一个错误,即它超出范围,这意味着它可能仍在尝试从没有6列的“clients”表中获取数据。我不认为我完全理解MySQLdb是如何工作的——这很神奇,但我找不到任何好的多表查询示例。下面是我的代码片段!谢谢
c, conn = connection()
#check if already exists
x = c.execute("SELECT * FROM clients WHERE email = (%s)", (thwart(email),))
if int(x) > 0:
flash("That email already has an account, please try a new email or sign in.")
return render_template('register.html', form=form)
else:
c.execute("INSERT INTO clients (email, phone, password) VALUES (%s, %s, %s)", (thwart(email), thwart(phone), thwart(password)))
c.execute("SELECT cid FROM clients WHERE email = (%s)", (thwart(email),))
clientcid = c.fetchone()[0]
c.execute("INSERT INTO cpersonals (first_name, last_name, address, zip) VALUES (%s, %s, %s, %s)", (thwart(first_name), thwart(last_name), thwart(address), czip))
c.execute("SELECT reg_date FROM cpersonals WHERE cid = (%s)", (clientcid,))
reg_date = c.fetchone()[5]
rating = c.execute("SELECT rating FROM clients WHERE email = (%s)", (thwart(email),))
conn.commit()
flash("Thanks for registering!")
c.close()
conn.close()
您的查询是
SELECT reg_date FROM cpersonals ...
。您只选择了一列。fetchone()[5]
失败的原因是,获取的记录中没有第6列。尝试用0
代替5
你为什么用
5
相关问题 更多 >
编程相关推荐