简体   繁体   中英

sqlite3 echo and '<' piping command not working as expected

I am reading a SQL book and the author is using Sqlite3, which is awesome because there is not a server to mess with.

In the book the author says to type:

sqlite3 -echo something.db < some.sql

The problem is nothing ever echos out to the terminal nor is there even a database created from the '<' redirection command.

Does anyone know what is going on...with this?

something.db is an existing database. < some.sql means it picks data from that file and write it in the sqlite console. So both files have to exist.

something.db must be a valid l sqlite3 database file (or an empty or not existing file); some.sql must be a text file with your commands in it.

The -echo params specify that it have to print the command before execution.

Actually, the command you show IS a proper way of creating a new database from sql dump.

Can you please show your sql file contents ( cat some.sql )? The only way I can reproduce the behavior described is by feeding sqlite an empty sql file.

Try this commands and see if you can get the same result:

$ cat <<EOF > test.sql
> create table test1 (f1, f2, f3);
> insert into test1(f1, f2, f3) values ("foo", "bar", "baz");
> EOF
$ sqlite3 -echo test.db < test.sql
create table test1 (f1, f2, f3);
insert into test1(f1, f2, f3) values ("foo", "bar", "baz");
$ file test.db
test.db: SQLite 3.x database

According to your comments and your terminal screen shot, I get the impression that your "some.sql" file may be in the wrong encoding and/or starts with a BOM which confuses sqlite.

You can use the file command to find out, and if this is the problem use iconv , recode or your favorite text editor to convert the file into the encoding your terminal expects, and the correct line endings ( \\n aka "LF" in Linux, \\r\\n aka "CRLF" in Windows).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM