当前位置:网站首页>pymysql格式化输入的一些问题
pymysql格式化输入的一些问题
2022-08-07 05:05:00 【Eongking】
最近在做一个tkinter+pymysql的小项目,算第一次做着玩,然后两种语言混到一块儿各种格式化输入的标点符号算是把我整的不行,冷不丁就给你报一个语法错误。国内找了一圈都挺模糊的,stack overflow上也有各种方法,其中有些也不能解决我当时遇到的问题。反正试了好多方法终于自己解决了,算是总结一下(不一定包含所有场景),给大家避个雷。
首先明确'(单引号或双引号,方便表示用A代替)和`(ESC下面这个符号,方便表示用B代替)的区别:
mysql中" ' "和 " ` "的区别_一夜长风的专栏-CSDN博客_mysql
https://blog.csdn.net/wlzx120/article/details/52524130
- A用于引用字符串等
- B用于引用关键字,例如数据库、表、索引、列和别名等
grant_sql_1 = f"grant select,insert,delete on userinfo.`{u_name}` to '{u_name}'@'%' identified by '{u_pwd}'"例如这句话里面,第一个格式化是为了分配权限给userinfo database下的一个与用户名同名的表,算关键字,因此这里只能用B;而后面的格式化是为了分配权限指定的用户,是之前设置的字符串,因此用A
第二个例子
table_sql_2 = "CREATE TABLE `{}` (id int not null auto_increment primary key," \
"album_id varchar(32) not null default 'name'," \
"insert_time timestamp," \
"add_flag int not null default 0," \
"buy_flag int not null default 0" \
")engine=innodb default charset=utf8".format(u_name)同理,因为是表名,只能用B,并且我这边如果不用B的话是会报错的
第三个例子
select_sql = f"select user from user where user='{u_name}'"有新的问题的话,到时候再补上吧。
边栏推荐
猜你喜欢

Fedora Team Announces Fedora 36 System Release

Automated operation and maintenance tools - ansible overview and deployment

线性代数学习笔记2-3补充:抽象向量空间

Detailed explanation of C51 basic functions, interrupt functions and library functions

volatile原理

小 P 周刊 Vol.14

typescript79-配置文件tsconfig.json文件

The setting and clearing of the inconsistency between the data displayed in the Excel cell and the actual data

Seq2Seq superficial understanding

推荐抓包工具
随机推荐
茶叶交易行情小程序开发制作功能介绍
Redis关闭持久化
洛谷P1188 PASTE
什么是负载均衡
RT_thread的IIC设备学习笔记
Tips for recording WPF (2) 16-30
洛谷P1202 黑色星期五Friday the Thirteenth
线性代数学习笔记5-2:(正交的)投影、投影矩阵、A^T A、最小二乘法LS
"Postgraduate Research Ability Training and Cultivation"
不是吧,10:00面试,10:08就出来了 ,问的实在是太...
Detailed explanation of C51 basic functions, interrupt functions and library functions
One Pass 1225 - Treasure Island
Seq2Seq superficial understanding
Redis 发布订阅操作
Redis内存管理之过期、淘汰策略
阿三的CV很有意思
刷题《剑指Offer》day11
Talk about 7 magic skills of Redis memory optimization
【Yu Niangniang】1373. Maximum key value and DFS of binary search subtree
Fedora 团队宣布 Fedora 36 系统发布了