【SQLite 3】1_sql共通文紹介

6594 ワード

ベースデータ型
を選択します.
説明
int(integer)
-2^31(-21147483648)~2^31(2147483647)の整数値
float
-1.79 E+308~1.79 E+308可変精度の数値
real
-3.04 E+38~3.04 E+38可変精度の数値
char
Unicode以外の文字型データを最長8000
varchar
Unicode以外の文字型データが長くなり、最大長8000
text
Unicode以外の文字型データが長くなり、最大長さは2^31-1(2 G)
例:
date(生年月日)
name(名前)
gender(性別)
ホビー(趣味)
char(16)
varchar(256)
char(16)
text
19940801
シロ

C++
19940802
黒ちゃん

PHP
sql汎用文
注意:コマンドライン文の最後に追加する
; (セミコロン)
データベースの作成
  • .Openデータベース名
  • 注意:sqlite 3が提供するコマンドライン機能文、sql汎用文ではない
    .open person.db

    データテーブルの作成
  • create tableテーブル名(フィールド名データ型、フィールド名データ型、フィールド名データ型).
  • create table child(date char(16), name varchar(256), gender char(16), hobby text);

    データの挿入
  • insert intoテーブル名values('フィールドデータ','フィールドデータ','フィールドデータ','フィールドデータ');

  • 注意:データ型がchar,varcharの場合、textデータは''または''参照を使用する必要があります.
    insert into child values('19940801', '  ', ' ', 'C++');
    insert into child values('19940802', '  ', ' ', 'PHP');

    データの問合せ
  • selectフィールド名,...,フィールド名fromテーブル名;

  • 注意:フィールド名が複数の場合は「,」カンマで区切られ、すべての場合は「*」アスタリスクで区切られます.
    すべて検索:
    sqlite> select * from child;
    19940801|  | |C++
    19940802|  | |PHP

    複数クエリー:
    sqlite> select name, date from child;
      |19940801
      |19940802
  • selectフィールド名,...,フィールド名fromテーブル名where条件;
  • sqlite> select * from child where gender=' ';
    19940802|  | |PHP
  • ファジイ条件クエリーlike("%"ワイルドカード)
  • を使用
    sqlite> select * from child where date like '%1';
    19940801|  | |C++
  • and(2つの条件が同時に成立)
  • sqlite> select * from child where date like '%1' and hobby like 'C%';
    19940801|  | |C++
  • or(いずれかの条件が成立する)
  • sqlite> select * from child where date like '%1' or  hobby like 'P%';
    19940801|  | |C++
    19940802|  | |PHP

    データの更新
  • updateテーブル名setフィールド1=フィールド1値、フィールド2=フィールド2値...where条件式
  • sqlite> update child set hobby='JAVA' where name = '  ';
    
    sqlite> select * from child;
    19940801|  | |C++
    19940802|  | |JAVA

    データの削除
    delete formテーブル名;テーブル全体のデータを削除し、テーブルは削除されず、テーブルはデータベース内のdrop tableテーブル名に任せる.テーブル全体がデータベースからdelete fromテーブル名where条件を削除します.
    .table
    child
    
    sqlite> select * from child;
    19940801|  | |C++
    19940802|  | |JAVA
    
    sqlite> delect from child where hobby='JAVA';
    sqlite> select * from child;
    19940801|  | |C++
    .table
    child
    
    delete from child;
    sqlite> select * from child;
    sqlite>
    
    sqlite> .table
    child
    .table
    child
    
    sqlite> drop table child;
    sqlite> .table
    sqlite>

    クエリー作成テーブルコマンド
  • .schemaテーブル名
  • 注意:sqlite 3が提供するコマンドライン機能文、sql汎用文ではない
    sqlite> .schema child
    CREATE TABLE child(date char(16), name varchar(256), gender char(16), hobby text);

    フィールドの追加
  • alter tableテーブル名add columnフィールド名データ型[default対応値];
  • sqlite> select * from child;
    19940801|  | |C++
    19940802|  | |PHP
    
    sqlite> alter table child add column height int;
    sqlite> select * from child;
    19940801|  | |C++|
    19940802|  | |PHP|
    
    sqlite> update child set height=171;
    sqlite> select * from child;
    19940801|  | |C++|171
    19940802|  | |PHP|171
    
    sqlite> alter table child add column addr varchar(256) default '  ';
    sqlite> select * from child;
    19940801|  | |C++|171|  
    19940802|  | |PHP|171|  

    テーブル構造情報の問合せ
  • pragma table_info(テーブル名);
  • sqlite> pragma table_info(child);
    0|date|char(16)|0||0
    1|name|varchar(256)|0||0
    2|gender|char(16)|0||0
    3|hobby|text|0||0
    4|height|int|0||0
    5|addr|varchar(256)|0|'  '|0

    テーブルの作成時にフィールドコンストレイントを設定する
    ツールバーの
    説明
    integer promary key autoincrement
    プライマリ・キーとして自動的に増加
    not NULL
    NULLにはできません
    unique
    唯一、繰り返すことはできません
    default
    デフォルト
    例:
    id
    name
    status
    online
    1
    led1
    0
    0
    2
    led2
    0
    0
    3
    led3
    0
    0
    4
    led4
    0
    1
    create table device(id integer primary key autoincrement,   //    id    ,    
        name varchar(256) unique,                               //    name   
        status int not NULL default 0,                          //    status     ,     0
        online int not NULL);                                   //    online     

    例:
    sqlite> crate table device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL);
    
    sqlite> .table
    device
    
    sqlite> create table device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL);
    Error: table device already exists
  • if not existsはテーブルが存在するか否かを判断し、存在しない場合は
  • を作成する.
    sqlite> .table
    device
    
    sqlite> create table if not exists device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL);  //      !!
    sqlite> create table if not exists device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL);  //      !!
    
    sqlite> .table
    device
  • 挿入データ
  • プライマリ・キーの一意性;Uniqueプロパティの一意性
    sqlite> select * from device;
    
    sqlite> insert into device values(1, 'led1', 0, 0);
    sqlite> select * from device;
    1|led1|0|0
    
    sqlite> insert into device values(1, 'led1', 0, 0); //     !!
    Error: UNIQUE constraint failed: device.id
    
    sqlite> insert into device values(2, 'led1', 0, 0); //     !!
    Error: UNIQUE constraint failed: device.name
    
    sqlite> insert into device values(2, 'led2', 0, 0);
    sqlite> select * from device;
    1|led1|0|0
    2|led2|0|0
    sqlite>

    デフォルトの使用(指定フィールド(列)挿入、指定なしでデフォルトを使用)
    sqlite> select * from device;
    1|led1|0|0
    2|led2|0|0
    
    sqlite> insert into device (name, online) values('led3', 0);    //      !!
    sqlite> insert into device (name, online) values('led4', 1);    //      !!
    
    sqlite> select * from device;
    1|led1|0|0
    2|led2|0|0
    3|led3|0|0
    4|led4|0|1