run and execute text file using sqlite 3 command line - sqlite

I have a text file which contains an sqlite statment where I want to dump it into .sql format but not able to excute it. Any help would be appreciated. My file is stored on desktop as verbali.txt and when i go to command prompt and to specific sqlite folder and run it just open the text file .
.mode insert
.header on
.out file.sql
select select Id ,CompanyId ,DateTime ,Serial ,DeviceId ,AgentAId ,GpsAddress ,Targa ,CommonRoadDescription ,RoadCivicNumber ,VehicleBrandDescription ,VehicleModelDescription ,VerbaliVehicleTypeDescription ,CommonColorVehicleDescription ,VerbaliRuleOneCode ,VerbaliRuleOneDescription ,VerbaliRuleOnePoints ,VerbaliClosedNoteDescription ,Points ,VerbaliMissedNotificationDescription ,MissedNotificationNote ,StatementNote from VerbaliData

Pipe the file to sqlite3, in command prompt:
sqlite3 yourdatabase.db <path\to\verbali.txt
where sqlite3 is the sqlite3.exe command line tool.

Related

How to use SQLite db file location in select?

I would like to use the main database file location in function (replace or other) as input data. How can i get it?
for example: replace('c:\temp\main.db','main.db','')
I'm creating a database script for command line use under win10.
.database command result seems to be fine, but how to use in replace input?
EDIT: I have to use the location as variable, because i run the script from command line, recursively in a lot of directories with a lot of small databases, so i can't use constant value in replace.
The cmd line command:
forfiles /p c:\temp /m files.db /s /c "cmd /c c:\sqlite3.exe #path < c:\BookDbDataCopy.txt"
I want to use the path in the select as variable.
I got it :)
create table path (a text);
.mode csv
.output 'c:\data.csv'
.databases
.import 'c:\data.csv' path
The solution:
- create a table for the path
- write the command output in file
- import the file in table
- select the table in replace variable :)
select replace((select a from path where a like 'main%'),'main: c:\',''), t.* from path;

SQLite importing csv file

I am using SQLite and I can't import this csv file for some reason. I ran cmd first then entered SQLite.
my input in cmd:
cd C:\Users\Fries\Desktop\SQLite
sqlite3
.mode csv
.import C:\Users\Fries\Desktop\city.csv cities
.schema cities
I am using this tutorial. http://www.sqlitetutorial.net/sqlite-import-csv/
for some reason I can't open the csv
I am new to dbms and sqlite seems to be a super simple one
Going by the feedback in the error message, it looks like the backslashes need to be escaped. I would first try this:
sqlite> .import C:/Users/Fries/Desktop/city.csv
The following might also work, using escaped backslashes:
sqlite> .import C:\\Users\\Fries\\Desktop\\city.csv

How to save SQLite settings such as mode, headers and width so they persist

Everytime I start SQLite, I have to re-turn on headers, re-switch to column mode, re-change the separator and/or width. How can I make the settings persist??
Repeated Code
.mode column
.headers on
.separator ','
In other words, how can I save the settings so that next time I run SQLite, my preferences are automatically applied.
Create a .sqliterc file in your home directory
Ah, finally stumbled upon the answer. You can create a .sqliterc (SQLite Run Commands file) in your home directory. Every time you run sqlite, it will load the settings from the rc file.
Here's a quick command line script you can paste in the terminal for some basic settings (edit it to add additional preferences):
cat << EOF > ~/.sqliterc
.headers on
.mode column
EOF
There are alternative command line options. Something like this:
alias mysqlite='sqlite3 -column -header -separator ,'

SQLite .output stdout

I'm trying to export a csv file to a fixed length file.
I'm using SQLite because I have to process the file.
The problem I'm having is with the output.
Here is the code I am running from a sql file:
.output stdout --send output to screen
.echo off
.headers off
.separator ','
.mode column
.open tmp.db
DROP TABLE IF EXISTS myData;
.import 'C:\2015.csv' myData
.width 4 4 1 6 16 2 6
.output myData.txt --send output to file
select * from myData;
.output stdout --send output back to screen
.print "File was created."
DROP TABLE IF EXISTS myData;
vacuum;
.print "tmp.db vacuum complete"
.print "Type '.exit' or '.quit' to exit SQLite"
.exit
The problem is that in the first line, where I send the output to the screen, I get an error:
Error: unknown command or invalid arguments: "output". Enter ".help"
for help
The script executes, but it actually displays the results of the sql (and it also writes them to the file).
However, if I run every single command from the sqlite console, everything works fine. I only get the error when I run it from the SQLite console using .read
Any suggestions? What am I missing? I checked the docs in the SQLite website, and in the example presented they switch the output from console to file and back in the same manner.
Thank you.
I think I found the problem:
.output stdout --send output to screen
Apparently, the --send... was interpreted as an argument. Removing the -- and the comments after stdout fixed the problem.
Thanks!
The .output command expects a single parameter, not five.
This forms works fine:
.output stdout

Is it possible to append the current date to filename is sqlite?

I want the filename to be something like this top_queries_June.csv. Is it possible to do that in sqlite?
sqlite>.output (top_Queries + Select date('now')).csv
I run this batch file to create a file with the top queries.
Batch file:
sqlite3 mydb.db ".read x.sql"
x.sql:
.headers ON
.mode csv
.output Top_Queries + <current month>.csv
select * from query;
.quit
As #CL. already pointed out the sqlite3 command line utility is not flexible enough to do this. You appear to be using some kind of shell to execute sqlite3. Most shells support a feature called "here document" and the sqlite3 utility supports reading commands from a pipe. Using these you can inline the x.sql file into your shell script. In a POSIX compatible shell your code could look like this:
DATE=`date -I`
sqlite3 mydb.db <<EOF
.headers ON
.mode csv
.output Top_Queries${DATE}.csv
select * from query;
EOF

Resources