要粘貼整個文件,因爲我完全不知道如何解決我的問題;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和編程的世界,一般,謝謝!
在調用'connect()'後立即刪除除了那個之外的所有PRAGMA。然後刪除數據庫文件,然後重試。 (和「不工作」不是一個適當的錯誤描述。) –
我已經刪除了第三行中的每一個編譯指示(我假設這是你所指的那個),我也遇到了同樣的問題。當通過SQL數據庫瀏覽器查看創建的數據庫時,所有外鍵字段仍然顯示** NULL ** – Orbie