最終更新: cielo_cielo 2015年08月29日(土) 15:40:35履歴
sqlite3の勉強をする。
http://www.dbonline.jp/sqlite/type/index1.html
そのカラムの値を省略すると、自動的に数値が格納される。
例
例
- 起動
$ sqlite3 SQLite version 3.7.12.1 2012-05-22 02:45:53 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite>
- 終了
sqlite> .quit
- データベースの作成or既存のデータベースへの接続
$ sqlite3 test.tb SQLite version 3.7.12.1 2012-05-22 02:45:53 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite>
- テーブルの作成
sqlite> create table personal(id,name);
- テーブルの確認
sqlite> .table personal
- スキーマの確認
sqlite> .schema CREATE TABLE personal(id,name);又は
> select * from sqlite_master; table|personal|personal|2|CREATE TABLE personal(id,name)
- 接続しているデータベースの確認
sqlite> .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /cygdrive/d/hoge/hoge/test.tb
- テーブルの作成
sqlite> create table personal(id,name);
- データ型を指定してテーブルを作成
sqlite> create table personal2(id integer, name text);指定可能なデータ型は以下を参照
http://www.dbonline.jp/sqlite/type/index1.html
- テーブル名の変更
sqlite> .tables ppersonal personal2 sqlite> alter table personal2 rename to hoge; sqlite> .tables hoge personal
- カラムの追加
sqlite> .schema CREATE TABLE "hoge"(id integer, name text); sqlite> alter table hoge add column address; sqlite> .schema CREATE TABLE "hoge"(id integer, name text, address);
- テーブルの削除
sqlite> .schema CREATE TABLE "hoge"(id integer, name text, address); CREATE TABLE personal(id,name); sqlite> drop table hoge; sqlite> .schema CREATE TABLE personal(id,name);
- テーブル削除後に不要な空き空間を削除
sqlite> vacuum;
- primary key制約をつける
sqlite> create table product(id integer primary key,name text); sqlite> .schema CREATE TABLE product(id integer primary key,name text);複数のキーを指定する場合
sqlite> create table friend(name text,old int,address text,primary key(name,old)); sqlite> .schema CREATE TABLE friend(name text,old int,address text,primary key(name,old));integer型のカラムに対してprimary key制約を設定すると、新しいデータ型を追加するときに
そのカラムの値を省略すると、自動的に数値が格納される。
sqlite> create table address(id integer primary key,name text); sqlite> insert into address(name) values('Sachiko'); sqlite> insert into address(name) values('Koume'); sqlite> insert into address(name) values('Shoko'); sqlite> select * from address; 1|Sachiko 2|Koume 3|Shoko
- ROWID
sqlite> select *, ROWID from address; 1|Sachiko|1 2|Koume|2 3|Shoko|3where句の中で使用することもできる。
sqlite> select *,ROWID from address where ROWID = 2; 2|Koume|2
- データの追加
INSERT INTO テーブル名 VALUES(値1, 値2, ...);値1,2…はカラムの順番の通りにする
例
sqlite> .schema CREATE TABLE friend(name text,old int,address text,primary key(name,old)); sqlite> insert into friend values('Amami_Haruk',16,'Ninomiya'); sqlite> select * from friend; Amami_Haruk|16|Ninomiya主キーが重複しているデータは追加できない
sqlite> select * from friend; Amami_Haruk|16|Ninomiya sqlite> insert into friend values('Amami_Haruk',16,'Tokyo'); Error: columns name, old are not unique =|| *データの取得 -データの取得 =|BOX| SELECT カラム名1, カラム名2, ... FROM テーブル名;カラム名を*にすると全部のカラムを取得できる。
例
sqlite> .schema CREATE TABLE friend(name text,old int,address text,primary key(name,old)); sqlite> select * from friend; Amami_Haruk|16|Ninomiya Hagiwara_Yukiho|16|Tokyo Hosii_Miki|14|Kanagawa sqlite> select name,old from friend; Amami_Haruk|16 Hagiwara_Yukiho|16 Hosii_Miki|14
- データの更新
UPDATE テーブル名 SET カラム名1 = 値1, カラム名2 = 値2, ... WHERE 条件式;例
sqlite> select * from idols; 1|Amami_Haruka|16 2|Kisaragi_Chihaya|15 3|Hagiwara_Yukiho|16 4|Takatsuki_Yayoi|13 5|Akiduki_Ritsuko|18 6|Minase_Iori|14 7|Miura_Azusa|20 8|Futami_Ami|12 9|Futami_Mami|12 10|Kikuchi_Makoto|16 11|Hosii_Miki|14 12|Ganaha_Hibiki|15 13|Shijo_Takane|17 sqlite> update idols set old = 21 where id = 7; sqlite> select * from idols where id = 7; 7|Miura_Azusa|21
タグ
コメントをかく