Best approach to query SQL server for numpy Best approach to query SQL server for numpy numpy numpy

Best approach to query SQL server for numpy


Here's a minimal example, based on the other question that you linked to:

import pyodbcimport numpyconn = pyodbc.connect('DRIVER={SQL Server};SERVER=MyServer;Trusted_Connection=yes;')cur = conn.cursor()cur.execute('select object_id from sys.objects')results = cur.fetchall()results_as_list = [i[0] for i in results]array = numpy.fromiter(results_as_list, dtype=numpy.int32)print array


In the mean time, there is a better way. Check out the turbodbc package. To transform your result set into an OrderedDict of NumPy arrays, just do this:

import turbodbcconnection = turbodbc.connect(dsn="My data source name")cursor = connection.cursor()cursor.execute("SELECT 42")results = cursor.fetchallnumpy()

It should also be much faster than pyodbc (depending on your database, factor 10 is absolutely possible).


How about using pandas? For example:

import psycopg2import pandastry :    con = psycopg2.connect(    host = "host",    database = "innovate",    user = "username",    password = "password")except:    print "Could not connect to database."data = pandas.read_sql_query("SELECT * FROM table", con)