鱼C论坛

 找回密码
 立即注册
查看: 2512|回复: 3

[已解决]为啥报错呢?

[复制链接]
发表于 2023-7-12 18:04:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. conn = pymssql.connect(host=host, user=user, password=pwd, database=db)
  2. cursor = conn.cursor()

  3. # 插入数据的 SQL 语句
  4. insert_sql = "INSERT INTO BX (Times, TestTime) VALUES (?, ?)"

  5. row =(55, '2023-07-12 14:52:02')

  6. try:
  7.     # 执行 SQL 语句并提交更改
  8.     cursor.execute(insert_sql, row)
  9.     conn.commit()
  10. except pymssql.Error as e:
  11.     print("Error occurred: ", e)
  12. finally:
  13.     # 关闭游标和连接
  14.     cursor.close()
  15.     conn.close()
复制代码
最佳答案
2023-7-12 18:06:05
rsj0315 发表于 2023-7-12 18:04
报错如下
Error occurred:  (102, b"Incorrect syntax near '?'.DB-Lib error message 20018, severity 15 ...

你的问题出在 SQL 查询中的占位符上。在你的插入语句中,你使用了问号 ( ? ) 作为参数的占位符,但在 pymssql 中,这是不正确的。在 pymssql 中,你需要使用  %s  作为参数的占位符,无论参数的类型是什么。

所以你的插入语句应该是:

  1. insert_sql = "INSERT INTO BX (Times, TestTime) VALUES (%s, %s)"
复制代码


使用  %s  作为占位符,pymssql 会自动处理参数的类型转换和转义,从而避免 SQL 注入等问题。这就是为什么我们使用参数化查询,而不是字符串拼接来创建 SQL 查询的原因。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-12 18:04:28 | 显示全部楼层
根据你提供的代码,报错可能是因为缺少 pymssql 模块或模块未正确导入。请确保你已经正确安装了 pymssql 模块,并在代码开头添加 import pymssql

另外,请确保你已经正确填写了 hostuserpassworddatabase 的值,以连接到正确的数据库。

以下是修正后的代码示例:
  1. import pymssql
  2. host = 'your_host'
  3. user = 'your_user'
  4. password = 'your_password'
  5. database = 'your_database'
  6. conn = pymssql.connect(host=host, user=user, password=password, database=database)
  7. cursor = conn.cursor()
  8. # 插入数据的 SQL 语句
  9. insert_sql = "INSERT INTO BX (Times, TestTime) VALUES (?, ?)"
  10. row = (55, '2023-07-12 14:52:02')
  11. try:
  12.     # 执行 SQL 语句并提交更改
  13.     cursor.execute(insert_sql, row)
  14.     conn.commit()
  15. except pymssql.Error as e:
  16.     print("Error occurred: ", e)
  17. finally:
  18.     # 关闭游标和连接
  19.     cursor.close()
  20.     conn.close()
复制代码

请将 your_hostyour_useryour_passwordyour_database 替换为你实际的数据库连接信息。

如果你仍然遇到问题,请提供完整的错误信息,以便我可以更好地帮助你排查问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-12 18:04:48 | 显示全部楼层
报错如下
Error occurred:  (102, b"Incorrect syntax near '?'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-12 18:06:05 | 显示全部楼层    本楼为最佳答案   
rsj0315 发表于 2023-7-12 18:04
报错如下
Error occurred:  (102, b"Incorrect syntax near '?'.DB-Lib error message 20018, severity 15 ...

你的问题出在 SQL 查询中的占位符上。在你的插入语句中,你使用了问号 ( ? ) 作为参数的占位符,但在 pymssql 中,这是不正确的。在 pymssql 中,你需要使用  %s  作为参数的占位符,无论参数的类型是什么。

所以你的插入语句应该是:

  1. insert_sql = "INSERT INTO BX (Times, TestTime) VALUES (%s, %s)"
复制代码


使用  %s  作为占位符,pymssql 会自动处理参数的类型转换和转义,从而避免 SQL 注入等问题。这就是为什么我们使用参数化查询,而不是字符串拼接来创建 SQL 查询的原因。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 16:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表