Running a Sqlite3 Script from Command Line
The parameter you give to the sqlite3
program is the database file name.
To execute commands from a file, you must redirect the input to that file:
$ sqlite3 mydatabase.db < SQLTableTransfer
or tell it to read from that file:
$ sqlite3 mydatabase.db ".read SQLTableTransfer"
Execute SQLite script
There are many ways to do this, one way is:
sqlite3 auction.db
Followed by:
sqlite> .read create.sql
In general, the SQLite project has really fantastic documentation! I know we often reach for Google before the docs, but in SQLite's case, the docs really are technical writing at its best. It's clean, clear, and concise.
May I run a script on SQLite command line?
Mixing SQL statements with SQLite .commands
can be a bit tricky:
$ sqlite3 test.db 'create table X(x integer); .dump'
Error: near ".": syntax error
The easiest and most universal way to deal with this is probably to provide all commands to the standard input of the SQLite3 command line shell, separated by newlines as you would type them:
$ sqlite3 test.db <<EOF
> CREATE TABLE TEST(X INTEGER);
> .dump
> EOF
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE TEST(X INTEGER);
COMMIT;
Since here documents are not available in all shells, you could use an intermediate file to bypass this limitation:
$ cat test.sql
CREATE TABLE TEST(X INTEGER);
.dump
$ sqlite3 test.db <test.sql
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE TEST(X INTEGER);
COMMIT;
Run command-line SQLite query and exit
Just include the command in quotes after the database file argument.
For example, the following creates a table called abc
:
sqlite3 test.db 'create table abc (col0 int)'
execute Sqlite3 command line tool from code
As CaiusJard said in the comments, an error was passed in the error stream. Adding the following lines told me that my path was wrong.
pProcess.StartInfo.RedirectStandardError = true;
string error = pProcess.StandardError.ReadToEnd(); //The output result
The path divider "\" was removed since the path was parsed twice. Once setting the argument, and once when it was read by the tool. Replacing "\" with "/" in my paths did the trick
sqlite script is not executing from the command line
Your SQL is correct, I've stored your script into script.sql
and run:
sqlite3.exe my.db
sqlite> .read script.sql
sqlite> .exit
Db with required structure properly created. SQLite version 3.14.2
Typically Error: incomplete SQL:
shows you statement with error. ??P
means something wrong at the beginning of your file. Open it in Notepad++, View -> Show Symbol -> Show All Characters
and check what is wrong there.
Non-interactive SQLite3 usage from bash script
Looks like it's as simple as
#!/bin/bash
sqlite3 test.db "create table n (id INTEGER PRIMARY KEY,f TEXT,l TEXT);"
sqlite3 test.db "insert into n (f,l) values ('john','smith');"
sqlite3 test.db "select * from n;"
from https://mailliststock.wordpress.com/2007/03/01/sqlite-examples-with-bash-perl-and-python/
Related Topics
How to Use SQL Server Stored Procedures in Microsoft Powerbi
Help Me Put Oracle Terminology into SQL Server Terminology
Update Statement with Multiple Where Conditions
SQL Loop Through Each Row in a Table
Writing Data Back to SQL from Excel Sheet
Replacing Only The Nth Instance of Character
When Should I Nest Pl/SQL Begin...End Blocks
Extract Time Part from Timestamp Column in Oracle
Oracle SQL Date Range Intersections
Iterate Through a List of Strings in SQL Server
Sql Server Queries Case Sensitivity
How to Best Handle the Storage of Historical Data
Get Total Row Count While Paging