转载说明:

1.原文地址:http://www.askyb.com/sqlite/learn-sqlite-in-1-hour/

2.译文地址:http://www.oschina.net/question/12_53183(红薯翻译)

3.英文原文有5处错误,下面的已经修正过了

原文如下:

Learn SQLite in 1 hour

askyb on May, 9th 2012 in SQLite

1. Introduction

SQLite is an open source, embedded relational database which implements a self-contained, serverless, zero-configuration,transactional SQL database engine. SQLite has a well-deserved reputation for being highly portable, easy to use, compact, efficient, and reliable. Unlike client–server database management systems, installing and running of SQLite database is pretty straightforward in most cases — just make sure the SQLite binaries file exists anywhere you want and start to create, connect, and using the database. If you are looking for an embedded database for your projects or solutions, SQLite is definitely worth considering.

(SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下 - 只要确保SQLite的二进制文件存在即可开始创建、连接和使用数据库。如果您正在寻找一个嵌入式数据库项目或解决方案,SQLite是绝对值得考虑。[红薯 译])

2. Installation

SQLite on Windows

  1. Navigate to SQLite download page at http://www.sqlite.org/download.html
  2. Download the following Precompiled Binaries For Windows:
    • sqlite-shell-win32-x86-<build#>.zip
    • sqlite-dll-win32-x86-<build#>.zip

    Note: <build#> is the running build number of sqlite.

  3. Unpack the ZIP files into your favourite folder. Add folder path to the PATH system variable to make the SQLite command line shell available within the environment.
  4. OPTIONAL: If you plan to develop any application that host a sqlite database then you will need to download the source code in order to compile and utilize its API .
    • sqlite-amalgamation-<build#>.zip

SQLite on Linux
SQLite binaries can be obtained in a variety of ways depending on the Linux distro that you are using.

/* For Debian or Ubuntu /*
$ sudo apt-get install sqlite3 libsqlite3-dev /* For RedHat, CentOS, or Fedora/*
$ yum install SQLite3 libsqlite3-dev

SQLite on Mac OS X
If you are running a Mac OS Leopard or later, then it alraedy have pre-installed SQLite.

3. Create you first SQLite Database

you now should have the SQLite binaries ready and time to create your first SQLite database now. Type the following command in windows’s command prompt or Linux’s terminal.

To create a new database called test.db:

sqlite3 test.db

To create a table in the database:

sqlite> create table mytable(id integer primary key, value text);

2 columns were created.A primary key column called “id” which has the ability to automatically generate value by default and a simple text field called “value”.

NOTE: At least 1 table or view need to be created in order to commit the new database to disk. Otherwise, it won’t database won’t be created.

To insert data into mytable:

sqlite> insert into mytable(id, value) values(1, 'Micheal');
sqlite> insert into mytable(id, value) values(2, 'Jenny');
sqlite> insert into mytable(value) values('Francis');
sqlite> insert into mytable(value) values('Kerk');

To fetch data from mytable:

sqlite> select * from mytable;
1|Micheal
2|Jenny
3|Francis
4|Kerk

To fetch data from mytable by improving the formatting a little:

sqlite> .mode column
sqlite> .header on
sqlite> select * from mytable;
id value
----------- -------------
1 Micheal
2 Jenny
3 Francis
4 Kerk

The .mode column will display data into column format.
The .header on will display table’s column name.

To add additional column into mytable:

sqlite> alter table mytable add column email text not null '' collate nocase;

To create a view for mytable:

sqlite> create view nameview as select * from mytable;

To create an index for mytable:

sqlite> create index test_idx on mytable(value);

4. Useful SQLite’s command

Display table schema:

sqlite> .schema [table]

Retrieve a list of tables (and views):

sqlite> .tables

Retrieve a list indexes for a given table:

sqlite> .indices [table]

Export database objects to SQL format:

sqlite> .output [filename]
sqlite> .dump
sqlite> .output stdout

Import database objects(SQL format) to database:

sqlite> .read [filename]

Formatting exported data into CSV format:

sqlite>.output [filename.csv]
sqlite>.separator ,
sqlite> select * from mytable;
sqlite>.output stdout

Import CSV formatted data to a new table:

sqlite>create table newtable(id integer primary key, value text);
sqlite>.import [filename.csv] newtable

To backup database:

/* usage: sqlite3 [database] .dump > [filename] */
sqlite3 mytable.db .dump > backup.sql

