amphibient
amphibient

Reputation: 31212

How to create an empty SQLite db with command?

Is it possible to create an empty sqlite3 database from the command line (e.g. sqlite3 <someoption> dbname) which would create a database file for me empty of any tables so that I can access it from a different SQL editor?

Currently, when I do sqlite3 dbname, I get a sqlite prompt from which I can do CREATE TABLE ... but if I don't, when I exit, no file is created. So I am looking for a single command which would create an empty database for me without a need to create at least one table within that step.

Upvotes: 63

Views: 58828

Answers (8)

For example, you can create the empty database mydb.sqlite3 with '' or "" as shown below. *My answer explains how to create a SQLite database:

sqlite3 mydb.sqlite3 ''

Or:

sqlite3 mydb.sqlite3 ""

Be careful, you cannot create the empty database mydb.sqlite3 without '' or "" as shown below:

sqlite3 mydb.sqlite3

Upvotes: 0

njamescouk
njamescouk

Reputation: 179

from https://sqlite.org/cli.html

Start the sqlite3 program by typing "sqlite3" at the command prompt, optionally followed by the name the file that holds the SQLite database (or ZIP archive). If the named file does not exist, a new database file with the given name will be created automatically. If no database file is specified on the command-line, a temporary database is created and automatically deleted when the "sqlite3" program exits.

my emphasis

Upvotes: 0

Igor Mikushkin
Igor Mikushkin

Reputation: 1317

Just create an empty file.

> test.db

Symbol ">" here means redirection.

Upvotes: 20

catfood
catfood

Reputation: 475

You can also call .databases when you enter the command prompt.

Or:

sqlite3 test.db ".databases"

Upvotes: 28

emkey08
emkey08

Reputation: 6161

Use the VACUUM command to create a valid empty SQLite database file, including the root database page and the database header.

sqlite3 file.db "VACUUM;"

Upvotes: 102

SusanthCom
SusanthCom

Reputation: 31

Creating a blank db like this ( provided by mosty-mostacho ) has an advantage of being recognised as a valid SQLite db; better than an empty file.

$sqlite3 foo.db "create table t(f int); drop table t;"

Verify it by :

$file foo.db 
foo.db: SQLite 3.x database, last written using SQLite version 3024000

Upvotes: 3

Owen Zhao
Owen Zhao

Reputation: 3355

The simple way is in bash, use command

touch file.db

This will just create a 0 size file and can be used as an empty sqlite file.

Upvotes: 32

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

I don't think there is a way to do that in just one statement.

In Linux I would workaround it this way:

sqlite3 aFile.db "create table aTable(field1 int); drop table aTable;"

This will automatically create the needed file with the table in it and then drop it leaving the database file without tables. That's the closest thing I know.

Anyway, I think most editors will even accept an empty file too. Give that a try.

Upvotes: 43

Related Questions