亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)/技術(shù)文章
文章詳情頁(yè)

python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法

瀏覽:54日期:2022-06-16 10:08:11

sqllite里面并沒(méi)有與numpy的array類(lèi)型對(duì)應(yīng)的數(shù)據(jù)類(lèi)型,通常我們都需要將數(shù)組轉(zhuǎn)換為text之后再插入到數(shù)據(jù)庫(kù)中,或者以blob類(lèi)型來(lái)存儲(chǔ)數(shù)組數(shù)據(jù),除此之外我們還有另一種方法,能夠讓我們直接以array來(lái)插入和查詢(xún)數(shù)據(jù),實(shí)現(xiàn)代碼如下

import sqlite3import numpy as npimport iodef 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)查詢(xún)數(shù)據(jù)的時(shí)候?qū)ext轉(zhuǎn)換為arraysqlite3.register_converter('array', convert_array)#連接數(shù)據(jù)庫(kù)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, ))#查詢(xún)數(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ù)庫(kù)及保存查詢(xún)numpy類(lèi)型數(shù)據(jù)

# -*- coding: utf-8 -*-’’’Created on 2019年3月6日@author: Administrator’’’import sqlite3import numpy as npimport iodef 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ù)庫(kù)連接對(duì)象conn = sqlite3.connect(’sample_database.db’, detect_types=sqlite3.PARSE_DECLTYPES) # 連接到SQLite數(shù)據(jù)庫(kù)’’’sqlite3.PARSE_DECLTYPES本常量使用在函數(shù)connect()里,設(shè)置在關(guān)鍵字參數(shù)detect_types上面。表示在返回一行值時(shí),是否分析這列值的數(shù)據(jù)類(lèi)型定義。如果設(shè)置了本參數(shù),就進(jìn)行分析數(shù)據(jù)表列的類(lèi)型,并返回此類(lèi)型的對(duì)象,并不是返回字符串的形式。sqlite3.PARSE_COLNAMES 本常量使用在函數(shù)connect()里,設(shè)置在關(guān)鍵字參數(shù)detect_types上面。表示在返回一行值時(shí),是否分析這列值的名稱(chēng)。如果設(shè)置了本參數(shù),就進(jìn)行分析數(shù)據(jù)表列的名稱(chēng),并返回此類(lèi)型的名稱(chēng)’’’# 參數(shù):memory:來(lái)創(chuàng)建一個(gè)內(nèi)存數(shù)據(jù)庫(kù)# conn = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES)# Converts np.array to TEXT when insertingsqlite3.register_adapter(np.ndarray, adapt_array)# Converts TEXT to np.array when selectingsqlite3.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ù)庫(kù)表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)閉Cursorconn.close() # 關(guān)閉數(shù)據(jù)庫(kù)

以上就是python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于python numpy數(shù)組的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久激情视频 | 综合久久一区二区三区 | 久久香蕉影院 | 97国产福利| 一区在线免费 | 精品国产第一国产综合精品gif | 国产精品福利片 | 日韩妞干网 | 免费网站看黄 | 久久国产一级毛片一区二区 | 欧美一区二区手机在线观看视频 | 一级做a爱片特黄在线观看免费看 | 国产 高清 在线 | 国产网红在线视频 | 免费观看黄色在线视频 | 国产精品二区三区 | 国产九九免费视频网站 | 夜色在线影院 | 成人免费淫片95视频观看网站 | 国自产拍在线天天更新2019 | 口国产成人高清在线播放 | 亚洲综合日韩欧美一区二区三 | 黄色免费看看 | 国产成人久久精品二区三区 | 国产一二三四区在线观看 | 久久青草免费91线频观看站街 | 网址黄| 黄色片免费在线 | 国产免费a v吧在线观看不卡 | 欧美毛片aaaaa片久久久久 | 国产亚洲欧美久久久久 | 青青草社区| asian极品呦女爱爱 | 亚洲中国日本韩国美国毛片 | 免费人成黄页网站在线观看国产 | 91精品久久久久久久久中文字幕 | 黄色一级毛片免费 | 免费看国产做爰大片 | 午夜资源网 | 亚洲韩精品欧美一区二区三区 | 成人一级黄色大片 |