当前位置:网站首页>记录 splite3 库的一个坑(表名和字段定义不能用占位符?)
记录 splite3 库的一个坑(表名和字段定义不能用占位符?)
2022-04-21 21:59:00 【Xavier Jiezou】
项目场景
Python 内置 sqlite3 模块的官方文档推荐我们向表中插入记录时使用 ? 充当占位符,然后将数据通过参数传递。而不推荐使用 Python 的字符串拼接操作,说是有 SQL 注入风险。于是我这里创建表的时候也想用占位符这种方式:
import sqlite3
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
sql = 'create table ? (?)'
table_name = 'student'
arg = 'id integer'
cursor.execute(sql, (table_name, arg))
conn.commit()
cursor.close()
conn.close()
问题描述
但执行上述代码报错:
sqlite3.OperationalError: near "?": syntax error
原因分析
表名称和字段定义不能使用 ? 充当占位符。
解决方案
既然表名称和字段定义不支持使用 ? 充当占位符,那就不用了吧,改回 Python 的字符串拼接操作。
import sqlite3
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
table_name = 'student'
arg = 'id integer'
sql = f'create table {
table_name} ({
arg})'
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
引用参考
Inserting a table name into a query gives sqlite3.OperationalError: near “?”: syntax error
版权声明
本文为[Xavier Jiezou]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42951560/article/details/124322702
边栏推荐
- 2022 safety officer-a certificate examination exercises and online simulation examination
- 分析师认为三星Galaxy Z Fold 4和Z Flip 4可能比其前代产品更便宜
- 2022 high altitude installation, maintenance and demolition of examination question bank and simulated examination
- What should you do to benefit from digital transformation?
- 【ES6】变量的解构赋值
- mysql 模糊搜索与校对规则
- 自动生成排除某个字段的sql
- GAMES101 Lec6 反走样与深度缓冲
- redis配置文件详解
- The development of China's industrial Internet industry has achieved remarkable results, but the technical challenge is still a long-term project
猜你喜欢

【测试用例级别定义】

Oracle合并数据操作(MERGE)

Detailed explanation of redis configuration file

硬核实力,多方认可|云扩科技作为RPA核心厂商入选《2022中国RPA采购指南》

【函数实现c语言基础问题】

Use try-with-resources or close this “FileOutputStream“

"Three wars" Hong Kong stock exchange, and "Guo Qi Wang Hong" green tea restaurant can't tell a new story?

解放双手,推荐一款阿里开源的低代码工具,YYDS~

How does wechat applet realize the function of jumping from commodity list to commodity details page

Live555 learning
随机推荐
Oracle级联删除表(不受外键约束)
【ES6】Iterator和forof循环
Finding a way to combine new technologies with itself is the key to the development of industrial Internet
【MySQL】解决Mac访问Windows上MySQL服务器问题
2022 safety officer-b certificate operation certificate examination question bank and simulation examination
2022 crane driver (limited to bridge crane) work license question bank and online simulation examination
Games101 lec6 anti aliasing and depth buffer
MySQL fuzzy search and proofreading rules
Oracle合并数据操作(MERGE)
A thorough understanding -- > shell script
The development of China's industrial Internet industry has achieved remarkable results, but the technical challenge is still a long-term project
Architecture document of outsourcing student management system
【ES6】let和const命令
【史上最全 BAT 必问高并发总结】
kotlin环境搭建,2021百度Android岗面试真题收录解析
Push to origin/master was rejected:报错解决办法
外包学生管理系统的架构文档
【Canvas】基础绘制与使用
During iPhone test, the picture of custom tabbar will slide along the screen
找到新技术与自身的结合方式,才是开启产业互联网发展的关键所在