收到错误:“试图将主键列'id'重新定义为表上的非主键列...”

时间:2018-12-10 03:48:07

标签: json sqlite dictionary dataset tweepy

我正在尝试运行此脚本:

import tweepy
import json
import dataset

API_KEY = 'IEwf0YtuQBvBwvhIGWjStZUsY'
API_SECRET = '7OPNAoeILOE4QvoRBzJkrea1d2FVq4gDPw5AFhzPxwNl8V9bWu'
TOKEN_KEY = '979886346364895233-TV3VFWJc5SEm8zhPBNXr22JF8VOcin3'
TOKEN_SECRET = 'u70diOL7NEzdqqPu0uVs3wzOoRodPkTmWazkxbuDPcSQi'

def store_tweet(item):
    db = dataset.connect('sqlite:///data_wrangling.db')
    table = db['tweets']
    item_json = item._json.copy()
    for k, v in item_json.items():
    if isinstance(v, dict):
        item_json[k] =  str(v)
    table.insert(item_json)

auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(TOKEN_KEY, TOKEN_SECRET)

api = tweepy.API(auth)

query = '#childlabor'
cursor = tweepy.Cursor(api.search, q=query, lang="en")

for page in cursor.pages():
    for item in page:
    store_tweet(item)

,我在下面收到此错误。我了解sqlite不支持保存python词典,并且不确定是否尝试了此操作?请问如何解决此错误?

Traceback (most recent call last):
  File "advanced_data_pull.py", line 30, in <module>
    store_tweet(item)
  File "advanced_data_pull.py", line 17, in store_tweet
    table.insert(item_json)
  File "/home/ryan/.local/lib/python2.7/site-packages/dataset/table.py", line 79, in insert
    row = self._sync_columns(row, ensure, types=types)
  File "/home/ryan/.local/lib/python2.7/site-packages/dataset/table.py", line 281, in _sync_columns
    self._sync_table(sync_columns)
  File "/home/ryan/.local/lib/python2.7/site-packages/dataset/table.py", line 247, in _sync_table
    self._table.append_column(column)
  File "/home/ryan/.local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 704, in append_column
    column._set_parent_with_dispatch(self)
  File "/home/ryan/.local/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 430, in _set_parent_with_dispatch
    self._set_parent(parent)
  File "/home/ryan/.local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 1376, in _set_parent
    self.key, table.fullname))
sqlalchemy.exc.ArgumentError: Trying to redefine primary-key column 'id' as a non-primary-key column on table 'tweets'

0 个答案:

没有答案