python - SQLITE:Python 插入乱码而不是整数
问题描述
我似乎找不到以下问题的原因:
cur_out.execute("DROP TABLE IF EXISTS key2val");
cur_out.execute("DROP INDEX IF EXISTS key2val_value_index");
cur_out.execute("CREATE TABLE key2val(key INTEGER PRIMARY KEY, val INT)");
cur_out.executemany("INSERT INTO key2val VALUES(?,?)",((key,value[next(iter(dictionary[key]))],) for key in dictionary));
cur_out.execute("CREATE INDEX key2val_value_index on key2val(val)");
con_out.commit();
其中value
是一个 numpy 整数数组,dictionary
是一个 dict,指向每个键的一组非空整数。
>>> for tup in ((key,value[next(iter(dictionary[key]))],) for key in dictionary):
>>> print(tup)
(615063, 10548)
(615980, 0)
(629315, 10627)
(631978, 0)
(642585, 0)
(643574, 0)
(667025, 0)
(152076, 5406)
(393084, 0)
...
然后在数据库中
sqlite> select distinct val from key2val limit 10;
返回一些无法在终端中正确显示或复制到 stackoverflow 中的奇怪符号或其组合。
解决方案
这不是胡言乱语,至少不是胡言乱语。问题是这value
是一个 numpy 数组,因此它的元素不是 Python 整数而是 numpyint32
或int64
取决于您的版本(32 位或 64 位)。
这不是sqlite3
模块的已知类型,因此它只存储字节表示。例如,在我的带有 32 位 Python 的 little endian 系统上,它存储为 7 个b'\x07\x00\x00\x00'
. 如果您显示这些值,这是明确repr
的,但如果您只是打印它们,则很难猜测会发生什么。
怎么修:
恕我直言,最简单的方法是显式强制转换为 Python 整数类型:
cur_out.executemany("INSERT INTO key2val VALUES(?,?)",
((key,int(value[next(iter(dictionary[key]))]),) for key in dictionary))
或者,您可以注册一个适配器:
def npint_adapt(x):
return int(x)
sqlite3.register_adapter(np.int32, npint_adapt) # or use np.int64 here if using 64 bits...
然后直接插入np.int32
(resp. np.int64
)类型:
cur_out.executemany("INSERT INTO key2val VALUES(?,?)",
((key,value[next(iter(dictionary[key]))],) for key in dictionary));
推荐阅读
- javascript - 如何编写一个数组方法,该方法返回一个双精度数组,其元素是参数数组元素的滑动平均值
- google-cloud-storage - 为什么 gsutil cp 在版本化存储桶上需要 storage.objects.delete?
- sql - Oracle SQL:根据固定时间范围(比如 15 或 30 分钟)计算记录
- ios - 随用随付 iOS 订阅介绍性优惠的定价是多少?
- xpath - Geb:用于多元素导航器的 Web 元素定位器
- excel - 条件是不同列的前 x% 的列的平均值
- excel - 运行代码时如何排除第一张纸?
- google-api - 从浏览器调用反向地理编码 API 时如何限制 API 密钥?
- google-app-engine - 我可以在谷歌应用引擎中创建新服务吗?
- android - Android coreLibraryDesugaringEnabled,使用 NoSuchMethodError 连接测试崩溃