Created
April 29, 2014 19:51
-
-
Save pawl/318114282bd16df36655 to your computer and use it in GitHub Desktop.
Copy Sybase IQ Table Schema to MySQL Using SQLalchemy and SQL Anywhere ODBC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlalchemy, sqlanydb | |
from sqlalchemy import create_engine, Table, Column, Integer, Unicode, MetaData, String, Text, update, and_, select, func, types | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.orm import sessionmaker | |
def connect(): | |
return sqlanydb.connect(DSN='<your DNS>', userid='<username>', password='<password>') | |
srcEngine = sqlalchemy.create_engine('sqlalchemy_sqlany://', creator=connect, echo=True) # pip install sqlalchemy-sqlany | |
# create a configured "Session" class | |
Session = sessionmaker(bind=srcEngine) | |
# create a Session | |
session = Session() | |
# create engine, reflect existing columns, and create table object for oldTable | |
srcEngine._metadata = MetaData(bind=srcEngine) | |
Base = declarative_base() | |
class srcTable(Base): | |
__table__ = Table('srcTable', srcEngine._metadata, Column('id', Integer, primary_key=True), autoload=True) # srcTable did not have primary key, you need to declare your own primary key (must be a column in the table) | |
# create engine and table object for newTable | |
destEngine = create_engine('mysql+mysqldb://user:[email protected]/newDB?charset=utf8', echo=True) | |
destEngine._metadata = MetaData(bind=destEngine) | |
destTable = Table('newTable', destEngine._metadata) | |
# copy schema and create newTable from oldTable | |
for column in srcTable.__table__.columns: | |
destTable.append_column(column.copy()) | |
destTable.create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment