I am trying to create a sealed command for my build pipeline which inserts data and quits.
So far I have created my data files
things-to-import-001.sql and 002 etc, which contains all the INSERT statements I'd like to run, with a file per table.
I have created a command file to run them
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.quit
However when I run my command
sqlite3 -init ./import-all.sql ./database.sqlite
..the data is inserted, but the program remains running and shows the sqlite> prompt, despite the .quit command. I have also tried using .exit 0.
From the sqlite3 --help
-init FILENAME read/process named file
Docs: https://www.sqlite.org/cli.html#reading_sql_from_a_file
How can I tell sqlite to exit once my inserts have finished?
I have managed to find a dirty workaround for this issue.
I have updated my import file to include a bad command, and executed using -bail to quit on first error.
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.fakeErrorToQuitWithBail
Then you can execute with
sqlite3 -init import-all.sql -bail
and it should quit with
Error: unknown command or invalid arguments: "fakeErrorToQuitWithBail". Enter ".help" for help
Try using ".exit" at the place of ".quit". For some reason SQLite dont doccumented this commands.
https://www.tutorialspoint.com/sqlite/sqlite_commands.htm
Related
I'm getting an error when running an SQLite script.
--drop use table before replacing it
DROP TABLE IF EXISTS db.use;
--Create the use table in the saved database
CREATE TABLE db.use AS SELECT * FROM use2; -- this is the line that generates the error: Error: near line 145: database is locked
Are these two statements run asynchronously or something? I don't understand what's causing the error, but I'm wondering if it has to do with that.
Might there be a way to run the script in a lock-step manner, i.e. non-asynchronously?
This is how I was running the command: sqlite3 --init script_name.sql dbname.db, and elsewhere in the script I had an ATTACH statement reading the same database dbname.db. Essentially reading the same file twice.
The way I solved this was by executing the script in the sqlite3 shell:
sqlite3> .read script_name.sql
Have you tried to add a commit statement after the drop statement?
I think that would make sure the create table statement run after the drop statement is totally done.
My goal is to automate a deployment of SQL scripts to Teradata via BTEQ. So far my script is working. However, I would like to generate a log file where possible failures are captured.
.LOGON tdserver/username,pw
.EXPORT file=\logfile.txt;
.run file = \Desktop\test\test.sql;
.LOGOFF
.EXIT
My SQL script will create a VIEW. When this view for example already exists I see an error in the BTEQ command window:
*** Failure 3804 View 'ViewName' already exists.
I would like to have this TD Message in my log file. I tried several tings, have been looking for 3 hours but unfortunately without success.
You may want to experiment using .SET ERROROUT STDERR which re-routes the error stream to the STDERR output file instead of the default action of routing the error stream to STDOUT.
There is more information in the BTEQ manual under Chapter 5 - BTEQ Commands.
Save all your script written above as a text file and create a batch file that generates a log after running the script:
echo off
bteq < script.txt > script.log 2>&1
#echo off goto end
:end
#echo exit
Errors will be recorded in this way.
I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.
But, when I call the script from my QT program
void MainWindow::on_importButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}
it compiles successfully but gives an error message in console when the button is pressed at runtime.
Error: near line 1: near "-": syntax error
Error: cannot open "ora_exported.csv"
what could be causing this ???
EDITED
I have changed my .sh script now to--
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.
Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.
echo is a built in command of a shell that may behave differently.
E.g. take this test script: echotest.sh
echo -e "123"
Now we can compare different results:
$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123
You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.
Additionally, you are messing up your quotations
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'
results in (no quotations in the first line)
attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported
but you pobably want
echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"
It looks strange that you are using external script to update database!
why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{
QProcess::startDetached("/bin/sh",
QStringList()<<"/home/aj/script.sh",
"<location of: 'ora_exported.csv' file>");
}
i using start terminal
-macbook:sqlTest user1$ sqlite3 sqlTest.sqlite
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> ATTACH DATABASE 'encrypted.sqlite' AS encrypted KEY 'testkey';
sqlite> SELECT sqlcipher_export('encrypted');
Error: no such function: sqlcipher_export
sqlite>
what makes no such function: sqlcipher_export?
As answered on the mailing list:
The first step is to build the sqlcipher command line tool, as described here:
http://sqlcipher.net/introduction/
Once you have done this, you should run the command like this:
$ ./sqlcipher sqlTest.sqlite
or
$ /full/path/to/sqlcipher/sqlcipher sqlTest.sqlite
On unix systems, if you don't provide an explicit path for a command, the system will look for the program in $PATH. On OSX, the system ships with a sqlite3 command, so you've probably been using that instead of the version compiled with SQLCipher. Please let us know if that resolves the problem. Thanks!
I want to know how to vacuum sqlite database.
I tried a syntax MANUAL VACUUM command for the whole database from command prompt:
$sqlite3 database_name "VACUUM;";
But it's giving error as:
near "database_name": syntax error.
and also AUTO VACUUM:
PRAGMA auto_vacuum = INCREMENTAL;
And tried it for a particular table as:
VACUUM table_name;
But no result.
You don't to specify the table name in the syntax. Only VACUUM works.
Also, it will clean the main database only and not any attached database files.
For more info, refer to the SQLite documentation.
Give the command like this:
$sqlite3 database_name 'VACUUM;'
As a matter of fact, this is the way to do also other queries from command line:
$sqlite3 database_name 'select * from tablename;'
You can use the full path to the db:
$sqlite3 /path/to/db/foo.db 'VACUUM;'
Run the command:
VACUUM;
if you use DB Browser for Sqlite or
open Sqlite from command prompt:
cd C:\your_folder
C:\Users\your_user\AppData\Local\Android\Sdk\platform-tools\sqlite3.exe -line your_db_name.db
and run
VACUUM;