How do I import .sql files into SQLite 3?

SqlSqlite

Sql Problem Overview


I have .sql files which have the following content:

#cat db.sql
create table server(name varchar(50),ipaddress varchar(15),id init)
create table client(name varchar(50),ipaddress varchar(15),id init)

How do I import this file into SQLite so that these are created automatically?

Sql Solutions


Solution 1 - Sql

From a sqlite prompt:

sqlite> .read db.sql

Or:

cat db.sql | sqlite3 database.db

Also, your SQL is invalid - you need ; on the end of your statements:

create table server(name varchar(50),ipaddress varchar(15),id init);
create table client(name varchar(50),ipaddress varchar(15),id init);

Solution 2 - Sql

Use sqlite3 database.sqlite3 < db.sql. You'll need to make sure that your files contain valid SQL for SQLite.

Solution 3 - Sql

Alternatively, you can do this from a Windows commandline prompt/batch file:

sqlite3.exe DB.db ".read db.sql"

Where DB.db is the database file, and db.sql is the SQL file to run/import.

Solution 4 - Sql

You can also do:

sqlite3 database.db -init dump.sql

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwebminal.orgView Question on Stackoverflow
Solution 1 - SqlDominic RodgerView Answer on Stackoverflow
Solution 2 - SqlEifionView Answer on Stackoverflow
Solution 3 - SqleurekaView Answer on Stackoverflow
Solution 4 - SqlIan NewlandView Answer on Stackoverflow