[Numpy-discussion] query to array
Wed Jan 31 17:04:02 CST 2007
i would do something like the following. I don't have your odbc
library so I mocked it up with a fake iterator called "it". This
example would be for a two column result where the first is an int and
the second a string. Note it creates a recarray which you can have
match you database column names if you want to generalize the
function. All you need to provide is the dtype equivalent to your
query result layout.
In : import numpy
In : it = iter([(1,'String1'), (2, 'String2')])
In : dtype = numpy.dtype([('intvalue', int), ('stringcolumn', '|S20')])
In : numpy.fromiter(it, dtype)
array([(1, 'String1'), (2, 'String2')],
dtype=[('intvalue', '<i4'), ('stringcolumn', '|S20')])
On 1/31/07, BBands <email@example.com> wrote:
> Good afternoon,
> The following works:
> import pyodbc
> import numpy as np
> connection = pyodbc.connect('DSN=DSNname')
> cursor = connection.cursor()
> symbol = 'ibm'
> request = "select to_days(Date), Close from price where symbol = '" +
> symbol + "' and date > '2006-01-01'"
> for row in cursor.execute(request):
> price.append([row, row])
> priceArray = np.array(price)
> Is there a better way to do this? Can fromiter be used?
> Could this query result be put into a wider array from the start such
> that columns 2... could be filled with calcs later?
> Thanks in advance,
> John Bollinger, CFA, CMT
> If you advance far enough, you arrive at the beginning.
> Numpy-discussion mailing list
More information about the Numpy-discussion