This is a bit of code I’m writing for a plugin for a bit of software (Anki).
for i in date:
self.deck.s.execute(
"""CASE WHEN EXISTS(SELECT * FROM :table WHERE day=date( :i )) THEN
UPDATE :table SET WHERE day=date(:i)
ELSE
INSERT INTO :table (day, matureRise) VALUES ( date( :i ),1)
END""", {'table':STATSTABLE,'i':i})
The problem is that it keeps throwing an OperationalError. Specifically I’m getting:
sqlalchemy.engine.default", line 299, in do_execute
OperationalError: (OperationalError) near "CASE": syntax error u'CASE WHEN EXISTS(SELECT * FROM ? WHERE day=date( ? )) THEN\nUPDATE ? SET WHERE day=date(?)\nELSE\nINSERT INTO ? (day, matureRise) VALUES ( date( ? ),1) END' ('stats_2', (u'2011-05-03',), 'stats_2', (u'2011-05-03',), 'stats_2', (u'2011-05-03',))
I’ve tried a number of things but I haven’t been able to figure out whats wrong. I’m not very familiar with sqlalchemy so I’m wondering if maybe there’s some nuance in supplying raw SQL that I’m missing.
Try this
When i run this file first time i get log like
Then i check in database
Then I run the program again
and check the mysql again.