can we take user input using python for mysql table values?

721 Views Asked by At

I tried taking input,but its showing error.

raise errors.ProgrammingError(

ProgrammingError: Not all parameters were used in the SQL statement

A part of code that i tried,

`#D_O_B

import datetime
 year = int(input('Enter birth year'))
 month = int(input('Enter birth month'))
 day = int(input('Enter birth day'))
 D_O_B = datetime.date(year, month, day)
 
 #Age
 A=int(input("Enter age"))
sql = """INSERT INTO PLAYER_DETAILS(
   Name,DATE_OF_BIRTH ,Age ,Federation ,Elo_RATING ,Title)
   VALUES ( %s,%d ,(%d %b %y), %s,%d , %s)"""

 val = (N,D_O_B,A,Fed,RATING,T)
 cursor.execute(sql, val)

 db.commit()
1

There are 1 best solutions below

1
kartik patel On
pip install pyodbc

pyodbc is open source odbc driver for python will allow you to connect sql, Connect with

import pyodbc    
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')

then Create Cursor

cursor = cnxn.cursor()

and then create query like you do in sql query and wherever you want to use user input , replace it with ? add second argument of execute as array and in array put respective values

cursor.execute("""
    select user_id, user_name
      from users
     where last_logon < ?
       and bill_overdue = ?
""", [datetime.date(2001, 1, 1), 'y'])

you can pass multiple argument if you dont want to use array as second argument but it also should be respective

cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')

and do commit after insert update or delete

cnxn.commit()

you found more here in official docs Getting Started