Python 操作sqlite数据库及保存查询numpy类型数据(二)
# -*- 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() # 关闭数据库
Python 操作sqlite数据库及保存查询numpy类型数据(二)的更多相关文章
- Python 操作sqlite数据库及保存查询numpy类型数据(一)
# -*- coding: utf-8 -*- ''' Created on 2019年3月6日 @author: Administrator ''' import sqlite3 import nu ...
- Python操作sqlite数据库小节
学习了Python操作sqlite数据库,做一个小结,以备后用. import sqlite3import os# 进行数据库操作时,主要是参数如何传输try:# 链接数据库conn=sqlite3. ...
- Python操作SQLite数据库的方法详解
Python操作SQLite数据库的方法详解 本文实例讲述了Python操作SQLite数据库的方法.分享给大家供大家参考,具体如下: SQLite简单介绍 SQLite数据库是一款非常小巧的嵌入式开 ...
- Python 操作 SQLite 数据库
写在之前 SQLite 是一个小型的关系型数据库,它最大的特点在于不需要单独的服务.零配置.我们在之前讲过的两个数据库,不管是 MySQL 还是 MongoDB,都需要我们安装.安装之后,然后运行起来 ...
- python操作sqlite数据库
root@cacti:~/box# cat convert.py #!/usr/bin/env python import sqlite3,time,rrdtool,os def boxstatus( ...
- 8.1 使用Python操作SQLite数据库
SQLite是内嵌在Python中的轻量级.基于磁盘文件袋额数据库管理系统,不需要安装和配置服务,支持使用SQL语句来访问数据库.该数据库使用C语言开发,支持大多数SQL91标准,支持原子的.一致的. ...
- python 操作sqlite数据库
'''SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说 没有独立的维护进程,所有的维护都来自于程序本身. 在python中,使用sqlite3创建数据库的连接,当我们指定的数据库文件不 ...
- 42.QT-QSqlQuery类操作SQLite数据库(创建、查询、删除、修改)详解
Qt 提供了 QtSql 模块来提供平台独立的基于 SQL 的数据库操作.这里我们所说的“平台 独立”,既包括操作系统平台,也包括各个数据库平台,Qt支持以下几种数据库: QT自带SQLITE数据库, ...
- Python操作Mysql数据库进阶篇——查询操作详解(一)
前面我们已经介绍了在Python3.x中如何连接一个Mysql数据库,以及怎么样对这个数据库创建一个表,增删改查表里的数据.想必大家对Mysql数据库和简单的sql语句有了一定的了解,其实sql语句博 ...
随机推荐
- es索引基本操作(1)
1:创建索引 创建索引的时候可以通过number_of_shards和number_of_replicas来指定当前索引的分片和副本数量: PUT demo_index1 { " ...
- bootstrap editable 行内编辑
除了那些bootstrap/bootstrap table的js , css之外,要额外添加editable的文件: <link href="../assets/css/bootstr ...
- 【Python】学习笔记五:缩进与选择
Python最具特色的用缩进来标明成块的代码 缩进 i = 4 j = 2 if i > j: i = i+1 print(i) 这是一个简单的判断,Python的if使用很简单,没有括号等繁琐 ...
- Oracle开发:创建一个用户并分配表空间和分配权限
-- 创建一个用户并分配表空间和分配权限 -- 以sysdba登录 oracle@sha-col-oracle-2:~> sqlplus / as sysdba SQL*Plus: Releas ...
- serial redirection
int setOption(int fd,int nSpeed, int nBits, char mode,char nEvent, int nStop) { struct termios newti ...
- STL标准库-容器-set与map
STL标准库-容器-set与multiset C++的set https://www.cnblogs.com/LearningTheLoad/p/7456024.html STL标准库-容器-map和 ...
- ubuntu通过windows下的ccproxy代理上网
网上教程很多,需要注意的是将ubuntu的ip和windows的Ip设置到同一个网段,即子网掩码是1的对应的部分要相同.由于没有配置到同一个网段,折腾了我好久.
- freemarker程序开发
1.程序开发入门 1.1 创建配置实例 首先,你应该创建一个freemarker.template.Configuration的实例,然后调整它的设置.Configuration实例是存储FreeMa ...
- mysql 批量删表
Select CONCAT( 'drop table ', table_name, ';' ) FROM information_schema.tables Where table_name LIKE ...
- RESR API (二)之Responses
Responses 与基本的HttpResponse对象不同,TemplateResponse对象保留 the details of the context that was provided by ...