1
我想讀取一些Twitter數據並在Python 2.7中創建一些SQLite表。其中一個表(Tweets)有一個外鍵鏈接到用戶表中的主鍵ID。當我運行下面的程序的Python給了我這個錯誤:如果tweetDict在[關鍵] [ '',[]: KeyError異常: 'UserID_fk1'不知道如何在Python 2.7的表中存在外鍵時使用SQLite編寫插入語句
在這個問題上的任何幫助,非常感謝!
import sqlite3, json
conn = sqlite3.connect('Tweets_Database_A6.db')
c = conn.cursor()
localFD = open('alltweets.txt','r')
numLines = 10
createTable1 = '''CREATE TABLE User (
ID NUMBER(20),
Name VARCHAR(75),
Screen_name VARCHAR(75),
Description VARCHAR(200),
Friends_count INTEGER,
CONSTRAINT User_PK PRIMARY KEY (ID)
);'''
c.execute('DROP TABLE IF EXISTS User');
c.execute(createTable1)
createTable2 = '''CREATE TABLE Tweets (
ID NUMBER(20),
Created_At DATE,
Text CHAR(141),
Source VARCHAR(200) DEFAULT NULL,
In_Reply_to_User_ID NUMBER(20),
Retweet_Count NUMBER(10),
UserID NUMBER(20),
CONSTRAINT Tweets_PK PRIMARY KEY (id,text),
CONSTRAINT fk1_Tweets
FOREIGN KEY(UserID)
REFERENCES User(ID)
);'''
c.execute('DROP TABLE IF EXISTS Tweets')
c.execute(createTable2)
def readTweets(localFD, numLines, output = 'C:/Python27/tweettables.txt'):
if output:
outFD = open(output, 'w')
batchRows = 1
batchedInserts1 = []
batchedInserts2 = []
while numLines > 0:
line = localFD.readline()
numLines = numLines - 1
tweetDict = json.loads(line)
userDict = tweetDict['user']
newRow1 = []
newRow2 = []
tweetKeys = ['id','created_at','text','source','in_reply_to_user_id','retweet_count','UserID_fk1']
userKeys = ['id','name','screen_name','description','friends_count']
for key in userKeys:
if userDict[key] in ['',[]]:
newRow1.append(None)
else:
newRow1.append(userDict[key])
batchedInserts1.append(newRow1)
for key in tweetKeys:
if tweetDict[key] in ['',[]]:
newRow2.append(None)
else:
newRow2.append(tweetDict[key])
batchedInserts2.append(newRow2)
if len(batchedInserts2) >= batchRows or numLines == 0:
c.executemany('INSERT OR IGNORE INTO User VALUES(?,?,?,?,?)', batchedInserts1)
c.executemany('INSERT OR IGNORE INTO Tweets VALUES(?,?,?,?,?,?,?)', batchedInserts2)
batchedInserts1 = []
batchedInserts2 = []
if output:
outFD.write(line)
if output:
outFD.close()
readTweets(localFD, numLines)
localFD.close()
c.close()
conn.commit()
conn.close()