Can't Insert data into Access Table from Python - Solved
-
The
float
built-in only takes a single parameter, you are passing two, so that code will fail. Also you have specified two values in your insert statement but you are trying to pass only one.Victor pointed out my mistake. [edit] Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')
So perhaps your code should be:
cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])
[/edit]
Richard MacCutchan wrote:
Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:
cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')
So perhaps your code should be:
cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])
No, the 'extra' parentheses turn the two values to be passed into a single (2-element) Python Tuple, which is what the call wants passed. If this were the problem, Python should be throwing an error. I have written a fair bit of other code, adding data to SQLite tables, which is exactly parallel and works as it should.
-
Richard MacCutchan wrote:
Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:
cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')
So perhaps your code should be:
cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])
No, the 'extra' parentheses turn the two values to be passed into a single (2-element) Python Tuple, which is what the call wants passed. If this were the problem, Python should be throwing an error. I have written a fair bit of other code, adding data to SQLite tables, which is exactly parallel and works as it should.
-
You will have to do some debugging as it is impossible to guess what is happening on your system. Does the execute command return a value, such as the number of rows inserted?
It returns a cursor. Fetching the contents of that gets the following:
pyodbc.ProgrammingError: No results. Previous SQL was not a query.
That does make a certain amount of sense, given that nothing is happening :), but doesn't really help, particularly since the syntax I am using appears valid and works with Sqlite. I was hoping that someone else had run into and solved this specific problem, since I am out of ideas. [EDIT] Further research suggests that this 'error' is even less helpful, and may well be the normal result when executing something that is not a SELECT query. [/EDIT]
-
It returns a cursor. Fetching the contents of that gets the following:
pyodbc.ProgrammingError: No results. Previous SQL was not a query.
That does make a certain amount of sense, given that nothing is happening :), but doesn't really help, particularly since the syntax I am using appears valid and works with Sqlite. I was hoping that someone else had run into and solved this specific problem, since I am out of ideas. [EDIT] Further research suggests that this 'error' is even less helpful, and may well be the normal result when executing something that is not a SELECT query. [/EDIT]
-
It returns a cursor. Fetching the contents of that gets the following:
pyodbc.ProgrammingError: No results. Previous SQL was not a query.
That does make a certain amount of sense, given that nothing is happening :), but doesn't really help, particularly since the syntax I am using appears valid and works with Sqlite. I was hoping that someone else had run into and solved this specific problem, since I am out of ideas. [EDIT] Further research suggests that this 'error' is even less helpful, and may well be the normal result when executing something that is not a SELECT query. [/EDIT]
I have been playing with pyodbc and have successfully added records to my pre-built access database. Unfortunately that does not give any clues as to why your code fails. [edit] Created a RawValues table and successfully added a record with 2 values. [/edit]
-
I have been playing with pyodbc and have successfully added records to my pre-built access database. Unfortunately that does not give any clues as to why your code fails. [edit] Created a RawValues table and successfully added a record with 2 values. [/edit]
Can you post your working Python code, and what version/'bitness' of Access are you running?
-
Can you post your working Python code, and what version/'bitness' of Access are you running?
-
No need, I just discovered I am blind (and maybe you are). The following calls are missing the parentheses that make them into function calls:
conn2.commit
conn2.close
conn1.closeshould be:
conn2.commit()
conn2.close()
conn1.close()Of course! Thanks!!
-
No need, I just discovered I am blind (and maybe you are). The following calls are missing the parentheses that make them into function calls:
conn2.commit
conn2.close
conn1.closeshould be:
conn2.commit()
conn2.close()
conn1.close():laugh: I see it happens to the best of us.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
:laugh: I see it happens to the best of us.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP