LoginSignup
6
4

More than 3 years have passed since last update.

10分くらいでできるSQLite入門

Last updated at Posted at 2020-10-20

対象

ProgateのSQLコース学んだくらいのレベル

目標

ローカルでもSQL叩ける環境を用意する。

本記事ではsampleという名前のデータベース, usersという名前のテーブルを作る

環境

Mac (であればデフォルトsqlite3のコマンドが使えるはず)

SQLite3の特徴

1つのファイルと1つのデータベースが1:1対応。
コマンドでしか操作できないエクセルみたいなもの(雑)

準備

作業用フォルダを作る
ここではsqlite-sampleというフォルダ名で作った

ファイルを開く (なければ更新時にファイルが作られる)

sqlite-sample $ sqlite3 sample.sqlite3
SQLite version 3.28.0 2019-04-15 14:49:49
Enter ".help" for usage hints.
sqlite>

sqlite3の拡張子はsqlite3, db, sqlite等複数あるがどれでもよい

sqlite3のファイルを開くと、

sqlite>

のようにsqlite用のコンソールになる。

終了コマンド 以下のコマンドを叩くと戻ってこれる

sqlite> .exit

テーブルを作る

テーブル一覧を表示
.tables

最初は何もないので何も表示されない

sqlite> .tables
sqlite>

テーブルを作成

sqlite> create table users (id int, name string);

すると、テーブル一覧でusersが出るようになる

sqlite> .tables
users

テーブルのスキーマの確認

sqlite> .schema users
CREATE TABLE users (id int, name string);

データの追加・取得

テーブルに値追加

insert into users (id, name) values (1, "sato");

テーブルの値の取得

sqlite> select * from users;
1|sato

まとめ

これで無限にSQLの練習ができるよ!やったね!

参考

SQLite3まとめ

6
4
0

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
6
4