SQLit

https://www.sqlite.org

https://en.wikipedia.org

  1. SQLite是一个进程内库,它实现了一个自包含的、无服务器的、零配置的事务性SQL数据库引擎。跨平台,不支持远程连接。
  2. SQLite没有单独的服务器进程。SQLite直接读写普通磁盘文件。一个包含多个表、索引、触发器和视图的完整SQL数据库包含在一个磁盘文件中。
  3. 根据使用方式的不同,SQLite可以比直接文件系统I/O更快。
  4. 事务性数据库是这样一种数据库:所有的更改和查询看起来都是原子的、一致的、隔离的和持久的(ACID)。SQLite实现可序列化的事务,这些事务是原子的、一致的、隔离的和持久的,即使事务被程序崩溃、操作系统崩溃或计算机电源故障中断。

数据库管理系统中事务(transaction)的四个特性ACID

  1. 原子性(Atomicity):事务要么成功(可见),要么失败(不可见)。不存在事务部分成功的情况。
  2. 一致性(Consistency):数据库在事务开始前和结束后都应该是一致的。
  3. 隔离性(Isolation):一个事务不会看到另外一个还未完成的事务产生的结果。每个事务就像在单独、隔离的环境下运行一样。
  4. 持久性(Durability):成功提交的事务,数据是持久保留,不会因为系统失败而丢失。

SQLite的适当使用

  1. SQLite不能直接与客户机/服务器SQL数据库引擎(如MySQL、Oracle、PostgreSQL或SQL server)相提并论,因为SQLite试图解决的是另一个问题。
  2. 客户机/服务器SQL数据库引擎努力实现企业数据的共享存储库。它们强调可伸缩性、并发性、集中和控制。SQLite致力于为单个应用程序和设备提供本地数据存储。SQLite强调经济、高效、可靠、独立和简单。

Situations Where SQLite Works Well

  1. 嵌入式设备和物联网
  2. 应用程序文件格式

    SQLite通常用作桌面应用程序(如版本控制系统、财务分析工具、媒体编目和编辑套件、CAD软件包、记录保存程序等)的磁盘文件格式。
  3. 网站

    SQLite作为大多数低到中等流量的网站(也就是说,大多数网站)的数据库引擎非常出色。SQLite能够处理的web流量数量取决于网站使用其数据库的程度。一般来说,任何一天的点击率低于100K的站点都可以使用SQLite。100,000次/天的命中率是一个保守的估计,而不是一个严格的上限。SQLite已经被证明可以处理10倍于此的流量。
  4. 数据分析

SQLite特点总结

  • sqlite最适合的应用场景是作为本地数据库使用,其次是作为网站的数据库。
  • 不适合直接访问数据库的客户机/服务器应用程序

    (如果有许多客户机程序通过网络将SQL发送到同一个数据库,那么使用客户机/服务器数据库引擎而不是SQLite。SQLite将在网络文件系统上工作,但由于与大多数网络文件系统相关的延迟,性能不会很好。)
  • 不适用于非常大的数据集

    SQLite数据库的大小限制为140 terabytes (247字节,128 tibibytes)。即使它可以处理更大的数据库,SQLite也将整个数据库存储在一个磁盘文件中,许多文件系统将文件的最大大小限制在小于此的范围内。例如NTFS文件系统支持的最大单一文件大小2TB。
    FAT16(最大分区2GB,最大文件2GB ,最大容量)。
    FAT32(最大分区32GB,最大容量2TB,最大文件32G )

Here is what you do to start experimenting with SQLite without having to do a lot of tedious reading and configuration:

Download The Code
Get a copy of the prebuilt binaries for your machine, or get a copy of the sources and compile them yourself. Visit the download page for more information.

Create A New Database
At a shell or DOS prompt, enter: "sqlite3 test.db". This will create a new database named "test.db". (You can use a different name if you like.)

Enter SQL commands at the prompt to create and populate the new database.

Additional documentation is available here.

Write Programs That Use SQLite

