0

I have SQl statement List, when running the a single statement it work running the loop it gives:

pyodbc.ProgrammingError: ('42000', "[42000] [MySQL][ODBC 5.1 Driver][mysqld-5.5.8]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 'Sql_2' at line 1 (1064) (SQLExecDirectW)")

SQl = """Select something"""
    SQl_2 = """Select something"""
    SQl_3 = """Select something"""


Sqls= ('Sql','Sql_2','Sql_3')

for x in Sqls:
    print x
    use = Sql_2  
    # use = x
    cxn = pyodbc.connect('DSN=MySQL;PWD=xxx') 
    csr = cxn.cursor()
    csr.execute(use)
    fetch = csr.fetchall()

2 Answers 2

6

Your tuple should be

Sqls = (Sql,Sql_2,Sql_3)

instead of

Sqls = ('Sql','Sql_2','Sql_3')
Sign up to request clarification or add additional context in comments.

1 Comment

This could be made even simpler by creating the tuple in the for loop: for x in (Sql, Sql_2, Sql_3): ....
3

You should additionally move connecting to the database as well as creating a cursor out of the for-loop, since it's unneeded overhead.

SQl = """Select something"""
SQl_2 = """Select something"""
SQl_3 = """Select something"""

Sqls = (Sql, Sql_2, Sql_3)
cxn = pyodbc.connect('DSN=MySQL;PWD=xxx') 
csr = cxn.cursor()

for x in Sqls:
    print x
    use = Sql_2  
    # use = x    
    csr.execute(use)
    fetch = csr.fetchall()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.