Home Database Mysql Tutorial python使用sqlite3模块操作sqlite数据库

python使用sqlite3模块操作sqlite数据库

Jun 07, 2016 pm 04:42 PM
python sqlite sqlite3 use operate module

介绍: SQLite是一个包含在C库中的轻量级数据库。它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库。 一些应用可是使用SQLite保存内部数据。它也可以在构建应用原型的时候使用,以便于以后转移到更大型的数

介绍:

 

SQLite是一个包含在C库中的轻量级数据库。它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库。

一些应用可是使用SQLite保存内部数据。它也可以在构建应用原型的时候使用,以便于以后转移到更大型的数据库。

 

Hello,注明下原文地址,blog.xiaorui.cc

 

SQLite的主要优点:

1. 一致性的文件格式:

在SQLite的官方文档中是这样解释的,我们不要将SQLite与Oracle或PostgreSQL去比较,与我们自定义格式的数据文件相比,SQLite不仅提供了很好的

移植性,如大端小端、32/64位等平台相关问题,而且还提供了数据访问的高效性,如基于某些信息建立索引,从而提高访问或排序该类数据的性能,SQLite提供的事务功能,也是在操作普通文件时无法有效保证的。

2. 在嵌入式或移动设备上的应用:

由于SQLite在运行时占用的资源较少,而且无需任何管理开销,因此对于PDA、智能手机等

移动设备来说,SQLite的优势毋庸置疑。

3. 内部数据库:

在有些应用场景中,我们需要为插入到数据库服务器中的数据进行数据过滤或数据清理,以保证最终插入到数据库服务器中的数据有效性。有的时候,数据是否有效,不能通过单一一条记录来进行判断,而是需要和之前一小段时间的历史数据进行特殊的计算,再通过计算的结果判断当前的数据是否合法。

在这种应用中,我们可以用SQLite缓冲这部分历史数据。还有一种简单的场景也适用于SQLite,即统计数据的预计算。比如我们正在运行数据实时采集的服务程序,我们可能需要将每10秒的数据汇总后,形成每小时的统计数据,该统计数据可以极大的减少用户查询时的数据量,从而大幅提高前端程序的查询效率。在这种应用中,我们可以将1小时内的采集数据均缓存在SQLite中,在达到整点时,计算缓存数据后清空该数据。

4. 数据分析:

可以充分利用SQLite提供SQL特征,完成简单的数据统计分析的功能。这一点是yaml,csv文件无法比拟的。

用我的话来说,他很小,很适合做临时的数据库,迁移数据很简单,直接传递文件就可以了。 其实我一开始是选用leveldb的,但是他的特性像nosql,一些稍微复杂的查询,就有些麻烦了。说起来单文件db的模式,最像mysql的还是sqlite了。   

1、创建一个新的数据库:sqlite3     文件名

这个test.db 存放着所有的数据。

sqlite3  rui.db

2、打开一个已经存在的数据库:sqlite3      已经存在的文件名

创建一个新数据库和打开一个已经存在的数据库命令是一模一样的,如果文件在当前目录下不存在,则新建;如果存在,则打开。

3、导入数据:.read     数据文件

打开记事本,并将下列 SQL 语句复制到记事本中,保存为 test.sql 到上面说到的 Db 目录下,在命令行环境中输入

.read   test.sql

即将所有的数据导入到 rui.db 数据库中。

 4、列出所有的数据表: .tables

完成上面所有的工作以后,我们就可以列出所有的数据表了

 

[root@devops-ruifengyun /tmp ]$ sqlite3 rui.db 

SQLite version 3.7.17 2013-05-20 00:56:22

Enter ”.help” for instructions

Enter SQL statements terminated with a ”;”

sqlite> .tables

ceshi  tbl1 

sqlite>

 

 

5、显示数据库结构:.schema

其实就是一些 SQL 语句,他们描述了数据库的结构,如图

 

sqlite> .schema
CREATE TABLE tbl1(one varchar(10), two smallint);
CREATE TABLE ceshi (user text, note text);
Copy after login

6、显示表的结构:.schema 表名

sqlite> .schema ceshi
CREATE TABLE ceshi (user text, note text)
Copy after login

7、导出某个表的数据: .dump 表名

