Failing SQLite .import returns exit status 0

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Failing SQLite .import returns exit status 0

Faheem Mitha

Hi,

Consider the following shell script:

rm -f empty.db empty.csv
echo "" > empty.csv
echo "a, b" >> empty.csv
sqlite3 empty.db \
'DROP TABLE IF EXISTS empty;' \
'.mode csv' \
'CREATE TABLE IF NOT EXISTS empty(A, B NOT NULL);' \
'.import empty.csv empty' '.exit'
echo "error code is" $?

This returns:

sh sqlite_err.sh

empty.csv:1: expected 2 columns but found 1 - filling the rest with NULL
empty.csv:1: INSERT failed: NOT NULL constraint failed: empty.B
error code is 0

There is an error, but the exit status is 0. Isn't that wrong?

Regards, Faheem Mitha
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Failing SQLite .import returns exit status 0

Adrian Ho
On 8/6/19 10:49 AM, Faheem Mitha wrote:

> rm -f empty.db empty.csv
> echo "" > empty.csv
> echo "a, b" >> empty.csv
> sqlite3 empty.db \
> 'DROP TABLE IF EXISTS empty;' \
> '.mode csv' \
> 'CREATE TABLE IF NOT EXISTS empty(A, B NOT NULL);' \
> '.import empty.csv empty' '.exit'
> echo "error code is" $?
>
> This returns:
>
> sh sqlite_err.sh
>
> empty.csv:1: expected 2 columns but found 1 - filling the rest with NULL
> empty.csv:1: INSERT failed: NOT NULL constraint failed: empty.B
> error code is 0
>
Which version of the SQLite shell are you running? (Run "sqlite3 -version"
to see.) I'm guessing it's a pretty old one.


_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Failing SQLite .import returns exit status 0

Simon Slavin-3
In reply to this post by Faheem Mitha
Use

    .bail ON

If the shell bails out because of an error, it should be setting the exit code to indicate an error.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Failing SQLite .import returns exit status 0

Luuk

On 8-6-2019 15:26, Simon Slavin wrote:
> Use
>
>      .bail ON
>
> If the shell bails out because of an error, it should be setting the exit code to indicate an error.

unfortunatly still 0 is returned  (sqlite3 version 3.28.0)

~/temp> cat empty.sh
#!/bin/bash

rm -f empty.db empty.csv
echo "" > empty.csv
echo "a, b" >> empty.csv
sqlite3 empty.db \
'.bail on' \
'DROP TABLE IF EXISTS empty;' \
'.mode csv' \
'CREATE TABLE IF NOT EXISTS empty(A, B NOT NULL);' \
'.import empty.csv empty' \
'.exit'
echo "error code is" $?

~/temp> ./empty.sh
empty.csv:1: expected 2 columns but found 1 - filling the rest with NULL
empty.csv:1: INSERT failed: NOT NULL constraint failed: empty.B
error code is 0
~/temp>


_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Failing SQLite .import returns exit status 0

Adrian Ho
In reply to this post by Simon Slavin-3
On 8/6/19 9:26 PM, Simon Slavin wrote:
> Use
>
>     .bail ON
>
> If the shell bails out because of an error, it should be setting the exit code to indicate an error.

Except...it's not bailing on the failed import. Does the SQLite shell
consider failed dot-commands as errors?

$ cat empty.sh

#!/bin/sh

# Check SQLite version
sqlite3 -version

# Run supposedly-failing test
rm -f empty.db empty.csv
echo "" > empty.csv
echo "a, b" >> empty.csv
sqlite3 empty.db \
'.bail on' \
'DROP TABLE IF EXISTS empty;' \
'.mode csv' \
'CREATE TABLE IF NOT EXISTS empty(A, B NOT NULL);' \
'.import empty.csv empty' '.exit'
echo "error code is" $?

# Check contents of the generated files
echo "-----"
head empty.csv
echo "-----"
sqlite3 empty.db .dump

echo "-----"


$ ./empty.sh

3.28.0 2019-04-16 19:49:53
884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50
empty.csv:1: expected 2 columns but found 1 - filling the rest with NULL
empty.csv:1: INSERT failed: NOT NULL constraint failed: empty.B
error code is 0
-----

a, b
-----
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE empty(A, B NOT NULL);
INSERT INTO empty VALUES('a',' b');
COMMIT;
-----

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Failing SQLite .import returns exit status 0

Simon Slavin-3
On 8 Jun 2019, at 4:15pm, Adrian Ho <[hidden email]> wrote:

> Except...it's not bailing on the failed import. Does the SQLite shell
> consider failed dot-commands as errors?

That's not what I expected.  One for the developers.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Failing SQLite .import returns exit status 0

Gerry Snyder-4
In reply to this post by Faheem Mitha
A constraint doing its job is not an error.

On Fri, Jun 7, 2019, 7:49 PM Faheem Mitha <[hidden email]> wrote:

>
> Hi,
>
> Consider the following shell script:
>
> rm -f empty.db empty.csv
> echo "" > empty.csv
> echo "a, b" >> empty.csv
> sqlite3 empty.db \
> 'DROP TABLE IF EXISTS empty;' \
> '.mode csv' \
> 'CREATE TABLE IF NOT EXISTS empty(A, B NOT NULL);' \
> '.import empty.csv empty' '.exit'
> echo "error code is" $?
>
> This returns:
>
> sh sqlite_err.sh
>
> empty.csv:1: expected 2 columns but found 1 - filling the rest with NULL
> empty.csv:1: INSERT failed: NOT NULL constraint failed: empty.B
> error code is 0
>
> There is an error, but the exit status is 0. Isn't that wrong?
>
> Regards, Faheem Mitha
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users