New to sqlite3, how do I execute .sql code on .db3 files? - sqlite

Very basic question, having a hard time finding an explanation online.
I have a file code.sql that can be run on two different databases, a.db3 and b.db3. I used sqlite a.db3 to open the database in sqlite3. How do I run code.sql on it?

Use the .read code.sql command, or call sqlite3 with the file as input: sqlite3 a.db3 < code.sql.

I am guessing that you are trying to use the sqlite3 command line tool that you can dowload from the sqlite.org website.
I recomend that you use, instead, sqlitestudio http://sqlitestudio.one.pl
This has a feature to execute SQL from a file on a database:

Use DB Browser for SQLite is a high quality, visual, open source tool to create, design, and edit database files compatible with SQLite.
You can download the DB Browser at SQLite https://sqlitebrowser.org/

Related

SQLITE file is encrypted or is not a database

I have a huge problem... I am developing desktop app with SQLite but during copy/paste process I lost a power and process was terminated so base was lost. However, I found a way to recover it but base is encrypted. When I try to open connection using conn.Open(); I get this error. If I try to open it with DB Browser for SQLite it asks me a SQLCipher encryption password so it seams to me that data is lost..
Is there any default password ?
Why did this happen and how to prevent it from happening again ?
What can I do ?
Thanks in advance.
Also check that SQLite version you're "connecting" with aligns with the DB file version.
For example, here's a DB file written by SQLite version 3+:
$ file foobar.db
foobar.db: SQLite 3.x database, last written using SQLite version 3027002
And here I also have 2 versions of sqlite:
$ sqlite -version
2.8.17
$ sqlite3 -version
3.27.2 2019-02-25 16:06:06 bd49a8271d650fa89e446b42e513b595a717b9212c91dd384aab871fc1d0alt1
Obviously in hindsight, opening foobar.db with sqlite version 2 will fail, yielding the same error message:
$ sqlite foobar.db
Unable to open database "foobar.db": file is encrypted or is not a database
But all is good with the correct version:
$ sqlite3 foobar.db
SQLite version 3.27.2 2019-02-25 16:06:06
Enter ".help" for usage hints.
sqlite>
sqlite> .databases
main: /tmp/foobar.db
sqlite>
The error message is a catch-all, simply meaning that the file format was not recognized.
Ok, finally found a solution that works so posting the answer if anybody will have same trouble as I did..
First of all, use good recover software. For repairing the database I found 3 solutions that work without backup :
Open corrupted database using DB Browser an Export Database to SQL. Name it however you want. Then, create new database and import database from SQL.
There is software that repairs corrupted databases. Download one and use it to repair the database.
Download "sqlite3" from sqlite.org and in command line navigate to folder where "sqlite3" is unzipped. Then try to dump the entire database with .dump, and use those commands to create a new database:
sqlite3 corrupt_table_name.sqlite ".dump" | sqlite3 new.sqlite
I had the same error when I was trying to access a db dump in another system compared to compared to where it was obtained. When I tried to open on a dev machine, it threw the reported error in this thread:
$ sqlite3 db_dump.sqlite .tables
Error: file is encrypted or is not a database
This turned out to be due to the differences in the sqlite version between those systems. This dev system version was 3.6.20.
The version on the production system was 3.8.9. Once I had the sqlite3 upgraded to same version, I was able to access all its data. I am just showing below the tables are displayed as expected:
# sqlite3 -version
3.8.9
# sqlite3 db_dump.sqlite .tables
capture diskio transport
consumer filters processes
This error is rather misleading to begin with, though.
If you've interacted with the database at some point while specifying journal_mode = WAL, and then later try to use the database from a client that does not support WAL (< v3.7.0), this error can also come up.
As noted in the SQLite documentation under Backwards Compatibility, to resolve that without having to recreate the database, explicitly set the journal mode to DELETE:
PRAGMA journal_mode=DELETE;
Your database did not become encrypted (this is only one of the two options in the error message).
Your data recovery tool did not recover the correct data; what you have in the file is something else.
You have to restore the database file from the backup.
The issue is with sqlcipher version upgrade in my case, Whenever I update my pod it automatically upgrade the sqlcipher and the error occurred.
For a quick fix just manually add the SDK instead of Pod install. And for a proper solution use this link GitHub Solution