sqlite> .dump tbl1
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE tbl1(one varchar(10), two smallint);
INSERT INTO "tbl1" VALUES('goodbye',20);
INSERT INTO "tbl1" VALUES('hello!',10);
COMMIT;
Copy after login

再来讲解下python sqlite3的用法,其实和mysqldb很像吧,他的语法和mysql差不多

import sqlite3
#原文: xiaorui.cc 
#链接数据库文,sqlite都是以文件的形式存在的。
#如果数据库文件不存在,回新建一个,如果存在则打开此文件
conn = sqlite3.connect('example')
c = conn.cursor()
#创建table
c.execute('''create table ceshi (user text, note text)''')
# 插入数据,执行SQL语句
c.execute('''insert into ceshi (user,note)  values('mPfiJRIH9T','mPfiJRIH9T')''')
c.execute('''insert into ceshi (user,note)  values('7IYcUrKWbw','7IYcUrKWbw')''')
c.execute('''insert into ceshi (user,note)  values('bXB9VcPdnq','bXB9VcPdnq')''')
c.execute('''insert into ceshi (user,note)  values('2JFk7EWcCz','2JFk7EWcCz')''')
c.execute('''insert into ceshi (user,note)  values('QeBFAlYdPr','QeBFAlYdPr')''')
c.execute('''insert into ceshi (user,note)  values('bDL4T69rsj','bDL4T69rsj')''')
c.execute('''insert into ceshi (user,note)  values('BOxPqmkEd9','BOxPqmkEd9')''')
c.execute('''insert into ceshi (user,note)  values('rvBegjXs16','rvBegjXs16')''')
c.execute('''insert into ceshi (user,note)  values('CWrhA2eSmQ','CWrhA2eSmQ')''')
c.execute('''insert into ceshi (user,note)  values('qQicfV2gvG','qQicfV2gvG')''')
c.execute('''insert into ceshi (user,note)  values('s3vg1EuBQb','s3vg1EuBQb')''')
c.execute('''insert into ceshi (user,note)  values('Lne4xj3Xpc','Lne4xj3Xpc')''')
c.execute('''insert into ceshi (user,note)  values('PH3R86CKDT','PH3R86CKDT')''')
c.execute('''insert into ceshi (user,note)  values('HEK7Ymg0Bw','HEK7Ymg0Bw')''')
c.execute('''insert into ceshi (user,note)  values('lim2OCxhQp','lim2OCxhQp')''')
c.execute('''insert into ceshi (user,note)  values('kVFfLljBJI','kVFfLljBJI')''')
c.execute('''insert into ceshi (user,note)  values('Hpbs3VOXNq','Hpbs3VOXNq')''')
c.execute('''insert into ceshi (user,note)  values('f5ubmznBIE','f5ubmznBIE')''')
c.execute('''insert into ceshi (user,note)  values('beJCQA2oXV','beJCQA2oXV')''')
c.execute('''insert into ceshi (user,note)  values('JyPx0iTBGV','JyPx0iTBGV')''')
c.execute('''insert into ceshi (user,note)  values('4S7RQTqw2A','4S7RQTqw2A')''')
c.execute('''insert into ceshi (user,note)  values('ypDgkKi27e','ypDgkKi27e')''')
c.execute('''insert into ceshi (user,note)  values('Anrwx8SbIk','Anrwx8SbIk')''')
c.execute('''insert into ceshi (user,note)  values('k5ZJFrd8am','k5ZJFrd8am')''')
c.execute('''insert into ceshi (user,note)  values('KYcTv54QVC','KYcTv54QVC')''')
c.execute('''insert into ceshi (user,note)  values('Jv6OyfMA0g','Jv6OyfMA0g')''')
c.execute('''insert into ceshi (user,note)  values('kpSLsQYzuV','kpSLsQYzuV')''')
c.execute('''insert into ceshi (user,note)  values('u2zkJQWdOY','u2zkJQWdOY')''')
c.execute('''insert into ceshi (user,note)  values('D0aspFbW8c','D0aspFbW8c')''')
c.execute('''insert into ceshi (user,note)  values('CwqhvDOrWZ','CwqhvDOrWZ')''')
c.execute('''insert into ceshi (user,note)  values('Tdy5LA9sWO','Tdy5LA9sWO')''')
c.execute('''insert into ceshi (user,note)  values('76HnRVbLX0','76HnRVbLX0')''')
c.execute('''insert into ceshi (user,note)  values('B3aoFibRPV','B3aoFibRPV')''')
c.execute('''insert into ceshi (user,note)  values('7Q6lNdL5JP','7Q6lNdL5JP')''')
c.execute('''insert into ceshi (user,note)  values('Hsob6Jyv4A','Hsob6Jyv4A')''')
#将变动保存到数据库文件,如果没有执行词语句,则前面的insert 语句操作不会被保存
conn.commit()
c.execute('''select * from ceshi ''').fetchall()
#得到所有的记录
rec = c.execute('''select * from ceshi''')
print c.fetchall()
Copy after login

 

 

