LoginSignup
7

More than 5 years have passed since last update.

ターミナルでSQLiteを使う(1回目)

Last updated at Posted at 2017-10-12

今回はターミナルでSQLiteをかるーく使ってみる

データベースの作成

terminal
 sqlite>.open [ファイル名]

[ファイル名]の部分は任意のデータベースの名前を入力する。次回からはこのコマンドで作成済みのデータベースをオープンする

データベースが作られたか確認

terminal
sqlite>.databases
main: /Users/[ユーザー名]/[ファイル名]

テーブル作成

terminal
sqlite>create table [テーブル名]([カラム名] [データ型], [カラム名] [データ型]...);

終わりには必ず;(セミコロン)をつけること
(例)

terminal
sqlite>create table classroom(id integer, name text);

以下のようなテーブルがデータベースに作られる

id name

データ追加

terminal
sqlite>insert into [テーブル名] values(1, 2 ...);

(例)

terminal
sqlite>insert into classroom values(1, '太郎');
sqlite>insert into classroom values(2, '小太郎');
id name
1 太郎
2 小太郎

データ削除

terminal
sqlite>delete from [テーブル名] where (条件式)

(例)idが1のデータを削除

terminal
delete from classroom where id == 1;
id name
2 小太郎

テーブルの確認

terminal
sqlite>select [カラム名] from [テーブル名];

(例)classroomのデータ全てを抽出

terminal
sqlite>select * from classroom;

ぼちぼち長くなってきたのでこれで一旦終わり

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7