0

I've tried to use couple of methods to insert data into mysql database but getting error in all: In the first method:

sql = ('''Insert into lgemployees (EmpID,Name,Gender,DOB,Address,PhoneNumber,Email)        
 VALUES (%d,$s,$s,$s,$s,$d,$s)''', (eid, name, gen, dob, add, mob, email))

mycursor.execute(sql)
mycursor.commit()

Error in this approach:

'tuple' object has no attribute 'encode'

2nd method:

sql = "Insert into lgemployees (EmpID,Name,Gender,DOB,Address,PhoneNumber,Email)  VALUES(?,?,?,?,?,?,?,)"
val =  (eid, name, gen, dob, add, mob, email)
mycursor.execute(sql, val)
mycursor.commit()

Error in this approach :

    "Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

I've troubleshooted a lot from my end but no luck. Can any one please help as where am I wrong or what else can be a good option to insert data into mysql from python.

3
  • Also you can look here: pynative.com/python-mysql-insert-data-into-database-table Commented Jul 19, 2019 at 11:11
  • In the first approach you are writing $s instead of %s Commented Jul 19, 2019 at 11:15
  • @vidriduch The extra comma in the end was given in one of the articles only :). Although I've tried removing that extra comma as well. but no luck same error. Commented Jul 19, 2019 at 11:16

1 Answer 1

1

I dont know where you error is at, but ive tested with this code and it works.

        insert_tuple = (eid, name, gen, dob, add, mob, email)
        sql = """INSERT INTO lgemployees (`EmpID `, 
        `Name`,`Gender`, `DOB`, `Address`, `PhoneNumber`, `Email`)
        VALUES (%s,%s,%s,%s,%s,%s,%s)"""
        mycursor = mySQLconnection.cursor()
        mycursor.execute(sql, insert_tuple)
        mySQLconnection.commit()
        mycursor.close()

your code throws this because one of the parameters are empty or are in a format it cant read.

  "Not all parameters were used in the SQL statement")
    mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks its working now after using all %s in all fields.. But why does it gives that error when I use %d where columns are expressed as INT in mysql. Ideally %d and %s should be used depending on INT of varchar.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.