Below is a simple C program that demonstrates how to use the C/C++ interface to SQLite. The name of a database is given by the first argument and the second argument is one or more SQL statements to execute against the database. The function calls to pay attention to here are the call to sqlite3_open() on line 22 which opens the database, sqlite3_exec() on line 28 that executes SQL commands against the database, and sqlite3_close() on line 33 that closes the database connection.

See also the Introduction To The SQLite C/C++ Interface for an introductory overview and roadmap to the dozens of SQLite interface functions.

01  #include <stdio.h>
02  #include <sqlite3.h>
03  
04  static int callback(void *NotUsed, int argc, char **argv, char **azColName){
05    int i;
06    for(i=0; i<argc; i++){
07      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
08    }
09    printf("\n");
10    return 0;
11  }
12  
13  int main(int argc, char **argv){
14    sqlite3 *db;
15    char *zErrMsg = 0;
16    int rc;
17  
18    if( argc!=3 ){
19      fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
20      return(1);
21    }
22    rc = sqlite3_open(argv[1], &db);
23    if( rc ){
24      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
25      sqlite3_close(db);
26      return(1);
27    }
28    rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
29    if( rc!=SQLITE_OK ){
30      fprintf(stderr, "SQL error: %s\n", zErrMsg);
31      sqlite3_free(zErrMsg);
32    }
33    sqlite3_close(db);
34    return 0;
35  }

See the How To Compile SQLite document for instructions and hints on how to compile the program shown above.

python 内置SQLite3

SQLite支持常见的标准SQL语句以及几种常见的数据类型。具体文档请参阅SQLite官方网站。

# 导入SQLite驱动:
>>> import sqlite3
# 连接到SQLite数据库
# 数据库文件是test.db
# 如果文件不存在,会自动在当前目录创建:
>>> conn = sqlite3.connect('test.db')
# 创建一个Cursor:
>>> cursor = conn.cursor()
# 执行一条SQL语句,创建user表:
>>> cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
<sqlite3.Cursor object at 0x10f8aa260>
# 继续执行一条SQL语句,插入一条记录:
>>> cursor.execute('insert into user (id, name) values (\'1\', \'Michael\')')
<sqlite3.Cursor object at 0x10f8aa260>
# 通过rowcount获得插入的行数:
>>> cursor.rowcount
1
# 关闭Cursor:
>>> cursor.close()
# 提交事务:
>>> conn.commit()
# 关闭Connection:
>>> conn.close()

我们再试试查询记录:

>>> conn = sqlite3.connect('test.db')
>>> cursor = conn.cursor()
# 执行查询语句:
>>> cursor.execute('select * from user where id=?', ('1',))
<sqlite3.Cursor object at 0x10f8aa340>
# 获得查询结果集:
>>> values = cursor.fetchall()
>>> values
[('1', 'Michael')]
>>> cursor.close()
>>> conn.close()

如果SQL语句带有参数,那么需要把参数按照位置传递给execute()方法,有几个?占位符就必须对应几个参数,例如:

cursor.execute('select * from user where name=? and pwd=?', ('abc', 'password'))

在Python中操作数据库时,要先导入数据库对应的驱动,然后,通过Connection对象和Cursor对象操作数据。

要确保打开的Connection对象和Cursor对象都正确地被关闭,否则,资源就会泄露。

如何才能确保出错的情况下也关闭掉Connection对象和Cursor对象呢?请回忆try:...except:...finally:...的用法。

其中conn对象是数据库链接对象,而对于数据库链接对象来说,具有以下操作:

    commit()            --事务提交
    rollback()          --事务回滚
    close()             --关闭一个数据库链接
    cursor()            --创建一个游标

对于游标对象cursor,具有以下具体操作:

    execute()           --执行一条sql语句
    executemany()       --执行多条sql语句
    close()             --游标关闭
    fetchone()          --从结果中取出一条记录
    fetchmany()         --从结果中取出多条记录
    fetchall()          --从结果中取出所有记录
    scroll()            --游标滚动

转载于:https://www.cnblogs.com/ravener/p/9807054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值