To restore database:

/* usage: sqlite3 [database] < [filename] */
sqlite3 mytable.db < backup.sql

[转]Learn SQLite in 1 hour的更多相关文章

  1. Wiki安装(PHP +Sqlite+Cache)

    前期准备 PHP http://windows.php.net/download   WinCache Extension for PHP URL:http://sourceforge.net/pro ...

  2. SQLite入门与分析(二)---设计与概念

    写在前面:谢谢各位的关注,没想到会有这么多人关注.高兴的同时,也感到压力,因为我接触SQLite也就几天,也没在实际开发中用过,只是最近项目的需求才来研究它,所以我很担心自己的文章是否会有错误,误导别 ...

  3. An Introduction To The SQLite C/C++ Interface

    1. Summary The following two objects and eight methods comprise the essential elements of the SQLite ...

  4. 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Reminders Lab: Part 2 This chapter covers capturing user input through the ...

  5. 第五章:Reminders实验:第一部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 By now you are familiar with the basics of creating a new project, program ...

  6. Persisting iOS Application Data in SQLite Database Using FMDB

    In previous articles we have utilized NSUserDefaults and .NET web services to persist iPhone data. N ...

  7. [转]Android | Simple SQLite Database Tutorial

    本文转自:http://hmkcode.com/android-simple-sqlite-database-tutorial/ Android SQLite database is an integ ...

  8. 【开源】分享2011-2015年全国城市历史天气数据库【Sqlite+C#访问程序】

    由于个人研究需要,需要采集天气历史数据,前一篇文章:C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子),介绍了基本的采集思路和核心代码,经过1个星期的采集,历史数据库 ...

  9. UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?

    选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...

随机推荐

  1. java 源码分析2 -List

    1.是一个接口,继承了Collection,提供了size(),isEmpty(),contanis(),iterator(),toArray(),clear()等方法 2.分析常用的ArrayLis ...

  2. 四则运算结对编程(GUI)

    四则运算GUI coding地址:https://git.dev.tencent.com/qyj814/GUI.git 结对伙伴:李梦宇 一.题目要求 定制出题要求.每次出题时用户都可以在界面上定制如 ...

  3. 1048 石子归并codevs

    1048 石子归并codevs 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1 ...

  4. Ubuntu 16.04监控网络带宽软件

    1.系统监控器已经自带了默认的监控功能,包括CPU.内存.带宽.进程等,但带宽的一些详情不能显示出来. 2.使用top命令进行监控进程 sudo top 3.安装Indicator-sysmonito ...

  5. [Angular] New in V6.1

    Router Scroll Position Restoration: remember and restore scroll position as the user navigates aroun ...

  6. HTML5权威指南之—第三章

    HTML页面上元素的焦点能够通过"tab"键在各个元素之间切换,使用"tabindex"属性能够改变默认的转移顺序 Tabindex为1的元素会首先被选中.然后 ...

  7. Linux声卡驱动移植和測试

    一.分析驱动程序,依据开发板改动代码 代码太长,就不贴了,几个注意点: 1. 查看开发板原理图和S3C2410的datasheet,UDA1341的L3MODE.L3DATA.L3CLOCK分别与S3 ...

  8. jstl自己定义函数的使用

    因为本人之前并没有接触过jstl标签,说来也可笑,之前一直使用struts2标签.近期项目用到jstl,所以做些记录方便以后自己查看. jstl的强大原因之中的一个我觉得就是他的自己定义函数,我们能够 ...

  9. Java程序学习中各阶段的建议

    第一部分:对于尚未做过Java工作的同学,包括一些在校生以及刚准备转行Java的同学. 一.Java基础 首先去找一个Java的基础教程学一下,这里可以推荐一个地址,或者你也可以参照这个地址上去找相应 ...

  10. Mysql 存储引擎中InnoDB与MyISAM差别(网络整理)

    1. 事务处理 innodb 支持事务功能,myisam 不支持. Myisam 的运行速度更快,性能更好. 2,select ,update ,insert ,delete 操作 MyISAM:假设 ...