<bdo id='JPDqc'></bdo><ul id='JPDqc'></ul>

    <legend id='JPDqc'><style id='JPDqc'><dir id='JPDqc'><q id='JPDqc'></q></dir></style></legend>

  1. <small id='JPDqc'></small><noframes id='JPDqc'>

    1. <i id='JPDqc'><tr id='JPDqc'><dt id='JPDqc'><q id='JPDqc'><span id='JPDqc'><b id='JPDqc'><form id='JPDqc'><ins id='JPDqc'></ins><ul id='JPDqc'></ul><sub id='JPDqc'></sub></form><legend id='JPDqc'></legend><bdo id='JPDqc'><pre id='JPDqc'><center id='JPDqc'></center></pre></bdo></b><th id='JPDqc'></th></span></q></dt></tr></i><div id='JPDqc'><tfoot id='JPDqc'></tfoot><dl id='JPDqc'><fieldset id='JPDqc'></fieldset></dl></div>
      <tfoot id='JPDqc'></tfoot>

      Python将numpy数组插入到sqlite3数据库中

      时间:2023-09-18
      <i id='W0wKz'><tr id='W0wKz'><dt id='W0wKz'><q id='W0wKz'><span id='W0wKz'><b id='W0wKz'><form id='W0wKz'><ins id='W0wKz'></ins><ul id='W0wKz'></ul><sub id='W0wKz'></sub></form><legend id='W0wKz'></legend><bdo id='W0wKz'><pre id='W0wKz'><center id='W0wKz'></center></pre></bdo></b><th id='W0wKz'></th></span></q></dt></tr></i><div id='W0wKz'><tfoot id='W0wKz'></tfoot><dl id='W0wKz'><fieldset id='W0wKz'></fieldset></dl></div>
        <bdo id='W0wKz'></bdo><ul id='W0wKz'></ul>

          <tbody id='W0wKz'></tbody>

          • <small id='W0wKz'></small><noframes id='W0wKz'>

                <legend id='W0wKz'><style id='W0wKz'><dir id='W0wKz'><q id='W0wKz'></q></dir></style></legend>
                <tfoot id='W0wKz'></tfoot>
              1. 本文介绍了Python将numpy数组插入到sqlite3数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试在 sqlite3 数据库中存储大约 1000 个浮点数的 numpy 数组,但我不断收到错误InterfaceError:错误绑定参数 1 - 可能不受支持的类型".

                I'm trying to store a numpy array of about 1000 floats in a sqlite3 database but I keep getting the error "InterfaceError: Error binding parameter 1 - probably unsupported type".

                我的印象是 BLOB 数据类型可以是任何类型,但它绝对不适用于 numpy 数组.这是我尝试过的:

                I was under the impression a BLOB data type could be anything but it definitely doesn't work with a numpy array. Here's what I tried:

                import sqlite3 as sql
                import numpy as np
                con = sql.connect('test.bd',isolation_level=None)
                cur = con.cursor()
                cur.execute("CREATE TABLE foobar (id INTEGER PRIMARY KEY, array BLOB)")
                cur.execute("INSERT INTO foobar VALUES (?,?)", (None,np.arange(0,500,0.5)))
                con.commit()
                

                是否有另一个模块可以用来将 numpy 数组放入表中?或者我可以将 numpy 数组转换为 SQLite 可以接受的 Python 中的另一种形式(比如我可以拆分的列表或字符串)?性能不是优先事项.我只是想让它工作!

                Is there another module I can use to get the numpy array into the table? Or can I convert the numpy array into another form in Python (like a list or string I can split) that sqlite will accept? Performance isn't a priority. I just want it to work!

                谢谢!

                推荐答案

                您可以使用 sqlite3 注册一个新的 array 数据类型:

                You could register a new array data type with sqlite3:

                import sqlite3
                import numpy as np
                import io
                
                def adapt_array(arr):
                    """
                    http://stackoverflow.com/a/31312102/190597 (SoulNibbler)
                    """
                    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)
                
                
                # 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)
                
                con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
                cur = con.cursor()
                cur.execute("create table test (arr array)")
                

                通过此设置,您可以简单地插入 NumPy 数组,而无需更改语法:

                With this setup, you can simply insert the NumPy array with no change in syntax:

                cur.execute("insert into test (arr) values (?)", (x, ))
                

                并直接从 sqlite 检索数组作为 NumPy 数组:

                And retrieve the array directly from sqlite as a NumPy array:

                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'>
                

                这篇关于Python将numpy数组插入到sqlite3数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何将自定义属性添加到 SQL 连接字符串? 下一篇:在 SQL Server 上使用 varchar(MAX) 与 TEXT

                相关文章

              2. <legend id='flLeg'><style id='flLeg'><dir id='flLeg'><q id='flLeg'></q></dir></style></legend>

                <small id='flLeg'></small><noframes id='flLeg'>

                • <bdo id='flLeg'></bdo><ul id='flLeg'></ul>
                <tfoot id='flLeg'></tfoot>

                    <i id='flLeg'><tr id='flLeg'><dt id='flLeg'><q id='flLeg'><span id='flLeg'><b id='flLeg'><form id='flLeg'><ins id='flLeg'></ins><ul id='flLeg'></ul><sub id='flLeg'></sub></form><legend id='flLeg'></legend><bdo id='flLeg'><pre id='flLeg'><center id='flLeg'></center></pre></bdo></b><th id='flLeg'></th></span></q></dt></tr></i><div id='flLeg'><tfoot id='flLeg'></tfoot><dl id='flLeg'><fieldset id='flLeg'></fieldset></dl></div>