2017-02-10 75 views
0

要粘貼整個文件,因爲我完全不知道如何解決我的問題;PRAGMA外鍵錯誤(Python)

import sqlite3 
import time 
import datetime 
import sys 

conn = sqlite3.connect('offerdatabase1.db') 

c = conn.cursor() 

c.execute('PRAGMA foreign_keys = ON') 

############################# Creating the Database Tables ############################# 

# Creating the 'Odds' Table 

def create_odds_table(): 

    c.execute("""CREATE TABLE IF NOT EXISTS Odds(OddsID INTEGER PRIMARY KEY, 
         TeamSelection TEXT, 
         BackOdds INTEGER, 
         LayOdds INTEGER) 
         """) 

    c.execute('PRAGMA foreign_keys = ON') 

# # # Creating the 'Value' Table # # # 

def create_value_table(): 

    c.execute("""CREATE TABLE IF NOT EXISTS Value(ValueID INTEGER PRIMARY KEY, 
         BackStake INTEGER, 
         LayStake INTEGER, 
         Liability INTEGER, 
         NetValue INTEGER) 

         """) 

    c.execute('PRAGMA foreign_keys = ON') 

# Creating the 'User' Table 

def create_user_table(): 

    c.execute("""CREATE TABLE IF NOT EXISTS User(UserID INTEGER PRIMARY KEY, 
         FirstName TEXT, 
         LastName TEXT, 
         Email TEXT, 
         Date TEXT, 
         Time TEXT) 
         """) 

    c.execute('PRAGMA foreign_keys = ON') 

# Creating the 'Offer' Table 

def create_offer_table(): 

    c.execute("""CREATE TABLE IF NOT EXISTS Offer(OfferID INTEGER PRIMARY KEY, 
         OfferType TEXT, 
         OfferDesc TEXT, 
         Bookmaker TEXT, 
         Exchange TEXT, 

         OddsID INTEGER, 
         ValueID INTEGER, 
         UserID INTEGER, 

         FOREIGN KEY(OddsID) REFERENCES Odds(OddsID), 
         FOREIGN KEY(ValueID) REFERENCES Value(ValueID), 
         FOREIGN KEY(UserID) REFERENCES User(UserID))""") 

    c.execute('PRAGMA foreign_keys = ON') 

# Running the Subroutines, in order to create the database with tables previously stated. 

if __name__ == "__main__": 

    db_name = ('offerdatabase1.db') 

    c.execute('PRAGMA foreign_keys = ON') 

    create_odds_table() 

    create_value_table() 

    create_user_table() 

    create_offer_table() 

############################# Inserting Data into Tables ############################# 

def data_entry_odds(): 

    print('==================== Odds and Team Selection ====================') 

    TeamSelection = input('Team you selected: ') 
    BackOdds = input('Back Bet Odds: ') 
    LayOdds = input('Lay Bet Odds: ') 

    c.execute("INSERT INTO Odds (TeamSelection, BackOdds, LayOdds) VALUES (?, ?, ?)", 
       (TeamSelection, BackOdds, LayOdds)) 

    c.execute('PRAGMA foreign_keys = ON') 

    conn.commit() 

def data_entry_value(): 

    print('================ Stakes, Liability and Net Value ================') 

    BackStake = input('Stake on Back Bet: ') 
    LayStake = input('Stake on Lay Bet: ') 
    Liability = input('Liability (applies only with exchange): ') 
    NetValue = input('Net value : ') 

    c.execute("INSERT INTO Value (BackStake, LayStake, Liability, NetValue) VALUES (?, ?, ?, ?)", 
       (BackStake, LayStake, Liability, NetValue)) 

    c.execute('PRAGMA foreign_keys = ON') 

    conn.commit() 

