python中sqllite插入numpy数组到数据库的实现方法

您所在的位置:网站首页 python里seek python中sqllite插入numpy数组到数据库的实现方法

python中sqllite插入numpy数组到数据库的实现方法

2023-04-02 16:25| 来源: 网络整理| 查看: 265

python中sqllite插入numpy数组到数据库的实现方法 2023-03-10 13:50:00 网络 数据库

本文整理自网络,侵删。

sqllite里面并没有与numpy的array类型对应的数据类型,通常我们都需要将数组转换为text之后再插入到数据库中,或者以blob类型来存储数组数据,除此之外我们还有另一种方法,能够让我们直接以array来插入和查询数据,实现代码如下

import sqlite3 import numpy as np import io def adapt_array(arr): out = io.BytesIO() np.save(out, arr) out.seek(0) return sqlite3.Binary(out.read()) def convert_array(text): out = io.BytesIO(text) out.seek(0) return np.load(out) # 当插入数据的时候将array转换为text插入 sqlite3.register_adapter(np.ndarray, adapt_array) # 当查询数据的时候将text转换为array sqlite3.register_converter("array", convert_array) #连接数据库 con = sqlite3.connect("test.db", detect_types=sqlite3.PARSE_DECLTYPES) cur = con.cursor() #创建表 cur.execute("create table test (arr array)") #插入数据 x = np.arange(12).reshape(2,6) cur.execute("insert into test (arr) values (?)", (x, )) #查询数据 cur.execute("select arr from test") data = cur.fetchone()[0] print(data) # [[ 0 1 2 3 4 5] # [ 6 7 8 9 10 11]] print(type(data)) #

实例代码看下Python 操作sqlite数据库及保存查询numpy类型数据

# -*- coding: utf-8 -*- ''' Created on 2019年3月6日 @author: Administrator ''' import sqlite3 import numpy as np import io def adapt_array(arr): out = io.BytesIO() np.save(out, arr) out.seek(0) return sqlite3.Binary(out.read()) def convert_array(text): out = io.BytesIO(text) out.seek(0) return np.load(out) # 创建数据库连接对象 conn = sqlite3.connect('sample_database.db', detect_types=sqlite3.PARSE_DECLTYPES) # 连接到SQLite数据库 ''' sqlite3.PARSE_DECLTYPES 本常量使用在函数connect()里,设置在关键字参数detect_types上面。表示在返回一行值时,是否分析这列值的数据类型定义。如果设置了本参数,就进行分析数据表列的类型,并返回此类型的对象,并不是返回字符串的形式。 sqlite3.PARSE_COLNAMES 本常量使用在函数connect()里,设置在关键字参数detect_types上面。表示在返回一行值时,是否分析这列值的名称。如果设置了本参数,就进行分析数据表列的名称,并返回此类型的名称 ''' # 参数:memory:来创建一个内存数据库 # conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) # Converts np.array to TEXT when inserting sqlite3.register_adapter(np.ndarray, adapt_array) # Converts TEXT to np.array when selecting 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(data) ''' [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]] ''' print(type(data)) ''' ''' cursor.close() # 关闭Cursor conn.close() # 关闭数据库

以上就是python中sqllite插入numpy数组到数据库的实现方法的详细内容,更多关于python numpy数组的资料请关注其它相关文章!

标签:SQLite

相关阅读 >>

c# Sqlite数据库入门使用说明

navicat premium操作mysql数据库(执行sql语句)

解决mac系统升级后虚拟机黑屏问题

android数据存储之Sqlite使用

c#查询sqlserver数据库并返回单个值的方法

ios中Sqlite使用教程

Sqlite教程(三):数据表和视图简介

android调试工具adb命令大全

打造自己的.net core项目模板

c#操作Sqlite实现数据的增删改查

更多相关阅读请进入《Sqlite》频道 >>

数据库系统概念 第6版 书籍 数据库系统概念 第6版

¥41.1元   机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。

转载请注明出处:木庄网络博客 » python中sqllite插入numpy数组到数据库的实现方法

标签:Sqlite

打赏

取消 木庄网络博客

感谢您的支持,我会继续努力的!

扫码支持 扫码打赏,您说多少就多少 支付宝 微信

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

相关推荐 关于mysql 时间戳格式化函数from_unixtime的使用说明 整理MySql常用查询语句(23种) 关于redis之lpush、rpush、lset、lrem 探索Redis持久化原理 Python操作SQLite数据库的方法详解 mysql添加用户失败怎么办 sqlserver 手工实现差异备份的步骤 深入了解MongoDB 分布式集群 评论

管理员已关闭评论功能...



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3