SteveJ
SteveJ

Reputation: 3323

pypyodbc cursor.execute() List index out of range

It's late, I'm tired - I hope I am just making a stupid mistake but I can't seem to see it. Can anyone see what I may be doing wrong? If I run the SQL on SQL Server Management studio, it works fine. I can run other SQL statements against the cursor, just not this one.

Thanks in advance

System:

Windows 7, Python 3.4, SQLServer 2008, pypyodbc

Query:

IF EXISTS(SELECT * FROM sys.databases WHERE name='{MyDBName}')
BEGIN
 ALTER DATABASE {MyDBName} SET SINGLE_USER WITH ROLLBACK IMMEDIATE; 
 DROP DATABASE {MyDBName}; 
END

CODE:

cursor.execute(query)
cursor.commit()

EXCEPTION:

ex = {IndexError} list index out of range

Upvotes: 2

Views: 3587

Answers (1)

SteveJ
SteveJ

Reputation: 3323

For posterity sake, I finally found my way through it and will share in case anyone reads this later:

It turns out that I need the autocommit property set to true when making the connection:

auto_commit = true
with pypyodbc.connect(cnx, auto_commit) as connection:
     cursor = connection.cursor()              

     cursor.execute(query)
     cursor.commit()

Note also that I change my query to just:

DROP DATABASE {MyDBName};

However, that alone didn't fix it without the auto-commit.

I hope that is helpful to someone, time for bed : )

Upvotes: 4

Related Questions