Lan Yi Education SQLite Database

Source: Internet
Author: User
Tags compact sqlite sqlite database

SQLite is an open-source, embedded relational database that implements a self-contained, 0 configuration, transactional SQL database engine. It is characterized by its high portability, ease of use, compact structure, high efficiency and reliability. Unlike other database management systems, SQLite is very simple to install and run, in most cases-just make sure that sqlite binaries exist to start creating, connecting, and using the database.

1. Introduction

SQLite is an open-source, embedded relational database that implements a self-contained, 0 configuration, transactional SQL database engine. It is characterized by its high portability, ease of use, compact structure, high efficiency and reliability. Unlike other database management systems, SQLite is very simple to install and run, in most cases-just make sure that sqlite binaries exist to start creating, connecting, and using the database. If you are looking for an embedded database project or solution, SQLite is definitely worth considering.

2. Installation

The MAC system is equipped with SQLite

3. Create your first SQLite database

In the Command Line window, enter the following command to create a database named Test.db.

    1. Sqlite3 test.db

To create a table:

    1. sqlite> Create table mytable (ID integer primary key, value text);
    2. 2 columns were created.

The table contains a primary key field named ID and a text field named value.

Note: At a minimum, you must create a table or view for the newly created database so that you can save the database to disk or the database will not be created.

Next, write some data into the table:

    1. sqlite> INSERT INTO mytable (ID, value) values (1, ' Micheal ');
    2. sqlite> INSERT INTO mytable (ID, value) values (2, ' Jenny ');
    3. sqlite> INSERT INTO mytable (value) values (' Francis ');
    4. sqlite> INSERT INTO mytable (value) values (' Kerk ');

Query data:

    1. Sqlite> SELECT * from test;
    2. 1| Micheal
    3. 2| Jenny
    4. 3| Francis
    5. 4| Kerk

To set the results of a formatted query:

    1. sqlite>. Mode column;
    2. Sqlite>. Header on ;
    3. Sqlite> SELECT * from test;
    4. ID value
    5. ----------- -------------
    6. 1 micheal
    7. 2 Jenny
    8. 3 Francis
    9. 4 Kerk

The. Mode column is set to the column display mode, and the. Header displays the column name.

To modify the table structure, add columns:

    1. sqlite> alter table mytable add column Email text not null ' collate nocase;;

To create a view:

    1. Sqlite> Create view Nameview as select * from mytable;

To create an index:

    1. sqlite> Create index test_idx on mytable (value);

4. Some useful SQLite commands

Show Table structure:

    1. Sqlite>. schema [table]

Get all tables and views:

    1. SQLite >. Tables

Gets the list of indexes for the specified table:

    1. SQLite > Indices [table]

To export a database to a SQL file:

  1. SQLite >. output [filename]
  2. SQLite > Dump
  3. SQLite >. Output stdout
  4. To import a database from a SQL file:

      1. SQLite >. read [filename]

    Format output data to CSV format:

      1. SQLite;. output [Filename.csv]
      2. SQLite >.separator,
      3. SQLite > SELECT * from test;
      4. SQLite;. Output stdout

    Import data from a CSV file into a table:

      1. SQLite >Create table newtable (ID integer primary key, value text);
      2. SQLite >.import [Filename.csv] NewTable

    To back up the database:

      1. /* Usage:sqlite3 [database]. dump > [FileName] * /
      2. Sqlite3 mytable.db dump > Backup.sql

    To recover a database:

      1. /* Usage:sqlite3 [database] < [filename] * /
      2. Sqlite3 Mytable.db < Backup.sql

Lan Yi Education SQLite Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.