2012-04-29 20 views
3

Python 2.6.4 + ScrapyツールキットでWebスクレイパーを作成する。データ分析を行う必要があるだけでなく、私の最初のPython学習プロジェクトです。私のpipeline.pyにSQL INSERT文を作成するのに問題があります。実際のクエリには約30個の属性が挿入されます。構文エラー:Python + ScrapyのSQL INSERT?

まず、このUPDATEまたはINSERTアルゴリズムを記述するには、より良い方法がありますか?改善のためにオープン。

次に、2つの異なる構文のバリエーションと、それらが生成するさまざまなエラーを示します。私は例に基づいて多くのバリエーションを試しましたが、複数の行にまたがって "INSERT SET"を使用する例は見つかりません。適切な構文は何ですか?

DBは空ですので、今は 'INSERT'ブロックに分岐しています。

def _conditional_insert(self, tx, item): 
# create record if doesn't exist. 
tx.execute("SELECT username FROM profiles_flat WHERE username = %s", (item['username'][0],)) 
result = tx.fetchone() 

if result: 
    # do row UPDATE 
    tx.execute(\ 
     """UPDATE profiles_flat SET 
     username=`%s`, 
     headline=`%s`, 
     age=`%s` 
     WHERE username=`%s`""", ( \ 
     item['username'], 
     item['headline'], 
     item['age'],) 
     item['username'],) 
    )   
else: 
    # do row INSERT 
    tx.execute(\ 
    """INSERT INTO profiles_flat SET 
     username=`%s`, 
     headline=`%s`, 
     age=`%s` """, (\ 
     item['username'], 
     item['headline'], 
     item['age'],) # line 222 
    ) 

エラー:

[Failure instance: Traceback: <class '_mysql_exceptions.OperationalError'>: (1054, "Unknown column ''missLovely92 '' in 'field list'") 
    /usr/lib/python2.6/threading.py:497:__bootstrap 
    /usr/lib/python2.6/threading.py:525:__bootstrap_inner 
    /usr/lib/python2.6/threading.py:477:run 
    --- <exception caught here> --- 
    /usr/lib/python2.6/vendor-packages/twisted/python/threadpool.py:210:_worker 
    /usr/lib/python2.6/vendor-packages/twisted/python/context.py:59:callWithContext 
    /usr/lib/python2.6/vendor-packages/twisted/python/context.py:37:callWithContext 
    /usr/lib/python2.6/vendor-packages/twisted/enterprise/adbapi.py:429:_runInteraction 
    /export/home/raven/scrapy/project/project/pipelines.py:222:_conditional_insert 
    /usr/lib/python2.6/vendor-packages/MySQLdb/cursors.py:166:execute 
    /usr/lib/python2.6/vendor-packages/MySQLdb/connections.py:35:defaulterrorhandler 
    ] 

代替構文:

query = """INSERT INTO profiles_flat SET 
     username=`%s`, 
     headline=`%s`, 
     age=`%s` """ % \ 
    item['username'], # line 196 
    item['headline'], 
    item['age'] 

    tx.execute(query) 

エラー:

[Failure instance: Traceback: <type 'exceptions.TypeError'>: not enough arguments for format string 
    /usr/lib/python2.6/threading.py:497:__bootstrap 
    /usr/lib/python2.6/threading.py:525:__bootstrap_inner 
    /usr/lib/python2.6/threading.py:477:run 
    --- <exception caught here> --- 
    /usr/lib/python2.6/vendor-packages/twisted/python/threadpool.py:210:_worker 
    /usr/lib/python2.6/vendor-packages/twisted/python/context.py:59:callWithContext 
    /usr/lib/python2.6/vendor-packages/twisted/python/context.py:37:callWithContext 
    /usr/lib/python2.6/vendor-packages/twisted/enterprise/adbapi.py:429:_runInteraction 
    /export/home/raven/scrapy/project/project/pipelines.py:196:_conditional_insert 
    ]  

答えて

2

あなたがバッククォートで値を囲むべきではありません。バッククォートは列名を引用するために使用されます。

INSERT INTO profiles_flat (username, headline, age) 
VALUES (%s, %s, %s) 
+1

実際には、引用符で囲んではいけません。 – SingleNegationElimination

+0

最初の構文を変更してバックティックを削除しました。[Failure instance:Traceback::(1064、 'SQL構文に誤りがあります。あなたのMySQLサーバーのバージョンに近い構文を使用するために\ ')、\ n ...また、' INSERT INTO .. SET 'と' INSERT INTO .. VALUES 'の構文はUPDATEと類似しているためこれはPythonではできないのでしょうか? – Garrick

+0

@Garrick:さらに問題がある場合は、新しいコードを投稿してください。私は問題は、パラメータ化されたクエリの代わりに文字列補間を使用していることだと思います。 'tx.execute(query%params)'ではなく、 'tx.execute(query、params)'を使うべきです。 –