迷ったらコレ!SQLiteデータベースに.sql/.csvファイルをインポートするベストプラクティス
SQLiteに.sqlまたは.csvファイルをインポートする方法
コマンドラインツールを使用する
SQLiteには、sqlite3
というコマンドラインツールが付属しています。このツールを使用して、.sqlまたは.csvファイルをデータベースにインポートできます。
例:
.sqlファイルのインポート
sqlite3 database.db < file.sql
sqlite3 database.db ".import file.csv table"
オプション:
-separator
オプションを使用して、CSVファイルの区切り文字を指定できます。-header
オプションを使用して、CSVファイルの最初の行をヘッダー行として指定できます。
GUIツールを使用する
DB Browser for SQLite: https://sqlitebrowser.org/ は、SQLiteデータベースを操作するための無料のGUIツールです。
- ツールを起動し、データベースファイルを開きます。
- "インポート" タブをクリックします。
- インポートするファイルの種類を選択します(.sqlまたは.csv)。
- ファイルを選択して、"インポート" ボタンをクリックします。
- "ファイル" メニューから "インポート" を選択します。
Pythonを使用して、.sqlまたは.csvファイルをSQLiteデータベースにインポートすることもできます。
import sqlite3
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
with open('file.sql', 'r') as f:
sql = f.read()
cursor.executescript(sql)
connection.commit()
connection.close()
import sqlite3
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
with open('file.csv', 'r') as f:
reader = csv.reader(f)
next(reader, None) # ヘッダー行をスキップ
for row in reader:
cursor.execute("INSERT INTO table (column1, column2) VALUES (?, ?)", row)
connection.commit()
connection.close()
その他の方法
上記以外にも、.sqlまたは.csvファイルをSQLiteデータベースにインポートする方法はいくつかあります。詳細は、SQLiteのドキュメントを参照してください。
コマンドラインツールを使用する
sqlite3 database.db < file.sql
sqlite3 customers.db < customers.sql
このコマンドは、customers.sql
ファイルの内容をcustomers.db
データベースにインポートします。
sqlite3 database.db ".import file.csv table"
sqlite3 orders.db ".import orders.csv orders"
このコマンドは、orders.csv
ファイルの内容をorders.db
データベースのorders
テーブルにインポートします。
sqlite3 products.db ".import products.csv products -separator ','"
sqlite3 customers.db ".import customers.csv customers -header"
このコマンドは、customers.csv
ファイルの内容をcustomers.db
データベースのcustomers
テーブルにインポートします。CSVファイルの最初の行はヘッダー行としてスキップされます。
GUIツールを使用する
DB Browser for SQLite
- DB Browser for SQLiteを起動し、
customers.db
データベースを開きます。
SQLiteStudio
- SQLiteStudioを起動し、
orders.db
データベースを開きます。
Pythonを使用する
import sqlite3
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
with open('file.sql', 'r') as f:
sql = f.read()
cursor.executescript(sql)
connection.commit()
connection.close()
import sqlite3
connection = sqlite3.connect('customers.db')
cursor = connection.cursor()
with open('customers.sql', 'r') as f:
sql = f.read()
cursor.executescript(sql)
connection.commit()
connection.close()
import sqlite3
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
with open('file.csv', 'r') as f:
reader = csv.reader(f)
next(reader, None) # ヘッダー行をスキップ
for row in reader:
cursor.execute("INSERT INTO table (column1, column2) VALUES (?, ?)", row)
connection.commit()
connection.close()
import sqlite3
connection = sqlite3.connect('orders.db')
cursor = connection.cursor()
with open('orders.csv', 'r') as f:
reader = csv.reader(f)
next(reader, None) # ヘッダー行をスキップ
for row in reader:
cursor.execute("INSERT INTO orders (product_id, quantity) VALUES (?, ?)", row)
connection.commit()
connection.close()
SQLiteに.sqlまたは.csvファイルをインポートする他の方法
これらのツールを使用するには、以下の手順に従います。
- ツールをWebブラウザで開きます。
スクリプトを使用する
Python
import sqlite3
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
with open('file.csv', 'r') as f:
reader = csv.reader(f)
next(reader, None) # ヘッダー行をスキップ
for row in reader:
cursor.execute("INSERT INTO table (column1, column2) VALUES (?, ?)", row)
connection.commit()
connection.close()
JavaScript
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('database.db');
const csv = require('csv-parser');
const parser = csv();
parser.on('data', (row) => {
db.run(`INSERT INTO table (column1, column2) VALUES (?, ?)`, [row.column1, row.column2]);
});
parser.on('end', () => {
db.close();
});
fs.createReadStream('file.csv').pipe(parser);
これらのスクリプトは、.csvファイルをdatabase.db
データベースのtable
テーブルにインポートします。
データベース管理ツールを使用する
MySQL Workbench
- データベース接続を作成します。
database sqlite import