python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫的實(shí)現(xiàn)方法
sqllite里面并沒有與numpy的array類型對應(yīng)的數(shù)據(jù)類型,通常我們都需要將數(shù)組轉(zhuǎn)換為text之后再插入到數(shù)據(jù)庫中,或者以blob類型來存儲數(shù)組數(shù)據(jù),除此之外我們還有另一種方法,能夠讓我們直接以array來插入和查詢數(shù)據(jù),實(shí)現(xiàn)代碼如下
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)
# 當(dāng)插入數(shù)據(jù)的時(shí)候?qū)rray轉(zhuǎn)換為text插入
sqlite3.register_adapter(np.ndarray, adapt_array)
# 當(dāng)查詢數(shù)據(jù)的時(shí)候?qū)ext轉(zhuǎn)換為array
sqlite3.register_converter("array", convert_array)
#連接數(shù)據(jù)庫
con = sqlite3.connect("test.db", detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.cursor()
#創(chuàng)建表
cur.execute("create table test (arr array)")
#插入數(shù)據(jù)
x = np.arange(12).reshape(2,6)
cur.execute("insert into test (arr) values (?)", (x, ))
#查詢數(shù)據(jù)
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))
# <type 'numpy.ndarray'>
實(shí)例代碼看下Python 操作sqlite數(shù)據(jù)庫及保存查詢numpy類型數(shù)據(jù)
# -*- 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)
# 創(chuàng)建數(shù)據(jù)庫連接對象
conn = sqlite3.connect('sample_database.db', detect_types=sqlite3.PARSE_DECLTYPES) # 連接到SQLite數(shù)據(jù)庫
'''
sqlite3.PARSE_DECLTYPES
本常量使用在函數(shù)connect()里,設(shè)置在關(guān)鍵字參數(shù)detect_types上面。表示在返回一行值時(shí),是否分析這列值的數(shù)據(jù)類型定義。如果設(shè)置了本參數(shù),就進(jìn)行分析數(shù)據(jù)表列的類型,并返回此類型的對象,并不是返回字符串的形式。
sqlite3.PARSE_COLNAMES
本常量使用在函數(shù)connect()里,設(shè)置在關(guān)鍵字參數(shù)detect_types上面。表示在返回一行值時(shí),是否分析這列值的名稱。如果設(shè)置了本參數(shù),就進(jìn)行分析數(shù)據(jù)表列的名稱,并返回此類型的名稱
'''
# 參數(shù):memory:來創(chuàng)建一個(gè)內(nèi)存數(shù)據(jù)庫
# 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()
# 創(chuàng)建數(shù)據(jù)庫表
cursor.execute("create table test (arr array)")
# 插入一行數(shù)據(jù)
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))
'''
<class 'numpy.ndarray'>
'''
cursor.close() # 關(guān)閉Cursor
conn.close() # 關(guān)閉數(shù)據(jù)庫
以上就是python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫的實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于python numpy數(shù)組的資料請關(guān)注腳本之家其它相關(guān)文章!
- python中numpy數(shù)組與list相互轉(zhuǎn)換實(shí)例方法
- python numpy.power()數(shù)組元素求n次方案例
- python 將numpy維度不同的數(shù)組相加相乘操作
- Python多進(jìn)程共享numpy 數(shù)組的方法
- Python中numpy數(shù)組的計(jì)算與轉(zhuǎn)置詳解
- python中的Numpy二維數(shù)組遍歷與二維數(shù)組切片后遍歷效率比較
- 詳解python如何通過numpy數(shù)組處理圖像
- Python中的?Numpy?數(shù)組形狀改變及索引切片
- Python數(shù)據(jù)分析numpy數(shù)組的3種創(chuàng)建方式
相關(guān)文章
Python利用pandas計(jì)算多個(gè)CSV文件數(shù)據(jù)值的實(shí)例
下面小編就為大家分享一篇Python利用pandas計(jì)算多個(gè)CSV文件數(shù)據(jù)值的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python上selenium的彈框操作實(shí)現(xiàn)
這篇文章主要介紹了python上selenium的彈框操作實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
13個(gè)最常用的Python深度學(xué)習(xí)庫介紹
這篇文章主要介紹了13個(gè)最常用的Python深度學(xué)習(xí)庫介紹,具有一定參考價(jià)值,需要的朋友可以參考下。2017-10-10
django框架基于queryset和雙下劃線的跨表查詢操作詳解
這篇文章主要介紹了django框架基于queryset和雙下劃線的跨表查詢操作,結(jié)合實(shí)例形式詳細(xì)分析了Django框架queryset和雙下劃線的跨表查詢相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-12-12
python報(bào)錯TypeError: ‘NoneType‘ object is not subscriptable的解決
這篇文章主要給大家介紹了關(guān)于python報(bào)錯TypeError: ‘NoneType‘ object is not subscriptable的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
python的pytest框架之命令行參數(shù)詳解(上)
這篇文章主要介紹了python的pytest框架之命令行參數(shù)詳解,pytest是一款強(qiáng)大的python自動化測試工具,可以勝任各種類型或者級別的軟件測試工作。pytest提供了豐富的功能,包括assert重寫,第三方插件,需要的朋友可以參考下2019-06-06

