Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7617937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:16:13+00:00 2026-05-31T03:16:13+00:00

This is a bit of code I’m writing for a plugin for a bit

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:16:14+00:00Added an answer on May 31, 2026 at 3:16 am

    Try this

    from sqlalchemy import create_engine
    
    engine = create_engine('mysql://test:test@localhost/test', echo=True)
    from sqlalchemy.orm import sessionmaker
    
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Date, Column, Integer
    
    
    Base = declarative_base()
    
    
    
    class User(Base):
        __tablename__ = 'users'
    
        id = Column(Integer, primary_key=True, autoincrement=True)
        day = Column(Date, unique=True)
    
    
    if __name__ == '__main__':
    
        Base.metadata.create_all(engine)
        c = engine.connect()
        c.execute("""INSERT INTO users(day) VALUES ('2012-03-05') ON DUPLICATE KEY UPDATE day='2012-03-04'""")
    

    When i run this file first time i get log like

    2012-03-05 12:39:42,202 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
    2012-03-05 12:39:42,202 INFO sqlalchemy.engine.base.Engine ()
    2012-03-05 12:39:42,204 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%'
    2012-03-05 12:39:42,204 INFO sqlalchemy.engine.base.Engine ()
    2012-03-05 12:39:42,205 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
    2012-03-05 12:39:42,205 INFO sqlalchemy.engine.base.Engine ()
    2012-03-05 12:39:42,206 INFO sqlalchemy.engine.base.Engine SHOW COLLATION
    2012-03-05 12:39:42,206 INFO sqlalchemy.engine.base.Engine ()
    2012-03-05 12:39:42,209 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
    2012-03-05 12:39:42,209 INFO sqlalchemy.engine.base.Engine ()
    2012-03-05 12:39:42,210 INFO sqlalchemy.engine.base.Engine DESCRIBE `users`
    2012-03-05 12:39:42,210 INFO sqlalchemy.engine.base.Engine ()
    2012-03-05 12:39:42,211 INFO sqlalchemy.engine.base.Engine INSERT INTO users(day) VALUES ('2012-03-05') ON DUPLICATE KEY UPDATE day='2012-03-04'
    2012-03-05 12:39:42,211 INFO sqlalchemy.engine.base.Engine ()
    2012-03-05 12:39:42,212 INFO sqlalchemy.engine.base.Engine COMMIT
    

    Then i check in database

    mysql> select * from users;
    +----+------------+
    | id | day        |
    +----+------------+
    |  8 | 2012-03-05 |
    +----+------------+
    1 row in set (0.00 sec)
    

    Then I run the program again

    and check the mysql again.

    mysql> select * from users;
    +----+------------+
    | id | day        |
    +----+------------+
    |  8 | 2012-03-04 |
    +----+------------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This bit of code is taking almost a half second to execute. Could somebody
IS this bit of code in PHP/mysql considered a stored procedure? $sql = 'SELECT
I have this bit of code, which should be self explanatory: _item.Distance = Decimal.Round(dDistanceDec,
I have this bit of code: //execute post (the result will be something like
Is there a more elegant/shorter/organized way of writing this bit of code? for (int
I've got this bit of code that basically tries to use a SQL case
Okay consider this bit of code: var d1 = new Date(); var d2 =
This bit of code comes with new classes that are subclasses of UITableViewController... -
To this bit of code I pass the string kellogs special k and I
What does this bit of code represent? I know it's some kind of if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.