2016-03-27 30 views
1

PyMySQL ve Python ile çalışıyorum.Python - MySQL sorgumdaki hata nerede?

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

Bu, belirsiz bir sözdizimi hatası veriyor ve nasıl düzeltileceğine dair bir fikrim yok. değerlendirilen sorgu:

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 hatadır:

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 

Herhangi bir yardım çok takdir edilecektir. Şimdiden teşekkürler.

cevap

2

hiçbir sağ ayraç yoktur: - bunu yapmak için daha güvenli ve daha kolay yolu vardır - Bir yan not olarak

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^ 

, sorguya sorgu parametrelerini eklemek için biçimlendirme dize kullanmayın parametreli sorgu:

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

Teşekkürler! Artık hepimiz sıralandık. – Alex