I'm trying to achieve the following. I want to create a python Class that transforms all tables in a database to pandas dataframes.
This is how I do it, which is not very generic...
class sql2df():
def __init__(self, db, password='123',host='127.0.0.1',user='root'):
self.db = db
mysql_cn= MySQLdb.connect(host=host,
port=3306,user=user, passwd=password,
db=self.db)
self.table1 = psql.frame_query('select * from table1', mysql_cn)
self.table2 = psql.frame_query('select * from table2', mysql_cn)
self.table3 = psql.frame_query('select * from table3', mysql_cn)
Now I can access all tables like so:
my_db = sql2df('mydb')
my_db.table1
I want something like:
class sql2df():
def __init__(self, db, password='123',host='127.0.0.1',user='root'):
self.db = db
mysql_cn= MySQLdb.connect(host=host,
port=3306,user=user, passwd=password,
db=self.db)
tables = (""" SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '%s' """ % self.db)
<some kind of iteration that gives back all the tables in df as class attributes>
Suggestions are most welcome...