Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty sqlite db from command line

Tags:

sqlite

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.

like image 385
amphibient Avatar asked Nov 22 '13 22:11

amphibient


People also ask

How do I start SQLite from command-line?

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.

How do I create a DB file?

Create a blank databaseOn the File tab, click New, and then click Blank Database. Type a file name in the File Name box. To change the location of the file from the default, click Browse for a location to put your database (next to the File Name box), browse to the new location, and then click OK. Click Create.

Which command is used to create or open an existing database in SQLite?

In SQLite, sqlite3 command is used to create a new SQLite database. You do not need to have any special privilege to create a database.

How do I get out of SQLite command-line?

Ctrl + D will get you out of the SQLite 3 database command prompt. That is: hold the "Ctrl" button then press the lowercase d key on your keyboard at the same time and you will escape the SQLite 3 command prompt.


2 Answers

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;" 
like image 76
emkey08 Avatar answered Sep 18 '22 16:09

emkey08


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.

like image 39
Mosty Mostacho Avatar answered Sep 19 '22 16:09

Mosty Mostacho