python-3.x - 如何使用 python-sqlite3 在表中插入空值?

标签 python-3.x sqlite

在sqlite控制台中,我们可以简单的插入null。

create table data(code TEXT,date  DATETIME)
insert into data values("x1","20010101");
insert into data values("x2",null);

如何使用 python-sqlite3 插入 null?

import sqlite3 
db = r'F:\test.sqlite'
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute('create table data(code TEXT,date  DATETIME)')
record1=('x1','2001-01-01')
cur.execute('INSERT INTO data VALUES(?,?);',record1)
record2=('x2','NULL')
cur.execute('INSERT INTO data VALUES(?,?);',record2)
record3=('x2','null')
cur.execute('INSERT INTO data VALUES(?,?);',record3)
record4=('x4',null)
cur.execute('INSERT INTO data VALUES(?,?);',record4)
record5=('x5',NULL)
cur.execute('INSERT INTO data VALUES(?,?);',record5)
con.commit()

cur.execute('INSERT INTO data VALUES(?,?);',record2)插入一串 NULL 到表中,不为空。
cur.execute('INSERT INTO data VALUES(?,?);',record3)插入一个null的字符串到表中,而不是null。

cur.execute('INSERT INTO data VALUES(?,?);',record4) 或 cur.execute('INSERT INTO data VALUES(?,?);',record5) 都不能将 null 插入表中数据。

如何使用 python-sqlite3 将 null 插入表中?

最佳答案

documentation说:

The following Python types can thus be sent to SQLite without any problem:

Python type   SQLite type
None          NULL
...

关于python-3.x - 如何使用 python-sqlite3 在表中插入空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26038935/

相关文章:

python - 如何在 python 中分析 python 代码以识别变量名称、函数调用等

multithreading - SQLite-插入操作会阻止读取吗?

ruby-on-rails - 如何创建一个基于实例方法的值对对象进行排序的类/作用域方法?

ios - sqlite3_prepare_v2 返回 1

vb.net - 如何即时进行自定义查询?

java - 我如何在 Android 中使用 SQLite?

python-3.x - 使用 MongoEngine 和 Flask 创建 WTForm 时出现类型错误

c++ - 包括 Python.h 错误 : initconfig. h 未找到

python - 在 joblib `matlab` 上下文中腌制 `Parallel` 对象时出错

python - 如何从本地目录导入模块而不是 pip 安装的库?