J.doe
J.doe

Reputation: 47

How do I execute sqlite3 file into a database in mac terminal?

I've tried the other suggestions on stackoverflow but can't seem to get it right. Nothing really works?

So say I have an sql file called afile.sql which contains sql commands for creating tables as well as inserting information into those tables, how do I run all these statements so that tables are created in a database I made in the terminal?

I have tried [someone@somewhere ~]$ echo "select * from sometable;" > file.sql [someone@somewhere ~]$ sqlite3 file.db < file.sql

from this link How to execute an sql script file against an SQLite 3 database file?

and a few others. Should I be running the "insert sql command file into database" outside of sqlite3 or in sqlite 3?

I have tried: DATA.db < afile.sql(in sqlite3) sqlite3 DATA.db < afile.sql (not in sqlite3)

and also .read afile.sql DATA.db

Nothing seems to work.

Screenshots:

try 1

try 2

still no database with instances stored in it =(

Still no database with instances stored in it =(

Upvotes: 1

Views: 2592

Answers (1)

CL.
CL.

Reputation: 180010

The command

sqlite3 data.db < c291.sql

must be run from the OS shell, i.e., from the $ prompt.

The commands

.open data.db
.read c291.sql

must be run from within the SQLite shell, i.e., from the sqlite> prompt.

Upvotes: 1

Related Questions