def data_entry_user(): 

    print('======================== User Information =======================') 

    FirstName = input('Firstname: ') 
    LastName = input('Surname: ') 
    Email = input('Email Address: ') 
    Date = time.strftime("%d/%m/%Y") 
    Time = time.strftime("%H:%M") 


    c.execute("INSERT INTO User (FirstName, LastName, Email, Date, Time) VALUES (?, ?, ?, ?, ?)", 
       (FirstName, LastName, Email, Date, Time)) 

    c.execute('PRAGMA foreign_keys = ON') 

    conn.commit() 

def data_entry_offer(): 

    print('======================= Offer Information =======================') 

    OfferType = input('Type of Offer: ') 
    OfferDesc = input('Offer Description: ') 
    Bookmaker = input('Name of Bookmaker: ') 
    Exchange = input('Name of Exchange: ') 

    c.execute("INSERT INTO Offer (OfferType, OfferDesc, Bookmaker, Exchange) VALUES (?, ?, ?, ?)", 
       (OfferType, OfferDesc, Bookmaker, Exchange)) 

    c.execute('PRAGMA foreign_keys = ON') 

    conn.commit() 

########################### Text Based User Interface ########################### 

def rootchoice(): 

    userchoice = input('Would you like to track a bet? (Y - Yes, N - No) ') 

    if userchoice.upper() == 'Y': 
     yeschoice() 

    elif userchoice.upper() == 'N': 
     nochoice() 

    else: 
     print('*ERROR* - Please enter either \'Y\' or \'N\' (no other characters accepted)') 
     rootchoice() 

def yeschoice(): 

    data_entry_user() 
    data_entry_offer() 
    data_entry_odds() 
    data_entry_value() 

    print('Data entry complete, recorded successfully.') 

    loopchoice() 

def nochoice(): 

    print('Thank you for using James\' Betting Tracker, goodbye!') 

    sys.exit() 

def loopchoice(): 

    loopuserchoice = input('Would you like to track another bet? (Y - Yes, N - No) ') 

    if loopuserchoice.upper() == 'Y': 
     yeschoice() 

    elif loopuserchoice.upper() == 'N': 
     nochoice 

    else: 
     print('*ERROR* - Please enter either \'Y\' or \'N\' (no other characters accepted)') 
     loopchoice() 

print('Welcome to James\' Betting Tracker!') 
rootchoice() 

打擾註釋和荒謬的標題,我正在寫這個代碼爲一個學校項目。在閱讀sqlite3中的外鍵主題後,我偶然發現了命令;

PRAGMA foreign_keys = ON 

閱讀圍繞這件事之後,有人告訴我,你必須設置PRAGMA foreign_keys爲ON,每次一個數據庫連接建立。

我已經這樣做了,但外鍵仍然不能與我的數據庫一起工作。

任何幫助將不勝感激,我是令人難以置信的新的python和編程的世界,一般,謝謝!

+0

在調用'connect()'後立即刪除除了那個之外的所有PRAGMA。然後刪除數據庫文件,然後重試。 (和「不工作」不是一個適當的錯誤描述。) –

+0

我已經刪除了第三行中的每一個編譯指示(我假設這是你所指的那個),我也遇到了同樣的問題。當通過SQL數據庫瀏覽器查看創建的數據庫時,所有外鍵字段仍然顯示** NULL ** – Orbie

回答

0

Foreign key constraints被稱爲「約束」,因爲它們是約束條件,即約束數據庫中的值。換句話說,它們阻止你插入違反規則的值。

在這種情況下,你會如果你試圖插入一個無效OddsIDValueIDUserID號(一個不父表存在)到Offers表得到一個錯誤。 但你永遠不會那樣做;你將這些列留空。

數據庫不可能自動在父表中插入對行的引用 - 應該選擇哪些行?

如果您的數據模型要求所有Offers行都有對其他三個表的有效引用,請將NOT NULL約束添加到這些列。

+0

我試圖將'NOT NULL'約束添加到'Offer'表中的所有相關列,發生同樣的問題。只是拋出和錯誤說NOT NULL約束失敗,執行暫停。 – Orbie

+0

此行爲是NOT NULL約束的目的。並看到我的答案的第三段。 –