【发布时间】:2017-10-07 21:06:26
【问题描述】:
我一直在尝试从两个不同的表中返回值,但似乎无法让 c.execute(query) 函数返回我想要的值。目前我的代码将返回第一个 c.fetchone()[0],但第二个 fetchone()[5] 给出一个错误,它超出范围,这意味着它可能仍在尝试从我的“客户”表中获取数据它没有 6 列。我不认为我完全理解 MySQLdb 的工作原理,这很神奇,但找不到任何好的多表查询示例。这是我的代码 sn-p 下面!谢谢!
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()
【问题讨论】:
标签: python mysql mysql-python fetchall