Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert data into MSSQL server using python

I am trying to insert data to the sql server table using following code,

import pyodbc
user='sa'
password='PC#1234'
database='climate'
port='1433'
TDS_Version='8.0'
server='192.168.1.103'
driver='FreeTDS'

   con_string='UID=%s;PWD=%s;DATABASE=%s;PORT=%s;TDS=%s;SERVER=%s;driver=%s' % (user,password, database,port,TDS_Version,server,driver)
   cnxn=pyodbc.connect(con_string)
   cursor=cnxn.cursor()
   cursor.execute("INSERT INTO mytable(name,address) VALUES (%s,%s)",('thavasi','mumbai'))
   cnxn.commit()

Its giving me the following error while executing,

   Traceback (most recent call last):
  File "sql.py", line 26, in <module>
  cursor.execute("INSERT INTO mytable(name,address) VALUES (%s,%s)",('thavasi','mumbai'))
 pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 2 parameters were supplied', 'HY000')

I have checked the syntax for the insert statement its correct. So what causes this error?

like image 450
amol desai Avatar asked Jun 02 '15 07:06

amol desai


1 Answers

cursor.execute("INSERT INTO mytable(name,address) VALUES (?,?)",('thavasi','mumbai'))

use ? instead of % in pyodbc module

like image 63
Dsw Wds Avatar answered Sep 30 '22 04:09

Dsw Wds