2017-03-07 87 views
0

我有一個非常基本的腳本下面搜索數據庫表和檢索數據取決於什麼是輸入然後顯示結果,但結果是用逗號分隔在每個字段周圍的引號是有一種方式,它可以更好地顯示可能是使用HTML。python腳本來搜索和顯示來自sqlite數據庫的數據和結果到屏幕的結果

import sqlite3 
conn = sqlite3.connect("lucyartists.db") 
cursor = conn.cursor() 

#Get user input 
print ('Search for an Artist') 
print ('') 
print ('You can search by Name, DOB, Movement or Description') 
print ('') 
name = input("Enter Artists Name: ") 
dob = input("Enter Date of Birth: ") 
movement = input("Enter Movement: ") 
description = input ("Enter Description: ") 
sql = ("select * from artists where name like (?) or dob like (?) or movement like (?) or description like (?)") 
values = (name, dob, movement, description) 
cursor.execute(sql, values) 
result = cursor.fetchall() 
for r in result: 
print(r) 
+1

歡迎StackOverflow上。請閱讀並遵守幫助文檔中的發佈準則。 [在主題](http://stackoverflow.com/help/on-topic)和[如何提問](http://stackoverflow.com/help/how-to-ask)適用於此處。 – Prune

+0

簡單的格式化可以通過使用選項卡來實現。 HTML也可以很容易地生成。 – frederick99

回答

0

首先,print(r)需要縮進。 Python代碼塊由它們的縮進定義。

參見str.format文檔以下4個領域here

例子:

for r in result: 
    print("Name: {}".format(r["name"]) 
    print("DOB: {}".format(r["dob"]) 
    print("Movement: {}".format(r["movement"]) 
    print("Description: {}".format(r["description"]) 
    print() # extra line between rows of output 

你可以做到這一切在一行如下:

for r in result: 
    print("Name: {} DOB: {} Movement: {} Description: {}".format(r["name"], r["dob"], r["movement"]), r["description"]) 
    print() # extra line between rows of output