Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlAlchemy: export table to new database

Only recently started using python, and I like it! However, I am stuck with SqlAlchemy.

I am trying to write a script that reads an MS SQL database, query a table (all fields, only a filter on some fields), and write the results to a local SQLite database.

(The object is to write a data adapter: perform some queries on the SQLite database before exporting the results to another database. Writing to temporary table in the target database is also possible.)

I can make a connection and get query results - I can print them so I know that part works. But how can I create a new table based on the structure of the query results from the source SQL Server?

This works:

import sqlalchemy

esd = sqlalchemy.create_engine( 'mssql+pyodbc://username:passwordSservername/dbname' )
for row in esd.execute( 'select * from ticket_actions where log_dt > \'2012-09-01\''):
    print( row.eFolderID )

This also works:

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=servername;DATABASE=dbname;UID=username;PWD=password')
cursor = cnxn.cursor()
for row in cursor.execute( 'select * from ticket_actions where log_dt > \'2012-09-01\''):
    print( row.eFolderID )

Any ideas on how to create a new table with the same structure as the query has?

Thanks!

like image 980
Jerry Avatar asked Sep 19 '12 12:09

Jerry


People also ask

What does Create_all do in SQLAlchemy?

Sqlalchemy create_all method is used to create a new table into the database. This method will first check whether the table exists in the database or not if suppose it has found an existing table it will not create any table.

Is SQLAlchemy good for ETL?

One of the key aspects of any data science workflow is the sourcing, cleaning, and storing of raw data in a form that can be used upstream. This process is commonly referred to as “Extract-Transform-Load,” or ETL for short.

How do you drop a table in SQLAlchemy Python?

Just call drop() against the table object.

What is DB Create_all ()?

create_all() function to create the tables that are associated with your models. In this case you only have one model, which means that the function call will only create one table in your database: from app import db, Student.


1 Answers

See Creating and Dropping Database Tables:

Creating … individual tables can be done via the create() … method of Table.

For reading the source structure, see Reflecting Database Objects:

A Table object can be instructed to load information about itself from the corresponding database schema object already existing within the database.
[…]
The reflection system can also reflect views.

like image 149
CL. Avatar answered Oct 31 '22 05:10

CL.