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的更多相关文章

  1. ORA-01400: cannot insert NULL into

    Error text: ORA-01400: cannot insert NULL into when insert into view, NULL value handler in trigger. ...

  2. [转]一次Delete&Insert引发的Mysql死锁

    近日遇到一个比较奇怪的deadlock错误, 错误详情: Deadlock found when trying to get lock; try restarting transaction; nes ...

  3. 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 ...

  4. mybatis学习 -每天一记 mybatis insert null 报错

    mybatis 插入数据,model的属性存在null,插入报错 在使用mybatis 进行insert时,如果字段值存在null的情况,会出现插入失败的情况,解决方案: 如果使用spring boo ...

  5. null在mysql中的不同表现

    在mysql中count(*)和count(id)具有不同的表现,其最大的影响是在我们进行联表的时候,如,我们现在要查询一个文章的评论数量,使用左连接查询,具体的sql语句如下: SELECT a.* ...

  6. 【原创】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; ...

  7. django insert data into mysql

    #!/usr/bin/python # -*- coding:utf-8 -*- # @filename: search # @author:wwx399777 wuweiji # @date: 20 ...

  8. 2020-04-06:insert语句在mysql里经历了什么?

    1.会话状态转换为update 2.激活事物状态由 not_active 变为 active 3.查找定位数据 4.进行乐观插入 记录insert的undo记录记录undo的redo log 入red ...

  9. 如何查找一个为NULL的MYSQL字段

    前言:在做这个题目 https://www.cnblogs.com/pipihao/p/13786304.html 因为之前 我好像没有接触过什么 为NULL字段的查询,细节不小 WHERE 字段 I ...

随机推荐

  1. HTML5 localStorage使用教程

    在客户端存储数据,HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...

  2. 【shell】grep命令

    1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局 ...

  3. 织梦开启PHP 标签

    第一步: dedecms出现DedeCMS Error:Tag disabled:php原因解决 --------------------------------------------------- ...

  4. mysql 按照 where in 排序

    select * from user_extend where `unique` in('mark.liu@xxxx.com','jason.gan@xxxx.com','ssgao@xxxx.com ...

  5. android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结

    总结: 1.webview 无法直接更改view的属性,可以通过 handler实现,因为跨线程 2.webview可以通过js api读取 view的属性值 代码: private Handler ...

  6. 【JSP中引入文件】JSP中获取根路径+引用js文件

    在jsp界面中经常需要引入js.css等文件,通常都需要先获取项目根路径,然后再引入文件. 例如: 项目路径如下,需要在index.jsp中引入FusionCharts相关的js.css等: inde ...

  7. JVM内存监视手段和内存溢出解决方案

    引言 本文仅关注一些常见的虚拟机内存监视手段,以及JVM运行时数据区各个部分内存溢出的发生和对应的解决方案,总体来说属于概括性总结,涉及相对不是很深入,目的是让自己和其它初学者有一个框架性.概念性的了 ...

  8. bzoj1293 生日礼物

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...

  9. poj 3069 Saruman's Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  10. 【精】Linux磁盘I/O性能监控之iostat详解

    [精]Linux磁盘I/O性能监控之iostat详解 Linux命令详解----iostat 使用iostat分析IO性能