Gibletto
Gibletto

Reputation: 21

Copying tables from a .sql to a .db file.. Learn SQL The Hard Way

I'm pretty sure there is a simple answer to this but I can't find it anywhere and can't seem to figure it out for myself.. Any help would be greatly appreciated.

I'm trying to copy a table from a .sql file (ex1.sql) that has a basic table in it. I can create this no problem but then I'm running the following to copy this table into a database (ex1.db):

sqlite3 ex1.db < ex1.sql

I'm getting the following response

C:\SQLite>sqlite3 ex1.db < ex1.sql onperson CREATE TABLE person (format 3) NB: smileyface symbol after "onperson"
first_name text,
last_name text,
age integer
)

It looks like the table is being replicated in the database or am I getting this wrong?

Anyway, when I then go back into the ex1.db there is no table there.

Any idea as to why this isn't saving? Do I need to add further commands to this to get it to save in the db file?

Apologies if this is a stupid question. Rather new to this.

Thanks!

Upvotes: 2

Views: 459

Answers (1)

Daiwik Daarun
Daiwik Daarun

Reputation: 3974

I ran into this exact problem, and I have never touched SQLite before. Here are more instructions for people in the future.

In a text editor, create ex1.sql. This is the file that will be used to create the .db when the command sqlite3 ex1.db < ex1.sql is run.

ex1.sql

CREATE TABLE person (
    id INTEGER PRIMARY KEY,
    first_name TEXT,
    last_name TEXT,
    age INTEGER
);

Make sure to save this file in the same folder as the sqlite3.exe file you downloaded. You can now run the aforementioned command and you are good to go!

Upvotes: 2

Related Questions