用某种格式打印出sqlite数据库查询的结果 [英] Print out the result of sqlite database query with a format

查看:22
本文介绍了用某种格式打印出sqlite数据库查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含四列的学生数据库:first_name、middle_name、last_name、school、birth_year.我已经使用 SELECT 语句成功读取了 db 的内容,但这里是我陷入困境的地方:

I have this student db with four columns: first_name, middle_name, last_name, school, birth_year. I have successfully read the content of the db using SELECT statement, but here's where I got stuck at:

如何使用如下格式打印查询结果:first_name middle_name(if available) last_name 出生于birth_year?所以对于查询中的每一行,预期的结果应该是这样的:Aida Blue 出生于 1985 年.

How do I print the query result with the format like this: first_name middle_name(if available) last_name was born in birth_year? So for every row in the query the intended result should do this: Aida Blue was born in 1985.

现在如果我运行 print(all_rows) 代码将打印这样的查询结果:[{'first_name':'Aida', 'middle_name': None, 'last_name': 'Blue', 'school': 'Washington', 'birth_year': 1981}, {...}, ....]

For now if I run print(all_rows) the code will print the query result like this: [{'first_name':'Aida', 'middle_name': None, 'last_name': 'Blue', 'school': 'Washington', 'birth_year': 1981}, {...}, ....]

以下是我对此类问题的代码尝试:

Below is my code attempt at such problem:

db = cs.SQL("sqlite:///students.db")

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("Usage: python student_school.py school")
        sys.exit()
    else:
        school = str(sys.argv[1])
        all_rows = db.execute("SELECT * FROM students WHERE school=?", school)
        first = all_rows[:][0] #I want to pick up the first col
        middle = all_rows[:][1] #if value <> None?
        last= all_rows[:][2]
        birth_year = all_rows[:][4]
        print(first_name, " " , middle_name, " ", last_name, "was born in ", birth_year \n)

谁能给点建议?谢谢!

推荐答案

  • 您的all_rows 变量字典列表.每个字典代表一个学生记录.
  • 您可以编写一个 for 循环来迭代它并打印每个循环.
  • 同样在print语句中,当你写','时,它会自动把分隔符放到(单个空格).无需显式编写 " ".与 \n 相同,无需显式编写 '\n'.在此处阅读更多信息 https://docs.python.org/3/library/functions.html#print.
    • Your all_rows variable list of dictionaries. Each dict representing a student record.
    • You can write a for loop to iterate over it and print each one.
    • Also in print statement when you write ',' it will automatically put seperater to (single space). There is no need to explicitly write " ". Same with \n there is no need to explicitly write '\n'. Read more here https://docs.python.org/3/library/functions.html#print.
    • db = cs.SQL("sqlite:///students.db")
      
      if __name__ == '__main__':
          if len(sys.argv) != 2:
              print("Usage: python student_school.py school")
              sys.exit()
          else:
              school = str(sys.argv[1])
              all_rows = db.execute("SELECT * FROM students WHERE school=?", school)
              for student in all_rows:
                  print(student['first_name'], student['middle_name'] if student['middle_name'] else '', student['last_name'], "was born in", student['birth_year'])
      

      这篇关于用某种格式打印出sqlite数据库查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