proximo
proximo

Reputation: 45

loading floats into SQL server using executemany in python

I am reading in some sensor data from a source that gives me a list of floats. example:

sensor_data = [1.1, 2.2, 1.3, 2.5]

I want to load these values using pyodbc's "execute_many" function; however from testing, I have verified that the data needs to be in this format in order to be loaded:

formattedArray = [[(1.1)], [(2.2)], [(1.3)], [(2.5)]]

What is the simplest way to do this conversion? Or is there a better way to do what I am trying to do?

I realize that this problem is related to:

pyodbc AccessDB TypeError: ('Params must be in a list, tuple, or Row', 'HY000')

However I have tried doing that in my executemany method as well.

data = [1.1, 2.2, 3.3, 4.4]
conn = pyodbc.connect(myString) 
cur = conn.cursor()
cursor.fast_executemany = True
cursor.executemany('insert into testTable (col) values(?);'(data[0:3]))
conn.commit() 

I get this output:

TypeError: ('Params must be in a list, tuple, or Row', 'HY000')

Upvotes: 0

Views: 506

Answers (1)

vurmux
vurmux

Reputation: 10030

data = [1.1, 2.2, 3.3, 4.4]
data_new = [(elem,) for elem in data]

will return:

[(1.1,), (2.2,), (3.3,), (4.4,)]

Upvotes: 1

Related Questions