如何在mysql数据库中对fetchone()的输出列表进行交互



我目前正在创建一个类似银行账户的主菜单。

def main_menu():
print("Main Menu")
print("0 - Quit")
print("1 - Open Account")
print("2 - Check Balance")
print("3 - Close Account")

loop = True
while loop:
main_menu()
choice = input("Enter your choice: ")
choice = int(choice)
if choice == 0:
exit(loop)
elif choice == 1:
name_on_account = input("Name on account: ")
balance = float(input("Enter Initial Balance: "))
print("---Account successfully created---")
print("Account number:", account_no())
print("Name on account:", name_on_account)
print("Balance:", balance)
print("Account opened on:", now)
cur.execute("""
INSERT INTO account(name_on_account, balance) VALUES 
("%s", "%s");""", (name_on_account, balance))
connection.commit()
elif choice == 2:
print("Checking account balance")
account_number = input("Enter account number: ")
print("---Checking account balance---")
print("Account number:", account_number)
cur.execute("""SELECT * from account;
""")
account_no1 = cur.fetchone()
for i in account_no1[0]:
if account_number == i:
cur.execute("""select name_on_account from account where account_no = "%s";
""", (account_number,))
name1 = cur.fetchone()
print(name1)
name2 = ''.join(map(str,name1))
name3 = int(name2)
print("Name on account:", name3)
cur.execute("""select balance from account where account_no = "%s";
""", account_number)
balance1 = cur.fetchone()
balance2 = ''.join(map(str,balance1))
balance3 = int(balance2)
print("Balance:", balance3)
cur.expecute("""select account_open_date from account where account no = "%s";
""", account_number)
date1 = cur.fetchone()
date2 = ''.join(map(str, date1))
date3 = int(date2)
print("Account opened on:", date3)
connection.commit()
else:
print("Error: Invalid account number")

到目前为止,我并不担心选项3,但我在选项2方面遇到了麻烦。

当一个人选择选项1时,他们会输入自己的姓名和存入银行账户的金额。

这些信息将存储在mysql表帐户中(account_no、name_on_account、balance、account_open_date、account_status(。

这意味着account_no自动递增,account_open_date默认为curdate((,account_status默认为"打开"(。

然而,在选项2中,当一个人输入其账号时;它应该返回其在选项1中如何显示的所有信息。

我遇到的问题是,如何使用fetchone((有效地迭代人员的信息,并能够使用(account_no=account_number(获得特定的列信息(如果你对实现这一点的更好方法有更好的建议,请在下面评论(

这是我收到的错误消息:追踪(最近一次通话(:文件";(出于隐私目的删除(";对于account_no1[0]中的i:TypeError:"int"对象不是可迭代的

谢谢你的帮助!

pymysql(和大多数其他python-mysql客户端(在调用fetchone()时返回一个元组,元组中的每个条目都与查询表中定义的数据类型匹配。让我们看看你的代码:

elif choice == 2:
print("Checking account balance")
account_number = input("Enter account number: ")
print("---Checking account balance---")
print("Account number:", account_number)
cur.execute("""SELECT * from account""")
account_no1 = cur.fetchone()
for i in account_no1[0]:

在这里,查询account表中的所有列和所有行,读取第一行,然后迭代该行第一列中的内容。但这可能只是一个id列,这可能就是您得到错误的原因:整数是不可迭代的。这就像写for i in 10一样,Python不明白这意味着什么

如果你想迭代所有返回的行,你可以这样做:

cur.execute("""SELECT * from account;
""")
for row i in cur.fetchall():
# Each iteration "row" is a tuple of values corresponding to the table's columns

不过这有点傻。您需要返回数据库中的所有行,然后对它们进行迭代并查找特定的帐号。为什么不把它作为查询的一部分呢?

nrows = cur.execute("SELECT * from account where account_no=%s", (account_number,))
if nrows == 0:
# Account number not found, do something
else:
row = curs.fetchone()
# row now contains all of the values from the discovered 

请注意,您不需要在%s占位符周围加引号,也不需要分号。客户端为您完成所有这些转换。

还要注意,您不应该从表中选择*。如果表定义更改为包含更多列,该怎么办?只需选择您实际需要的少数列,然后如果稍后向表中添加更多列,则根本不需要更改代码,也不会请求超过所需的数据。所以更像:

nrows = cur.execute("SELECT name, balance, date_opened from account where account_no=%s", (account_number,))
if nrows > 0:
name, balance, date_opened = curs.fetchone()

最新更新