Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. group_export.to_sql(con = db, name = config.table_group_export, if_exists = 'replace', flavor = 'mysql', index = False)
  2.  
  3. engine = sa.create_engine('postgresql:///somedb')
  4. pandas_sql = pd.io.sql.pandasSQL_builder(engine, schema=None, flavor=None)
  5.  
  6. def to_sql_k(self, frame, name, if_exists='fail', index=True,
  7. index_label=None, schema=None, chunksize=None, dtype=None, **kwargs):
  8. if dtype is not None:
  9. from sqlalchemy.types import to_instance, TypeEngine
  10. for col, my_type in dtype.items():
  11. if not isinstance(to_instance(my_type), TypeEngine):
  12. raise ValueError('The type of %s is not a SQLAlchemy '
  13. 'type ' % col)
  14.  
  15. table = pd.io.sql.SQLTable(name, self, frame=frame, index=index,
  16. if_exists=if_exists, index_label=index_label,
  17. schema=schema, dtype=dtype, **kwargs)
  18. table.create()
  19. table.insert(chunksize)
  20.  
  21. to_sql_k(pandas_sql, df2save, 'tmp',
  22. index=True, index_label='id', keys='id', if_exists='replace')
  23.  
  24. CREATE TABLE public.tmp
  25. (
  26. id bigint NOT NULL DEFAULT nextval('tmp_id_seq'::regclass),
  27. ...
  28. )
  29.  
  30. metadata = MetaData()
  31. metadata.reflect(db.engine, only=tableNamesDict.values())
  32. Base = automap_base(metadata=metadata)
  33. Base.prepare()
  34.  
  35. for df in dfs.keys():
  36. cols = dfs[df].columns
  37. cols = [str(col) for col in cols if 'id' in col.lower()]
  38. schema = pd.io.sql.get_schema(dfs[df],df, con=db.engine, keys=cols)
  39. db.engine.execute('DROP TABLE ' + df + ';')
  40. db.engine.execute(schema)
  41. dfs[df].to_sql(df,con=db.engine, index=False, if_exists='append')
  42.  
  43. alchemyClassDict = {}
  44. for t in Base.classes.keys():
  45. alchemyClassDict[t] = Base.classes[t]
  46.  
  47. res = db.session.query(alchemyClassDict['user']).first()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement