2017-02-10 78 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约束的目的。并看到我的答案的第三段。 –