我想在SQLite中插入日期(作爲數據類型的文本)。這是我的設置:在SQLite中插入日期
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS practice_data(date TEXT, distance REAL, duration REAL, avg_pace REAL)')
date = str(input('Enter DATE (i.e. 2016-01-10): '))
distance = float(input('Enter TOTAL DISTANCE RAN (i.e 2.11): '))
duration = float(input('Enter TOTAL DURATION (i.e. 20.34): '))
avg_pace = float(input('Enter AVERAGE PACE (i.e. 10.44): '))
def data_entry():
c.execute("INSERT INTO practice_data(date,distance,duration,avg_pace) VALUES ({}, {}, {}, {})".format(date, distance, duration, avg_pace))
conn.commit()
c.close()
conn.close()
create_table()
data_entry()
當我運行該程序時,它工作正常。但是當我打開SQLite瀏覽器時,日期列將不正確。例如,如果我輸入「2016-11-06」,它將輸入「1999」。出於某種原因,它暗示' - '是減法。即使我已指定該日期的數據類型爲TEXT。
我已經看到,SQLite有一個日期時間選項,但我不知道如何實現它。
我目前使用Python3.5。我繼續前進並將input()更改爲raw_input(),並在Python 2.7中運行它。但是我仍然將它解釋爲' - '的結果與減法相同。 – kstullich
完美!修復了這個問題,謝謝! – kstullich