This is my python code:
cx=sqlalchemy.create_engine(u"mysql://username:password@ipaddress/database?charset=utf8")
metadata=sqlalchemy.MetaData(cx)
orm_obj=sqlalchemy.Table(u'I_POI',metadata,autoload=True)
sql=orm_obj.select(u'poi_id,poi_name').where(u'poi_id>1 and poi_id>0').limit(3).offset(0)
resultz=sql.execute()
for i in resultz:
print i
[DB] I_POI Table:poi_id,poi_name,poi_data1,poi_data2......poi_data10
I do it with existing database,but "select()" is no work..It still return total columns.
I want to get only some columns,please help me.
The select() method of table object enables us to construct SELECT expression. The resultant variable is an equivalent of cursor in DBAPI. We can now fetch records using fetchone() method. Here, we have to note that select object can also be obtained by select() function in sqlalchemy.
The second one, filter_by(), may be used only for filtering by something specifically stated - a string or some number value. So it's usable only for category filtering, not for expression filtering. On the other hand filter() allows using comparison expressions (==, <, >, etc.)
There is a tool called sqlalchemy-migrate which works this way; you have a Table object, you say, "table. remove_column(col)", and it emits an "ALTER TABLE" which drops that column.
To access the column names we can use the method keys() on the result. It returns a list of column names.
here is the code that works for me:
from sqlalchemy import select
from sqlalchemy.sql import and_
results = select([orm_obj.c.poi_id, orm_obj.c.poi_name])\
.where(and_(orm_obj.c.id > 1, orm_obj.c.id < 100)).execute()
for id, name in results:
print id, name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With