无法打印查询结果

时间:2017-12-05 02:48:42

标签: python sql

query = c.execute('SELECT * FROM Patients')

resultset = c.fetchall()

if not resultset:
    print("Sorry, the last name you entered does not match any patients "
          "in the hospital.") #if there are no results this is printed
else:
    for result in resultset:
        result = ' '.join(result)
        print("\n\t", result)

我看到一个建议说要将其更改为','。加入([无])但我尝试了它并且它不起作用。

1 个答案:

答案 0 :(得分:1)

确保您已创建游标对象:

c = sqlite3.connect('databasename.db').cursor()
query = list(c.execute('SELECT * FROM Patients'))
#now, you can check query:

if not query  :
    print("Sorry, the last name you entered does not match any patients "
      "in the hospital.") #if there are no results this is printed
else:
   for val in [' '.join(i) for i in query]:
     print "\n\t{}".format(val)