Home > Blockchain >  Python adding user input to SQL database
Python adding user input to SQL database

Time:09-04

I have been trying to add certain ices of user inputs to an SQL database(name, date, id, and description)

name = input("enter your name here: ")
idx = input("enter Id:")
date= datetime.date
describe = input("enter brief description: ")

Here is my pyodbc connect lines:

with pyodbc.connect('Driver='   driver   ';SERVER=tcp:'   server   ';PORT=1433;DATABASE='   database   ';UID='   username   ';PWD='   password) as conn:
  with conn.cursor() as cursor:
    sql = ("Insert Into dbo.Report (Name, Date, id_number, decription)=? Values(?,?,?,?)")
    cursor.execute(sql, name, date, idx, describe)

but after running it I get this error:

cursor.execute(sql, name, date, idx, describe) pyodbc.ProgrammingError: ('The SQL contains 5 parameter markers, but 4 parameters were supplied', 'HY000')

I have entered 4 parameters (name, date[in form of datetime.date have also tried getdate() for sql], idx, and describe) what is the 5th?

Its for an Azure SQL database

Thank you in advance!

CodePudding user response:

You have a =? in your query and pyodbc tries to replace it also, besides the four values, removing it will do the trick

with pyodbc.connect('Driver='   driver   ';SERVER=tcp:'   server   ';PORT=1433;DATABASE='   database   ';UID='   username   ';PWD='   password) as conn:
  with conn.cursor() as cursor:
    sql = ("Insert Into dbo.Report (Name, Date, id_number, decription) Values(?,?,?,?)")
    cursor.execute(sql, name, date, idx, describe)

CodePudding user response:

try this

sql = ("Insert Into dbo.Report (Name, Date, id_number, decription)=? Values(?,?,?,?,?)")

add a one more "?" in values.

or this

cursor.execute(name, date, idx, describe)

clear the "sql"

  • Related