2016-10-04 86 views
-2

具有這兩種功能 在想,如果人能告訴我,我錯了 這是一個單獨的函數作爲蜘蛛的一部分,通過房價的網站搜索的麻煩sqlite的蟒蛇插入到表錯誤

def save_house_to_db(id, address, postcode, bedrooms): 
    conn = sqlite3.connect('houses_in_london.db') 
    d = conn.cursor() 
    d.execute('INSERT INTO TABLE houses (id, address, postcode, bedrooms) VALUES (%d %s %s %d)' %(id, str(address), str(postcode), float(bedrooms))) 
    d.commit() 
    d.close() 

def save_transactions_to_db(id, sale_price, date): 
    conn = sqlite3.connect('houses_in_london.db') 
    d = conn.cursor() 
    d.execute('INSERT INTO TABLE transactions (transaction_id NOT NULL AUTO_INCREMENT, house_id, date, sale_price) VALUES' 
       '(%d %s %s)' %(id, sale_price, str(date))) 
    d.commit() 
    d.close() 

這裏是引發的錯誤:

Traceback (most recent call last): 
    File "/Users/saminahbab/Documents/House_Prices/final_spider.py", line 186, in <module> 
    final_function(link_set=areas,id_counter=40) 
    File "/Users/s/Documents/House_Prices/final_spider.py", line 158, in final_function 
    page_stripper(link=(root+page), id_counter=id_counter) 
    File "/Users/s/Documents/House_Prices/final_spider.py", line 79, in page_stripper 
    save_house_to_db(id=float(id_counter), address=address, postcode=postcode, bedrooms=bedrooms) 
    File "/Users/s/Documents/House_Prices/final_spider.py", line 25, in save_house_to_db 
    d.execute('INSERT INTO TABLE houses VALUES (%d %s %s %d)' %(id, str(address), str(postcode), float(bedrooms))) 
sqlite3.OperationalError: near "TABLE": syntax error 

和參考這裏是執行的DATABSE

# conn = sqlite3.connect('houses_in_london.db') 
# database = conn.cursor() 
# database.execute('CREATE TABLE houses (id INTEGER PRIMARY KEY, address TEXT,' 
#     'postcode TEXT, bedrooms TEXT)') 
# 
# database.execute('CREATE TABLE transactions (transaction_id NOT NULL AUTO_INCREMENT, house_id INTEGER ' 
#     ' REFERENCES houses(id), date TEXT, sale_price INTEGER)') 

一如既往,感謝大家的支持

+0

在插入'房屋'時,你是否有理由不讓數據庫生成整數主鍵,但在插入時顯式傳遞它們。 –

回答

1

你有很多問題:

以下功能修復一些錯誤,將DDL更正限制在transactions表中。

def save_house_to_db(id, address, postcode, bedrooms): 
    conn = sqlite3.connect('houses_in_london.db') 
    d = conn.cursor() 
    # Remove the TABLE "keyword" 
    d.execute('INSERT INTO houses (id, address, postcode, bedrooms) ' 
       'VALUES (?, ?, ?, ?)', (id, address, postcode, bedrooms)) 
    d.commit() 
    d.close() 

def save_transactions_to_db(id, sale_price, date): 
    conn = sqlite3.connect('houses_in_london.db') 
    d = conn.cursor() 
    # This here expects that you've fixed the table definition as well 
    d.execute('INSERT INTO transactions (house_id, date, sale_price) ' 
       'VALUES (?, ?, ?)', (id, sale_price, date)) 
    d.commit() 
    d.close() 
+0

感謝Lija,現在經歷了更正,非常感謝。我沒有爲房屋做一個自動發電機的原因是我認爲在交易表中輸入交易時我需要這個數字作爲外鍵,並且這將是最有效的方法 – entercaspa

+0

看看[這裏]( http://stackoverflow.com/questions/6242756/how-to-retrieve-inserted-id-after-inserting-row-in-sqlite-using-python)關於如何通過遊標在python sqlite3中獲取最後插入的行ID 。然後,您可以讓數據庫生成該id並將其用於相關的「事務」。 –

+0

謝謝Lija,這非常有幫助 – entercaspa