How can i update or insert a SQL server table using pyodbc in Python.
I have two lists currently and this is how my code looks like:
column_name = ["ID","NAME","CLASS","SUBJECT"]
values = [123,"JOSEPH","ACCOUNT","BASICS"] # Values from one iteration
header_string = ', '.join('[{0}]'.format(w) for w in column_name)
var_string = ', '.join('?' * len(values))
query_string = 'INSERT INTO MY_TABLE(%s) values (%s);' % (header_string,var_string)
cursor.execute(query_string, values)
What i want to do is running the below SQL through cursor:
update test MY_TABLE NAME='JOHN' where ID=123
IF @@ROWCOUNT=0
insert into MY_TABLE ("ID","NAME","CLASS","SUBJECT") values(123,"JOSEPH","ACCOUNT","BASICS")
Can anyone suggest how can i do this ?