Re: [sqlalchemy] Itterating over database row?

2016-02-04 Thread Simon King

> On 4 Feb 2016, at 18:19, Alex Hall  wrote:
> 
> Hello all,
> I'm setting my application up the way Simon suggested. I still use the
> table object so I can get its name for displaying in one list, but the
> other list (which holds the actual rows of the selected table) is
> using the relevant subclass of base.
> 
> I use wx.ListCtrl to display everything, and when I load the rows into
> their list, I first grab the column names from the table and set the
> list to have those columns. Then, I want to itterate over each row,
> extracting the value for the given column and displaying it. That's
> the problem: these objects are not itterable. I tried to use
> value(columnName), but that didn't work, even though the rows are
> returned from a query. Here's my attempt thus far.
> 
> def updateSelectedIndex(self, evt):
>  """When the index changes in the list of tables, the list of records
> is automatically populated with the newly-selected table's records."""
>  super(DBTablesListManager, self).updateSelectedIndex(evt)
>  self.records_listControl.ClearAll() #to avoid appending items--we
> want each table to display only its own data
>  table = self.choices[self.selectedIndex][0] #this is the
> sql.schema.Table object
>  self.records =
> DBInterface.session.query(self.schemas[self.selectedIndex]).all()
> #self.schema is the list of actual subclasses of base, not table
> objects
>  #set the column names in the records list
>  i = 0
>  for column in table.columns:
>   self.records_listControl.InsertColumn(i, column.name)
>   i += 1
>  #add the data
>  i = 0
>  for record in self.records:
>   recordData = record.value(table.columns[i]).__str__() #this line errors
>   self.records_listControl.InsertStringItem(i, recordData)
>   for j in range(1, len(record)):
>self.records_listControl.SetStringItem(i, j, record[j].__str__())
> #this line would fail too
>   i += 1
> 
> I'm either missing something obvious, or thinking about this all
> wrong, because itterating over a row has to be a pretty common need.
> Thanks in advance for any suggestions anyone has.

“record” is an instance of your mapped class. It doesn’t have any public 
methods other than those which you define yourself (so for instance it doesn’t 
have a “value()” method or an __iter__ method unless you define them). If you 
want to get a named attribute from it, use the standard python getattr function:

  recordData = getattr(record, colname)

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Itterating over database row?

2016-02-04 Thread Alex Hall
Ah, getattr()… I forgot that one! It's a bit messy, but my tables now fill 
correctly:
recordData = getattr(record, table.columns.keys()[i].__str__()).__str__()

Maybe one day I'll make my table schemas (is that the right term for them?) 
itterable, but this works for now. Thanks for the help. I assumed subclasses of 
base would be iterable so one could iterate over individual rows, but now I 
know they aren't, it'll be easy enough to add that in the future as I get more 
complex tables.
> On Feb 4, 2016, at 18:51, Simon King  wrote:
> 
> 
>> On 4 Feb 2016, at 18:19, Alex Hall  wrote:
>> 
>> Hello all,
>> I'm setting my application up the way Simon suggested. I still use the
>> table object so I can get its name for displaying in one list, but the
>> other list (which holds the actual rows of the selected table) is
>> using the relevant subclass of base.
>> 
>> I use wx.ListCtrl to display everything, and when I load the rows into
>> their list, I first grab the column names from the table and set the
>> list to have those columns. Then, I want to itterate over each row,
>> extracting the value for the given column and displaying it. That's
>> the problem: these objects are not itterable. I tried to use
>> value(columnName), but that didn't work, even though the rows are
>> returned from a query. Here's my attempt thus far.
>> 
>> def updateSelectedIndex(self, evt):
>> """When the index changes in the list of tables, the list of records
>> is automatically populated with the newly-selected table's records."""
>> super(DBTablesListManager, self).updateSelectedIndex(evt)
>> self.records_listControl.ClearAll() #to avoid appending items--we
>> want each table to display only its own data
>> table = self.choices[self.selectedIndex][0] #this is the
>> sql.schema.Table object
>> self.records =
>> DBInterface.session.query(self.schemas[self.selectedIndex]).all()
>> #self.schema is the list of actual subclasses of base, not table
>> objects
>> #set the column names in the records list
>> i = 0
>> for column in table.columns:
>>  self.records_listControl.InsertColumn(i, column.name)
>>  i += 1
>> #add the data
>> i = 0
>> for record in self.records:
>>  recordData = record.value(table.columns[i]).__str__() #this line errors
>>  self.records_listControl.InsertStringItem(i, recordData)
>>  for j in range(1, len(record)):
>>   self.records_listControl.SetStringItem(i, j, record[j].__str__())
>> #this line would fail too
>>  i += 1
>> 
>> I'm either missing something obvious, or thinking about this all
>> wrong, because itterating over a row has to be a pretty common need.
>> Thanks in advance for any suggestions anyone has.
> 
> “record” is an instance of your mapped class. It doesn’t have any public 
> methods other than those which you define yourself (so for instance it 
> doesn’t have a “value()” method or an __iter__ method unless you define 
> them). If you want to get a named attribute from it, use the standard python 
> getattr function:
> 
>  recordData = getattr(record, colname)
> 
> Hope that helps,
> 
> Simon
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.



--
Alex Hall
Automatic Distributors, IT Department
942-6769, Ext. 629
ah...@autodist.com

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Itterating over database row?

2016-02-04 Thread Alex Hall
Hello all,
I'm setting my application up the way Simon suggested. I still use the
table object so I can get its name for displaying in one list, but the
other list (which holds the actual rows of the selected table) is
using the relevant subclass of base.

I use wx.ListCtrl to display everything, and when I load the rows into
their list, I first grab the column names from the table and set the
list to have those columns. Then, I want to itterate over each row,
extracting the value for the given column and displaying it. That's
the problem: these objects are not itterable. I tried to use
value(columnName), but that didn't work, even though the rows are
returned from a query. Here's my attempt thus far.

 def updateSelectedIndex(self, evt):
  """When the index changes in the list of tables, the list of records
is automatically populated with the newly-selected table's records."""
  super(DBTablesListManager, self).updateSelectedIndex(evt)
  self.records_listControl.ClearAll() #to avoid appending items--we
want each table to display only its own data
  table = self.choices[self.selectedIndex][0] #this is the
sql.schema.Table object
  self.records =
DBInterface.session.query(self.schemas[self.selectedIndex]).all()
#self.schema is the list of actual subclasses of base, not table
objects
  #set the column names in the records list
  i = 0
  for column in table.columns:
   self.records_listControl.InsertColumn(i, column.name)
   i += 1
  #add the data
  i = 0
  for record in self.records:
   recordData = record.value(table.columns[i]).__str__() #this line errors
   self.records_listControl.InsertStringItem(i, recordData)
   for j in range(1, len(record)):
self.records_listControl.SetStringItem(i, j, record[j].__str__())
#this line would fail too
   i += 1

I'm either missing something obvious, or thinking about this all
wrong, because itterating over a row has to be a pretty common need.
Thanks in advance for any suggestions anyone has.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.