2017-09-18 35 views
0

我剛開始學習python的SQLite3,我很難弄清楚爲什麼這不起作用。在Python中使用SQLite3

import sqlite3, os 

if not os.path.isfile("G:\\Python\\My first database.db"): 
    dtabse = sqlite3.connect("G:\\Python\\My first database.db") 
    cursr = dtabse.cursor() 

    cursr.execute("""CREATE TABLE Students 
    (first_name text, 
    surname text, 
    DOB text, 
    Form text) 
    """) 

    cursr.execute(""" INSERT INTO Students 
    VALUES ("Dave", "Edwards", "16", "11AB")""") 

    dtabse.commit() 
    dtabse.close() 
else: 
    dtabse = sqlite3.connect("G:\\Python\\My first database.db") 
    cursr = dtabse.cursor() 

    print(cursr.fetchall()) 

在我觀看的powerpoint中,它表示fetchall()應該檢索所有內容並顯示它。在這個程序的第一步中,它不會在這個目錄中找到一個文件,所以if區域被執行。當我下一次運行程序時,else區域將被執行。

這樣做很有效,首先去程序結束並開始。第二個去打印一個空列表,當我期待着桌子。我檢查了數據庫文件和數據,爲什麼我不能打印它?

回答

2

你需要一個SELECT語句來檢索你想要的數據。

+0

而且工作,謝謝。 – Krishi