sqlite - How does the command tool command .dump affect connected applications?

I'd like to know how the .dump command affects other applications connected to the same database. I'd like to know this for the following journal modes:
DELETE (the default mode)
WAL (write-ahead-logging)
From reading other posts on this forum .backup uses the online backup API of SQLite. It would be great to have this confirmed as well.
Thanks in advance!
The .dump command reads the contents of the database normally, just as if you would do a bunch of SELECT queries inside a transaction.
This means that when not using WAL, other connections cannot write as long as the dump is running.

How to open .SQLite files

I'm trying to open a .sqlite file on Windows, but I don't know how to. Do you know a good program for it?
It contains data for statistical analysis, but I prefer having a .txt file.
I also have a .spatialite file. Can you help me?
If you just want to see what's in the database without installing anything extra, you might already have SQLite CLI on your system. To check, open a command prompt and try:
sqlite3 database.sqlite
Replace database.sqlite with your database file. Then, if the database is small enough, you can view the entire contents with:
sqlite> .dump
Or you can list the tables:
sqlite> .tables
Regular SQL works here as well:
sqlite> select * from some_table;
Replace some_table as appropriate.
SQLite is database engine, .sqlite or .db should be a database. If you don't need to program anything, you can use a GUI like sqlitebrowser or anything like that to view the database contents.
Website: http://sqlitebrowser.org/
Project: https://github.com/sqlitebrowser/sqlitebrowser
There is also spatialite, https://www.gaia-gis.it/fossil/spatialite_gui/index
My favorite:
https://inloop.github.io/sqlite-viewer/
No installation needed. Just drop the file.
I would suggest using R and the package RSQLite
#install.packages("RSQLite") #perhaps needed
library("RSQLite")
# connect to the sqlite file
sqlite <- dbDriver("SQLite")
exampledb <- dbConnect(sqlite,"database.sqlite")
dbListTables(exampledb)

error while reading file into sqlite3

I am a newbie to sqlite3.
I have some files which contain a .db file and also another file which contains the definition s of many tables. I try to open with notepad and see that it has create table definitions of many files.
I am trying to read them in sqlite3 in linux.
I try to use .read FILENAME after which I try to see the tables by giving .tables command.
I cant see them.
Is there a way I need to source the file or execute.
Thanks for your help in advance
I haven't done anything with SQLite in a long time, but when I did work with it I used a Firefox extension that made it easy.
Check out SQLite Manager to open the DB file:
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
.read FILENAME already executes the sql script in the FILENAME. Check if your .db file is writable and/or if the sql contained your file is correct.

How to export SQLite to JSON?

Is there any way to get a SQLite view on a JSON file?
Thanks
On recent versions of SQLite, this is built in. The following:
sqlite3> .mode json
sqlite3> .once out.json
sqlite3> SELECT * from foo;
writes the table foo to out.json.
Or, directly from the command line:
sqlite3 db.sqlite3 '.mode json' '.once out.json' 'select * from foo'
.once, which writes the output of the next SQL command to the indicated file, has been in SQLite since 3.8.5 in 2014.
The .mode json is newer though, added in 3.33.0 in 2020-08. It comes with ubuntu 20.10 but older operating systems are unlikely to have that feature in their built-in SQLite version.
SQLiteStudio (sqlitestudio.pl) can export from sqlite3 database to JSON. SQLiteStudio is C++ Qt-based, open source GPLv3 licensed, Linux/macOS/Windows application with a git repository here: 'pawelsalawa/sqlitestudio'.
There are certainly ways to do this. For example, you could write a custom program that parses the JSON input via your favorite JSON processor and then generate the equivalent SQL statements to create tables, insert the rows, etc. and then import that into a SQLite capable tool (DB Browser for SQLite) to generate the actual SQLite db file.
I suspect you will be hard pressed to find a general purpose tool to accomplish this, as the content of the JSON input could vary widely, and in fact may not map well into a relational database at all.

Resources