2016-03-27 12 views
1

私はPyMySQLとPythonを使っています。Python - 私のMySQLクエリのエラーはどこですか?

sql = "INSERT INTO accounts(date_string, d_day, d_month, d_year, trans_type, descriptor, inputs, outputs, balance, owner) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', AES_ENCRYPT('%s', 'example_key_str')" 
cur.execute(sql % (date, d_day, d_month, d_year, ttype, desc, money_in, money_out, bal, owner)) 

これは、曖昧な構文エラーをスローし、どのように修正するかわかりません。評価されたクエリは次のとおりです。

INSERT INTO accounts(date_string, d_day, d_month, d_year, trans_type, descriptor, inputs, outputs, balance, owner) VALUES ('12 Feb 2012', '12', 'Feb', '2012', 'CHQ', 'CHQ 54', '7143.78', '0.00', '10853.96', AES_ENCRYPT('[email protected]', 'example_key_str') 

MySQLのエラーは次のとおりです。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 

すべてのヘルプははるかに高く評価されるだろう。前もって感謝します。

答えて

2

何の閉じ括弧がありません:サイドノートとして

INSERT INTO 
    accounts 
    (date_string, d_day, d_month, d_year, 
    trans_type, descriptor, inputs, outputs, balance, 
    owner) 
VALUES 
    ('12 Feb 2012', '12', 'Feb', '2012', 
    'CHQ', 'CHQ 54', '7143.78', '0.00', '10853.96',   
    AES_ENCRYPT('[email protected]', 'example_key_str')) 
                HERE^ 

、クエリにクエリパラメータを挿入するように書式設定文字列を使用していない - それを行うには、より安全で便利な方法があります - パラメータ化クエリ

sql = """ 
    INSERT INTO 
     accounts 
     (date_string, d_day, d_month, d_year, 
     trans_type, descriptor, inputs, outputs, balance, 
     owner) 
    VALUES 
     (%s, %s, %s, %s, 
     %s, %s, %s, %s, %s, 
     AES_ENCRYPT(%s, 'example_key_str'))""" 
cur.execute(sql, (date, d_day, d_month, d_year, ttype, desc, money_in, money_out, bal, owner)) 
+0

ありがとうございます!私たちはすべて今ソートされています。 – Alex

関連する問題