python - 面临 Python 到 MYSQL 插入的问题
问题描述
我尝试使用几种方法将数据插入 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
我已经解决了很多问题,但没有运气。任何人都可以帮忙,因为我在哪里错了,或者还有什么可以将数据从 python 插入 mysql 的好选择。
解决方案
我不知道你的错误在哪里,但我用这段代码测试过,它可以工作。
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
推荐阅读
- ios - 仅在 iPad 上长按时,在列表中加载更多内容的 NavigationView 崩溃
- c++ - IntRect SFML 声明
- django - 无法更改超级用户自己的 django admin inline
- jquery - Jquery 脚本中带有 fontawesome 图标类的 Span 元素自动被 SVG 替换?奇怪的行为
- perl - 在 perl 中从系统命令中捕获退出代码
- python - 将 Pandas 系列转换为没有索引的字典
- python - 如何使点文件夹/文件统一可见?
- html - 圆角和容器的问题
- firebase - 嵌套的 firebase-firestore 规则:父文档中的所有者 ID
- hibernate - 如何在休眠中将实体正确映射到多个实体类型?