Python 操作sqlite数据库及保存查询numpy类型数据(二)
2024-09-03 14:07:11
# -*- coding: utf-8 -*-
'''
Created on 2019年3月6日
@author: Administrator
'''
import sqlite3
import numpy as np
import json
# 创建数据库连接对象
conn = sqlite3.connect('sample_database.db', isolation_level=None) # 连接到SQLite数据库
'''
参数isolation_level是同Conection.isolation_level的属性意义一样
'''
# 参数:memory:来创建一个内存数据库
# conn = sqlite3.connect(":memory:", isolation_level=None)
x = np.arange(12).reshape(2, 6)
# conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
# 删除数据库表
cursor.execute("DROP TABLE test2")
# 创建数据库表
cursor.execute("create table test2 (arr BLOB)")
# 插入一行数据,numpy.array转List,json.dumps()函数是将字典转化为字符串
cursor.execute("insert into test2 (arr) values (?)", (json.dumps(x.tolist()),))
# 提交
conn.commit()
cursor.execute("select arr from test2")
data = cursor.fetchall()
print(data)
print(type(data))
# json.loads()函数是将字符串转化为字典
my_list = json.loads(data[0][0])
# List转numpy.array
temp = np.array(my_list)
print(temp)
print(type(temp))
cursor.close() # 关闭Cursor
conn.close() # 关闭数据库
最新文章
- PO VO DAO DTO BO TO概念与区别(转)
- github的使用步骤及体会
- rabbitMQ第四篇:远程调用
- uvalive4327(单调队列优化)
- 12.TCP的成块数据流
- Django__WSGI
- 简单介绍shell编程四剑客之grep
- 第一个SDL程序
- LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
- python之GIL官方文档 global interpreter lock 全局解释器锁
- sql格式化并高亮
- linux bin &; sbin different
- iOS webview加载时序和缓存问题总结
- 使用AOF持久化文件实现还原Redis数据库并得到RDB持久化文件
- Effective Java 第三版——61. 基本类型优于装箱的基本类型
- 使用Code First建模自引用关系笔记
- vSphere Replication:虚拟机的保护伞
- java集合的中的集合关系实现或继承关系图
- elasticsearch关于索引切分的实现
- 数据结构与算法JavaScript描述——列表
热门文章
- 在Java web模板的上进行编写
- logstash之OutPut插件
- ElasticSearch的介绍
- sqli-lab(16)
- 微信小程序打印json log
- CSS注
- php数组合并用加号(+)和用array_merge()的区别
- leetcode 441.排列硬币(python)
- 阶段3 1.Mybatis_07.Mybatis的连接池及事务_5 mybatis中使用poolead配置连接的原理分析
- 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_7 Mybatis中使用代理Dao的执行过程分析