Ex1Syntax error

Hi All,

I am trying to run this in Terminal but it keeps giving me syntax error when I type .schema. I even copied the code from the book and see if I type anything wrong but still syntax error. And I am really confused about the command line part for SQL. It was easy to understand when I did it with learning python the hard way. Maybe I need more practice?

This is the code in Atom:
CREATE TABLE person (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER
);

CREATE TABLE pet (
id INTEGER PRIMARY KEY,
name TEXT,
breed TEXT,
age INTEGER,
dead INTEGER
);

CREATE TABLE person_pet (
person_id INTEGER,
pet_id INTEGER
);

Below is what I typed in Terminal

$ cd lsqlthw

$ sqlite3 ex1.db <ex1.sql

$ sqlite3 ex1.sql

SQLite version 3.19.3 2017-06-27 16:48:08

Enter “.help” for usage hints.

sqlite> .schema

Error: near “)”: syntax error

sqlite> .schema

Error: near “)”: syntax error

Hi @Parixiat I don’t think there is anything wrong with the SQL table creations listed above as you successful create the db from them.

You must have a syntax error in your queries in ex1.sql ‘somewhere near’ parenthesis (as the error is telling you). Check the line before or after any such parenthesis and ensure you always have opening and closing bracket pairs.

As for command line usage, its not vastly different than what you did in Python. You are calling a program and specifying a file to run, so in this example; run file ex1 (which is of type sql) and use sqlite to execute it.

Or remember when you opened python’s interactive prompt, it’s the same with sqlite. You can open it to play around with an interactive prompt (“sqlite>”). Here you are using the sqlite software and asking it to show you the .schema from ex1.sql.

And… I think that’s your error. You probably want to open the db file, not the sql file! The sql file specifies the database that you then build with:

$ sqlite3 ex1.db <ex1.sql

So you might want to try:

sqlite3 ex1.db

Then use the interactive shell:

sqlite> .schema
1 Like

It worked now. Thank you very much :slightly_smiling_face::slightly_smiling_face:

1 Like

Try doing just one of the CREATE statements at a time until you narrow down which one, then look closely at how it comparse to mine.

A free service run by Zed A. Shaw for learncodethehardway.org.