2016-03-27 9 views
1

Ich arbeite mit PyMySQL und Python.Python - wo ist der Fehler in meiner MySQL-Abfrage?

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)) 

Dies wirft einen vagen Syntaxfehler, und ich habe keine Ahnung, wie Sie es beheben. Die ausgewerteten Abfrage ist:

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') 

Der MySQL-Fehler ist:

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 

Jede Hilfe wäre sehr geschätzt. Danke im Voraus.

Antwort

2

Es gibt keine schließende Klammer:

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^ 

Als Randbemerkung, verwenden Zeichenfolge nicht die Formatierung der Abfrageparameter in die Abfrage einfügen - es ist ein sicherer und bequemer Weg, es zu tun - parametrierte Abfrage:

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

Danke! Wir sind jetzt alle sortiert. – Alex

Verwandte Themen