Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate table not working with SQL server sqlalchemy engine and pandas

I can successfully query and insert data using sqlalchemy and pandas:

from sqlalchemy import create_engine
import pandas as pd
engine = create_engine('mssql://myserver/mydb?driver=SQL+Server+Native+Client+11.0?trusted_connection=yes')

Read tempy table:

sql_command = """
select top 100 * from tempy
"""

df = pd.read_sql(sql_command, engine)
print df

   tempID  tempValue
0       1          2

Append new data:

df_append = pd.DataFrame( [[4,6]] , columns=['tempID','tempValue']) 
df_append.to_sql(name='tempy', con=engine, if_exists = 'append', index=False)

df = pd.read_sql(sql_command, engine)
print df

   tempID  tempValue
0       1          2
1       4          6

Try to truncate data:

connection = engine.connect()
connection.execute( '''TRUNCATE TABLE tempy''' )
connection.close()

Read table again, but truncate failed:

df = pd.read_sql(sql_command, engine)
print df

   tempID  tempValue
0       1          2
1       4          6
like image 455
scottlittle Avatar asked Feb 06 '17 23:02

scottlittle


People also ask

Can I use SQLAlchemy with SQL Server?

sqlalchemy, a db connection module for Python, uses SQL Authentication (database-defined user accounts) by default. If you want to use your Windows (domain or local) credentials to authenticate to the SQL Server, the connection string must be changed.

Does pandas use SQLAlchemy?

Pandas in Python uses a module known as SQLAlchemy to connect to various databases and perform database operations.

How do you update existing table rows in SQLAlchemy in Python?

Update table elements in SQLAlchemy. Get the books to table from the Metadata object initialized while connecting to the database. Pass the update query to the execute() function and get all the results using fetchall() function. Use a for loop to iterate through the results.


1 Answers

I have the same problem with pandas 0.19.2 and sqlalchemy 1.1.5.

As I see autocommit is not forced in engine.execute() when running a TRUNCATE statement. If I force it manually then TRUNCATE works perfectly:

from sqlalchemy.sql import text as sa_text

engine.execute(sa_text('''TRUNCATE TABLE tempy''').execution_options(autocommit=True))

It's fancy that DROP works perfectly without forcing autocommit...

like image 91
ragesz Avatar answered Sep 18 '22 15:09

ragesz