Menu

what is wrong with this query

Help
2010-04-26
2012-09-19
  • norman khine

    norman khine - 2010-04-26

    hello, i have a list of tables i want to drop:

    user_tables =

    drop_user_tables = """DROP TABLE IF EXISTS db2.%s"""

    try:

    cursor.execute(drop_user_tables, (x for x in user_tables))

    thanks

     
  • Cat slave

    Cat slave - 2010-04-26

    This will create a serie of queries

    DROP TABLE IF EXISTS db2.'notification'

    Note the "single-quote" in the name, making it a constant, instead of a
    variable.

    You can use something like:

    for sql in :

    cursor.execute(sql)

     
  • Andy Dustman

    Andy Dustman - 2010-04-26

    try:

    for x in user_tables:
        cursor.execute(drop_user_tables % x)
    

    or

    cursor.execute(drop_user_tables % ",".join(user_tables))
    

    i.e. you can't just throw a generator expression at an SQL statement and
    expect it to work.

    DROP TABLE (and other DDL statements) don't technically take SQL parameters,
    either, i.e. table names and the like cannot be passed as parameters.

     
  • norman khine

    norman khine - 2010-04-26

    thank you,

    cursor.execute(drop_user_tables % ",".join(user_tables))
    

    worked for me well.

    i was unaware that DROP table did not take parameters.

    again thanks

     
  • norman khine

    norman khine - 2010-04-27

    sorry, but what if i have this sql, how do i make it work?

    user_tables = ['notification', 'userNotification', 'product_comments',
    'product_donation_paypalTransaction', 'product_donation',
    'productList_recommended', 'productList_user_assoc',
    'profile_values']
    
    create_user_tables = 'CREATE TABLE 3a050eed.%s SELECT * FROM production.%s'
    
     
  • Jack Repenning

    Jack Repenning - 2010-07-30

    The "create table" case will have to be handled with one of the above
    solutions that include "for". You can't create many tables with a single
    statement.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.