How to use sqlite3.exe to create database from exported .sql script - sqlite

Trying to convert an SqlCe database to SQLite, I export it to a .sql file. Now how would I use sqlite.exe to create a database from this .sql file?
Where to put the sql3.exe file?
What command syntax to use, in cmd prompt or in the sqlite.exe shell?

Use following command line:
sqlite3 -init dump.sql newsqlite.db ""
It will create new SQLite database file newsqlite.db by executing statements from dump.sql. Empty string "" is needed for sqlite3 to quit automatically.
If newsqlite.db file already existed with some data, import may fail unless you use IF NOT EXISTS for all table and index creation statements.

Put sqlite3.exe wherever you want, as long as you remember that place and you're able to start sqlite3 from there.
Applying a script to a database (maybe a newly-created one), in command prompt:
sqlite3.exe my-new-db.somesuffix < myscript.sql
Executing a script within interactive sqlite3 session:
sqlite3.exe my-new-db.somesuffix
....
.read myscript.sql
....
Both variants are valid and usable at times. (Note: if your .sql was generated for non-sqlite database, I'd expect that it will require some changes to work in sqlite3. And things like stored procedures and user-defined functions will be definitely lost).

Related

SQLite - Importing .csv data to database every day

So I need to import some .csv file every day in our firm. My idea was to write a .bat file and run it everyday automatically through Task Scheduler. The problem is that i can't succeed at writting .bat file correctly. It's working when I type physically in CMD (sqlite3) but don't do anything when I transform it to .bat file and run it.
CMD:
C:\WINDOWS\system32>sqlite3.exe
SQLite version 3.34.0 2020-12-01 16:14:00
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> ATTACH DATABASE "C:\...\database.db" AS "name";
sqlite> .mode csv
sqlite> .separator ;
sqlite> .headers on
sqlite> .import "\\\\path\\..." "dbname"
sqlite> SELECT * from "dbname"
The result is the way I want, so all the data is in the database correctly.
Do anyone has an idea how to transform this to a .bat file?
Create a text file for the sqlite script
The batch file will be something like sqlite3 ":memory:" ".read sqlite.script"
Et voilĂ ! The first argument to sqlite3 is the database name, the following arguments are CLI "executables".
We found a solution that works! I created a .bat file with the next code:
#ECHO off
sqlite3.exe -cmd ".mode csv" -cmd ".separator ";"" -cmd ".import C:\\the path to the .csv file\\ myTable" C:\the path to the database
pause
and it works with the task scheduler.
Thanks everyone!

Run sqlite3 command inline

This may sound odd but I have 15 databases within different folders and I wish to update them all.
I have made a text file for me to easily copy the sql file that I need to import in to each root folder and load the DB, so I can just copy and paste each to CLI and then I can then type the following in to each one.
sqlite> .read backup.sql
and it imports the data I need it too...I then exit (CTRL + Z) and run the next one...
However, can I easily run something like
sqlite3 database.db > .read backup.sql
So I can effectively run the whole thing from one command line?

SQLite3 one-line command for exporting table to the .sql file

In my rails application, I need to export an existing table as .sql file with one-line command ideally
Does anyone have an idea how to do it?
This dumps hole database
db_file = 'db/development.sqlite3'
sh("sqlite3 #{db_file} saraksts > from_file/test.sql")
sqlite3 database.db ".dump table_name"
should do it.

How do I send results of sql file to txt including queries run and comments in sqlite3?

I understand I can save my queries etc in an .sql file and run it against a database from the cmd prompt like this:
$ sqlite3 main.db < queries.sql > results.txt
but how can I write my .sql file to print comments or the actual statements themselves so that the results file is self explanatory when viewed?

Sqlite3: Location of the data base file in the file system

I am starting to get familiar with the sqlite. I am currently using SQLite version 3.13.0 2016-05-18.
I created a test data base called "test.db" as follows:
sqlite3 test.db
And then, I created a table called "employees" in the database as follow:
create table employees(id integer primary key, name text);
Then after I added the following entry to the table:
insert into employees(id, name) values(410,'test');
Everything works as expected. But, somehow I can not find where my test database file is located in the file system. Any idea where that .db file is located? I am in windows system.
I just realized that I had to save the .db file as follows:
.save test.db
And, then the .db file appeared in the same directory as SQLite database file is also a regular file.

Resources