博客
关于我
Python 操作sqlite数据库及保存查询numpy类型数据(一)
阅读量:796 次
发布时间:2023-03-07

本文共 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/

你可能感兴趣的文章
python | Python俄罗斯方块游戏详解
查看>>
python | Python动态代码执行:exec和compile函数
查看>>
Python读取文件数据进行数据图形化展示
查看>>
python | Python反向迭代:reversed实现机制
查看>>
python | Python开发必知的数据容器用法(建议收藏!)
查看>>
python读取文件夹下文件名称_如何在Python目录中获取文件名列表
查看>>
python | Python文本处理中的相似性识别应用
查看>>
python | Python模块缓存:sys.modules机制
查看>>
python | Python集成学习和随机森林算法
查看>>
python | Python高阶函数与函数式编程
查看>>
python | pytime,一个实用的 时间和日期处理 Python 库!
查看>>
python | pyupgrade,一个有趣的 Python 库!
查看>>
python | pyvips,一个神奇的 图像处理 Python 库
查看>>
Python | qutip,一个高级的 Python 库!
查看>>
python | rapidjson,一个实用的 提高JSON处理效率 Python 库!
查看>>
python | reflex,一个无敌的 Python 库!
查看>>
python | reloading,一个强大的 Python 库!
查看>>
python | rich,一个无敌的 Python 库!
查看>>
python | rlax,一个超强的 强化学习领域 Python 库!
查看>>
python | rpyc,一个超实用的 Python 库!
查看>>