在Python到MYSQL插入中面临的问题

2024-10-03 11:26:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试过使用几种方法将数据插入mysql数据库,但都出现了错误: 在第一种方法中:

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()

此方法中的错误:

'tuple' object has no attribute 'encode'

第二种方法:

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()

此方法中的错误

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

从我这一头开始,我已经解决了很多麻烦,但是运气不好。任何人都可以帮助我,因为我哪里错了或什么可以是一个很好的选择,插入数据到mysql从python


Tags: 数据方法namesqladdress错误mysqlgender
1条回答
网友
1楼 · 发布于 2024-10-03 11:26:01

我不知道你的错误是在哪里,但我测试了这个代码,它的工作

        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()

您的代码抛出此错误是因为其中一个参数为空或其格式无法读取

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

相关问题 更多 >