本文共 1778 字,大约阅读时间需要 5 分钟。
SQLite与numpy数组的集成示例 # -*- coding: utf-8 -*- # 创建内存数据库连接 import sqlite3 import numpy as np # 减少内存占用,建议在生产环境中使用 sqlite3.register_adapter(np.ndarray, adapt_array) sqlite3.register_converter("array", convert_array) # 创建示例数据 x = np.arange(12).reshape(2, 6) # 创建数据库表 conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) cursor = conn.cursor() cursor.execute("create table test (arr array)") # 插入示例数据 cursor.execute("insert into test (arr) values (?)", (x,)) conn.commit() # 查询数据 cursor.execute("select arr from test") data = cursor.fetchone()[0] # 输出结果 print("查询结果:") print(data) print(type(data))
# -*- coding: utf-8 -*- # 创建内存数据库连接 import sqlite3 import numpy as np # 减少内存占用,建议在生产环境中使用 sqlite3.register_adapter(np.ndarray, adapt_array) sqlite3.register_converter("array", convert_array) # 创建示例数据 x = np.arange(12).reshape(2, 6) # 创建数据库表 conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) cursor = conn.cursor() cursor.execute("create table test (arr array)") # 插入示例数据 cursor.execute("insert into test (arr) values (?)", (x,)) conn.commit() # 查询数据 cursor.execute("select arr from test") data = cursor.fetchone()[0] # 输出结果 print("查询结果:") print(data) print(type(data))
转载地址:http://pvofk.baihongyu.com/