SQlite 适合做一些个模拟开发环境,或者是不需要mysql这样的server的应用。但是谁又能抗住Sqlite的速度呢 ?  我肯定不会主要的去选择Sqlite,有人说,sqlite和mysql很像,可以很容易移植代码,这东西维护起来可不简简单单是文件那么简单,我记得有次遇到个线程安全的问题,可是把我坑鸟了。  吐槽完毕  ~

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How is the GPU support for PyTorch on CentOS How is the GPU support for PyTorch on CentOS Apr 14, 2025 pm 06:48 PM

Enable PyTorch GPU acceleration on CentOS system requires the installation of CUDA, cuDNN and GPU versions of PyTorch. The following steps will guide you through the process: CUDA and cuDNN installation determine CUDA version compatibility: Use the nvidia-smi command to view the CUDA version supported by your NVIDIA graphics card. For example, your MX450 graphics card may support CUDA11.1 or higher. Download and install CUDAToolkit: Visit the official website of NVIDIACUDAToolkit and download and install the corresponding version according to the highest CUDA version supported by your graphics card. Install cuDNN library:

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

MiniOpen Centos compatibility MiniOpen Centos compatibility Apr 14, 2025 pm 05:45 PM

MinIO Object Storage: High-performance deployment under CentOS system MinIO is a high-performance, distributed object storage system developed based on the Go language, compatible with AmazonS3. It supports a variety of client languages, including Java, Python, JavaScript, and Go. This article will briefly introduce the installation and compatibility of MinIO on CentOS systems. CentOS version compatibility MinIO has been verified on multiple CentOS versions, including but not limited to: CentOS7.9: Provides a complete installation guide covering cluster configuration, environment preparation, configuration file settings, disk partitioning, and MinI

How to operate distributed training of PyTorch on CentOS How to operate distributed training of PyTorch on CentOS Apr 14, 2025 pm 06:36 PM

PyTorch distributed training on CentOS system requires the following steps: PyTorch installation: The premise is that Python and pip are installed in CentOS system. Depending on your CUDA version, get the appropriate installation command from the PyTorch official website. For CPU-only training, you can use the following command: pipinstalltorchtorchvisiontorchaudio If you need GPU support, make sure that the corresponding version of CUDA and cuDNN are installed and use the corresponding PyTorch version for installation. Distributed environment configuration: Distributed training usually requires multiple machines or single-machine multiple GPUs. Place

How to choose the PyTorch version on CentOS How to choose the PyTorch version on CentOS Apr 14, 2025 pm 06:51 PM

When installing PyTorch on CentOS system, you need to carefully select the appropriate version and consider the following key factors: 1. System environment compatibility: Operating system: It is recommended to use CentOS7 or higher. CUDA and cuDNN:PyTorch version and CUDA version are closely related. For example, PyTorch1.9.0 requires CUDA11.1, while PyTorch2.0.1 requires CUDA11.3. The cuDNN version must also match the CUDA version. Before selecting the PyTorch version, be sure to confirm that compatible CUDA and cuDNN versions have been installed. Python version: PyTorch official branch

How to install nginx in centos How to install nginx in centos Apr 14, 2025 pm 08:06 PM

CentOS Installing Nginx requires following the following steps: Installing dependencies such as development tools, pcre-devel, and openssl-devel. Download the Nginx source code package, unzip it and compile and install it, and specify the installation path as /usr/local/nginx. Create Nginx users and user groups and set permissions. Modify the configuration file nginx.conf, and configure the listening port and domain name/IP address. Start the Nginx service. Common errors need to be paid attention to, such as dependency issues, port conflicts, and configuration file errors. Performance optimization needs to be adjusted according to the specific situation, such as turning on cache and adjusting the number of worker processes.

See all articles