Opened 8 years ago
Last modified 6 years ago
#3397 new defect
db.select - reading multiline SQL statements from file fails
Reported by: | maitl | Owned by: | |
---|---|---|---|
Priority: | normal | Milestone: | 7.4.5 |
Component: | Database | Version: | 7.2.1 |
Keywords: | db.select, db.execute | Cc: | |
CPU: | Unspecified | Platform: | All |
Description ¶
It is convenient to store longer SQL statements into file.
If each statement is not exactly one line in input file, then db.select ends with error.
Change History (10)
by , 8 years ago
Attachment: | query_examples_single_line_and_multi-line.zip added |
---|
comment:1 by , 8 years ago
Keywords: | db.select db.execute added |
---|---|
Milestone: | → 7.2.2 |
follow-up: 3 comment:2 by , 8 years ago
Milestone: | 7.2.2 → 7.4.0 |
---|
follow-up: 4 comment:3 by , 8 years ago
Replying to mlennert:
and I cannot integrate a newline ('\n') character:
echo "SELECT count(*) FROM boundary_county WHERE Z_MEAN > 38\n; SELECT count(*) FROM boundary_county;" | db.select input=- DBMI-SQLite erreur de pilote : Error in sqlite3_prepare(): SELECT count(*) FROM boundary_county WHERE Z_MEAN > 38\n; SELECT count(*) FROM boundary_county; unrecognized token: "\"
Sorry, the '\n' was not well placed. It should have been
echo "SELECT count(*) FROM boundary_county WHERE Z_MEAN > 38;\n SELECT count(*) FROM boundary_county;" | db.select input=- database=GRASS/GRASSDATA/NC_spm_temporal_workshop/PERMANENT/sqlite/sqlite.db count(*) 333
So, no error, but the query on the second line does not get executed...
comment:4 by , 7 years ago
Replying to mlennert:
Replying to mlennert:
and I cannot integrate a newline ('\n') character:
echo "SELECT count(*) FROM boundary_county WHERE Z_MEAN > 38\n; SELECT count(*) FROM boundary_county;" | db.select input=- DBMI-SQLite erreur de pilote : Error in sqlite3_prepare(): SELECT count(*) FROM boundary_county WHERE Z_MEAN > 38\n; SELECT count(*) FROM boundary_county; unrecognized token: "\"Sorry, the '\n' was not well placed. It should have been
echo "SELECT count(*) FROM boundary_county WHERE Z_MEAN > 38;\n SELECT count(*) FROM boundary_county;" | db.select input=- database=GRASS/GRASSDATA/NC_spm_temporal_workshop/PERMANENT/sqlite/sqlite.db count(*) 333So, no error, but the query on the second line does not get executed...
You need to use echo -e (enable interpretation of backslash escapes).
About the main issue, we could introduce a new flag, e.g. -m, to allow multiline statements: only a semicolon, not a newline would mark the end of a statement.
comment:6 by , 7 years ago
Milestone: | 7.4.1 → 7.4.2 |
---|
Replying to maitl:
This is due to the fact that db.select reads the input file line by line, and then uses all text before a semi-colon as the SQL statement.
This means that with a file test.sql that contains
you get
while if the file contains
you only get
The same happens when you do this:
and I cannot integrate a newline ('\n') character:
A rewrite of this to more flexibly parse the SQL, be it with one statement over several lines, or several statements on one line would be nice, but I would consider this too invasive to go into 7.2.2.
So, I propose that for 7.2.2 we just amend the manual. I've taken the liberty to add one sentence about that in trunk and release72 (r71389 and r71390).