insert NULL into mysql
https://stackoverflow.com/questions/36898130/python-how-to-insert-null-mysql-values
You are inserting the string 'NULL', not the NULL value. If these values are coming from a Python structure, you need to use something else to map to the NULL value in SQL.
You could use None for this, and only quote other values:
def sqlquote(value):
"""Naive SQL quoting
All values except NULL are returned as SQL strings in single quotes,
with any embedded quotes doubled.
"""
if value is None:
return 'NULL'
return "'{}'".format(str(value).replace("'", "''"))
sql = "INSERT INTO test VALUES ({column1}, {column2}, {column3})".format(
**{k: sqlquote(v) for k, v in d.items()})
Note that because you have to handle None differently, you also have to handle proper SQL quoting! If any of your values directly or indirectly come from user-supplied data, you'd be open for SQL injection attacks otherwise.
The above sqlquote() function should suffice for SQLite strings, which follow standard SQL quoting rules. Different databases have different rules for this, so tripple check your documentation.
Personally, I'd use the SQLAlchemy library to generate SQL and have it handle quoting for you. You can configure it to produce SQL for different database engines.
插入空的datetime类型:
sql = "INSERT INTO test_data_table (`data`, char_test, datetime_test) VALUES (%s, %s, %s)" % ('NULL', 'NULL', "'2017-09-01'")
sql = "INSERT INTO test_data_table (`data`, char_test, datetime_test) VALUES (%s, %s, %s)" % ('NULL', 'NULL', 'NULL')
注意两者之间的不同。
简而言之,python遇到None,需要在数据库插入'NULL'需要有一个转化过程,将None转为NULL,并视情况加单双引号,不加''在%s.
数字插入空值
INSERT INTO company (company_id, name, employee_nums) values (%s, %s, %s) on duplicate key update company_id = values(company_id), name=values(name), employee_nums=values(employee_nums);
args = (1, "test_name", None) # None will be convert to NULL
conn._cur.execute(sql, arg)
# 再次更新,%s全部不加引号,在传值之前处理完毕
INSERT INTO company (company_id, name, employee_nums, registered_date)
values (%s, %s, %s, %s)
on duplicate key update company_id = values(company_id), name=values(name), employee_nums=values(employee_nums),
registered_date=values(registered_date); % (company_id, name, employee_name, "'{}'".format(registered_date) if registred_date else "NULL")
insert NULL into mysql的更多相关文章
- ORA-01400: cannot insert NULL into
Error text: ORA-01400: cannot insert NULL into when insert into view, NULL value handler in trigger. ...
- [转]一次Delete&Insert引发的Mysql死锁
近日遇到一个比较奇怪的deadlock错误, 错误详情: Deadlock found when trying to get lock; try restarting transaction; nes ...
- Using INSERT IGNORE with MySQL to prevent duplicate key errors
An error will occur when inserting a new record in MySQL if the primary key specified in the insert ...
- mybatis学习 -每天一记 mybatis insert null 报错
mybatis 插入数据,model的属性存在null,插入报错 在使用mybatis 进行insert时,如果字段值存在null的情况,会出现插入失败的情况,解决方案: 如果使用spring boo ...
- null在mysql中的不同表现
在mysql中count(*)和count(id)具有不同的表现,其最大的影响是在我们进行联表的时候,如,我们现在要查询一个文章的评论数量,使用左连接查询,具体的sql语句如下: SELECT a.* ...
- 【原创】sql:慎用【数字字段1 - 数字字段2】这样的sql(10-null = null)mysql
如果只有一个表的情况下 a表: id num1,num2 1 10 5 2 10 0 3 20 0 select id, num1,num2,num1 - num2 AS subNum from a; ...
- django insert data into mysql
#!/usr/bin/python # -*- coding:utf-8 -*- # @filename: search # @author:wwx399777 wuweiji # @date: 20 ...
- 2020-04-06:insert语句在mysql里经历了什么?
1.会话状态转换为update 2.激活事物状态由 not_active 变为 active 3.查找定位数据 4.进行乐观插入 记录insert的undo记录记录undo的redo log 入red ...
- 如何查找一个为NULL的MYSQL字段
前言:在做这个题目 https://www.cnblogs.com/pipihao/p/13786304.html 因为之前 我好像没有接触过什么 为NULL字段的查询,细节不小 WHERE 字段 I ...
随机推荐
- 解决mysql 8 安装后命令行可以连接,navicat不能连接的问题
错误代码: client does not support authentication 解决办法: 1 使用命令行进入数据库 2 选着数据库 mysql --> user mysql 3 ...
- mysql 按照 where in 排序
select * from user_extend where `unique` in('mark.liu@xxxx.com','jason.gan@xxxx.com','ssgao@xxxx.com ...
- EF webapi json序列化 表间相互引用 无限循环问题解决方案
WebApiConfig.cs中加入 如下代码即可解决无限循环问题 var json = config.Formatters.JsonFormatter; // 解决json序列化时的循环引用问题 j ...
- spring SOA architecture
在谈这个之前,还得再说下SOA和平台.SOA做两件事情,一个是解耦并识别可重用的服务,一个是对服务进行灵活组装和编排满足业务需求,SOA核心是业务和技术的解耦,服务和能力的复用.而在IT领域的平台平台 ...
- RDD之七:Spark容错机制
引入 一般来说,分布式数据集的容错性有两种方式:数据检查点和记录数据的更新. 面向大规模数据分析,数据检查点操作成本很高,需要通过数据中心的网络连接在机器之间复制庞大的数据集,而网络带宽往往比内存带宽 ...
- 图片尺寸批量resize的matlab并行代码
在caffe ImageNet例子中有对图片进行resize的部分,文中使用的是linux shell脚本命令: for name in /path/to/imagenet/val/*.JPEG; d ...
- CGLib缺少jar出现 java.lang.ClassNotFoundException: org.objectweb.asm.Type
CGLib实现动态代理区别于JDK动态代理,不需要目标类实现任何接口,是通过生成代理类子类的方式,而且据说速度要快于JDK动态代理.所以我想要试验一下CGlib的动态代理,网上找了些例子,自己动手写了 ...
- 【洛谷】P1641 [SCOI2010]生成字符串(思维+组合+逆元)
题目 传送门:QWQ 分析 不想画图. https://www.luogu.org/problemnew/solution/P1641 好神仙的题啊. 代码 // luogu-judger-enabl ...
- [jni]Getting Started
写一个java应用程序来调用C函数打印“Hello World!" 这个过程包括以下步骤: 1:创建一个申明了native方法的java类(HelloWorld.java): 2:使用jav ...
- Linux内存管理大图(第三稿)
http://bbs.chinaunix.net/thread-2018659-2-1.html 描述讨论在http://bbs.chinaunix.net/thread-3760371-1-1.ht ...