濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法

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

熱門(mén)標(biāo)簽:地圖標(biāo)注視頻廣告 高德地圖標(biāo)注是免費(fèi)的嗎 大連crm外呼系統(tǒng) 梅州外呼業(yè)務(wù)系統(tǒng) 無(wú)錫客服外呼系統(tǒng)一般多少錢 老人電話機(jī)器人 洪澤縣地圖標(biāo)注 北京電信外呼系統(tǒng)靠譜嗎 百度地圖標(biāo)注位置怎么修改

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

#查詢數(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ù)及保存查詢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ù)庫(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ù)類型定義。如果設(shè)置了本參數(shù),就進(jìn)行分析數(shù)據(jù)表列的類型,并返回此類型的對(duì)象,并不是返回字符串的形式。

sqlite3.PARSE_COLNAMES 
本常量使用在函數(shù)connect()里,設(shè)置在關(guān)鍵字參數(shù)detect_types上面。表示在返回一行值時(shí),是否分析這列值的名稱。如果設(shè)置了本參數(shù),就進(jìn)行分析數(shù)據(jù)表列的名稱,并返回此類型的名稱
'''
# 參數(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 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ù)庫(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)閉Cursor
conn.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)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • Python NumPy灰度圖像的壓縮原理講解
  • Python多進(jìn)程共享numpy 數(shù)組的方法
  • python圖像處理基本操作總結(jié)(PIL庫(kù)、Matplotlib及Numpy)
  • python numpy中multiply與*及matul 的區(qū)別說(shuō)明
  • 淺談Python numpy創(chuàng)建空數(shù)組的問(wèn)題
  • Python NumPy中diag函數(shù)的使用說(shuō)明
  • Python機(jī)器學(xué)習(xí)三大件之一numpy
  • python利用numpy存取文件案例教程

標(biāo)簽:吉林 長(zhǎng)春 洛陽(yáng) 怒江 岳陽(yáng) 清遠(yuǎn) 安慶 泉州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  python,中,sqllite,插入,numpy,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    怀仁县| 湖北省| 滕州市| 沂源县| 周至县| 肇东市| 五家渠市| 通山县| 大港区| 微山县| 新乡县| 渭源县| 安康市| 武乡县| 商都县| 清水河县| 凌源市| 北安市| 囊谦县| 双流县| 诸城市| 邢台市| 土默特左旗| 绿春县| 乐安县| 镇赉县| 同仁县| 三原县| 平湖市| 桐梓县| 措勤县| 平陆县| 岑溪市| 奎屯市| 上饶县| 青州市| 汪清县| 都兰县| 分宜县| 泰州市| 高碑店市|