DB Browser for SQLite lets you view and edit SQLite databases on Linux. You can design, create, and edit these database files, and peek inside the inner workings of other applications. Here’s how to use this SQLite GUI.
The SQLite Database Project
The SQLite database library and tools are a phenomenally successful open-source Structured Query Language (SQL) database project. So successful, in fact, it can justifiably call itself the most widely deployed database engine in the world.
Since its initial release in 2000, SQLite has seen an absolutely staggering uptake. It’s inside every iPhone and Android phone, and Windows 10 or Mac computer. It’s also in every internet browser, including Chrome, Firefox, and Safari, as well as countless other applications.
The incredible reach of the SQLite database is due to its architecture. It’s a fast, lightweight library that’s incorporated (or linked, in developer-speak) in other applications. The database engine becomes an integral part of the product. This means you don’t have to provide an external SQL database server, like MySQL, MariaDB, or Microsoft SQL Server.
SQLite also has a command-line tool for manipulating databases, but it’s the library that’s made it a triumph. Having a capable, self-contained database engine tucked away inside your application removes a lot of problems. It simplifies your application’s installation routines and lowers the minimum hardware requirements for your application.
Additionally, because SQLite uses a single cross-platform file for its database tables, indexes, and schemas, the entire database can be moved to another computer. You can even move it to a machine running a different operating system by copying one file.
In fact, SQLite’s database file format is so well regarded, it’s one of only a handful recommended by the Library of Congress for long-term data storage.
Because SQLite is a developer’s library, however, there isn’t a front-end for it, meaning it doesn’t have a Graphical User Interface. It’s the application using the library that provides the user interface. The command-line utility can run in an interactive mode, but it still isn’t a GUI.
DB Browser for SQLite (DB4S) fits the bill nicely. It’s a visual tool developed by another open-source project to allow the creation and manipulation of SQLite databases from within a GUI.
DB Browser for SQLite
DB Browser for SQLite has been around (in one incarnation or another) since 2003 and has undergone several name changes. Previously, it was called SQLite Browser, but that caused confusion. People thought it had been written by the SQLite team, and thus, were forwarding feature requests and support queries about DB4S to SQLite.
So, SQLite Browser was renamed DB Browser for SQLite. You’ll still see references to the old name here and there. In fact, the website for the project still uses “sqlitebrowser” as its domain, and the old name is also used during the installation of DB4S.
With DB4S you can:
- Create databases.
- Import and export database schemas, tables, and data in SQL format.
- Import and export tables and data in CSV format.
- Create, edit, and delete tables and indexes.
- Add, edit, and delete records.
- Browse and search for database records.
- Edit and run SQL commands. You can make sure the command does what you think it’s going to before you hard-code some SQL into your application.
Installing DB Browser for SQLite
To install DB4S on Ubuntu, use the following command (again, note the installation still uses the old name):
sudo apt-get install sqlitebrowser
On Fedora, you type:
sudo dnf install sqlitebrowser
On Manjaro, we use
sudo pacman -Sy sqlitebrowser
Importing a Database from an SQL File
When DB4S starts, it doesn’t have a database loaded into it. We’re going to look at two ways you can import both data and database table definitions, as well as how you can create your own database.
Sometimes, you might be given or sent a database dump file in SQL format. This contains the instructions necessary to re-create a database and insert its data into a database.
Another common format used to import table definitions and data is the comma-separated values (CSV) format. You can use a data-generation site, such as Database Test Data, to generate dummy data for practicing purposes. You can then export your data as SQL or CSV.
Below is an SQL file we created on that site. After it was exported, we edited it and added a line at the top of the file, which is required for SQLite:
We then saved the file. In DB4S, we click File > Import > Database from SQL File.
A file selection dialog opens so we can choose our SQL file. In our example, it’s called “database_dump.sql,” and it’s located in the root of our home directory.
With the file selected, we click “Open,” and a file-save dialog opens. You now have to name your new database and decide where to save it. We called ours “geekbase.sqlite3,” and we’re saving it in our home directory.
Click “Save” when you’re ready to proceed. We’ve identified our source SQL file and named our new database, so the import process can now begin. After it’s complete, you’ll see the notification dialog below.
Because we’ve added tables and data to the database, we’re prompted to save those changes, so we click “Save” to do so.
The main DB4S window will now display the structure of our database.
There are two tables created, although there was only a definition for one in the SQL file. This is because the “id” field was defined as an auto-incrementing field. It will be added automatically whenever a new record is added to the database. SQLite creates a table to keep track of auto-incrementing fields.
Click the “Browse Data” tab to see your newly added records.
Of course, the power of a database lies in its ability to search and extract records. With an SQL-enabled database, we use the SQL language to do this. To get started, the “Execute SQL” tab.
We’ve added the following SQL command:
SELECT * FROM account_details WHERE last_name LIKE "%ll%" ORDER BY state
This will search for people with a double “l” in their last name, and the results will be sorted by state. Click the blue arrow (it looks like a “Play” button) to run your SQL command. The results are displayed in the lower pane.
We have four records that contain double “l” in the last name, and they’re sorted alphabetically by state, from Arizona to Wisconsin.
Importing a Database Table from a CSV File
We can also import tables from suitable CSV files. The most convenient way to do so is to have the table field names in the CSV file as the first row of text. Below is a short section of a CSV file.
The first line holds the field names: first_name, last_name, created, email, state, and ID. The other lines hold the data values for each record that will be added to the table. This is the same data as before; only the file format has changed.
When you import CSV data, you have to create an empty database so you have something to import it to. To do so, click “New Database” in the toolbar.
A file-save dialog opens. Name your new database and decide where to save it. We’re going to call ours “howtogeek.sqlite3” and save it in our home directory.
When the “Edit Table Definition” dialog appears, click “Cancel.” Back in the main DB4S window, click File > Import > Table From CSV File. A file selection dialog opens in which you can choose your CSV file.
In our example, it’s called “users.csv,” and it’s located in the root of our home directory. Click “Open,” and a preview dialog appears to show you how DB4S will interpret the CSV data.
The name of the file is used as the name of the table. You can edit this if you want, just make sure you select the checkbox next to “Column Names in First Line.”
Click “OK” (it’s off-screen in the image above). The data is imported and, if all is well, you should see the “Import Completed” dialog; click “OK.”
Click “Browse Data,” and you’ll see the imported data.
We still have a small tweak to make, though. Click the “Database Structure” tab, select the name of the table, and then click “Modify Table” in the toolbar.
In the “Edit Table Definition” dialog, select the “AI” (auto-incrementing) checkbox in the “id” field.
The “PK” (Primary Key) checkbox is automatically selected for you; click “OK.” This sets the “id” field to be auto-incrementing. We can now add a new record to the database to verify it’s working.
Click the “Execute SQL” tab, and then type the following SQL in the upper pane (note that we’re supplying values for all the fields except “id”):
INSERT INTO "users" ("first_name","last_name","created","email","state") VALUES ('Dave','McKay','12/08/2020','email@example.com','Idaho');
Click the blue arrow (that looks a Play button) to run your SQL command. Click “Browse Data” and scroll to the bottom. You should see your newly added record with an automatically provided “id” field holding a value that’s one higher than the previous highest “id” value.
Creating a Database Manually
If you don’t have an SQL or CVS file to import, you’ll have to create your database manually. To get started, click “New Database” and a file-save dialog appears. Type the name of your new database and where you want to save it.
We’ve named ours “geeksrock.sqlite3,” and we’re saving it in the “Documents” directory. After you name your database and navigate to the location in which you want to save it, click “Save.”
When asked to confirm your choices, click “Save” once again.
The “Edit Table Definition” dialog appears. Name your new table (we’ve called ours “eagles”), and then click “Add Field.” You can now type a name for the field and select the type of information it will contain from the “Type” drop-down menu.
We’ve added a text field to hold the name of the eagle, and a real (floating point) numeric field to hold the wingspan.
The checkboxes and other options next to each field allow you to add the following behaviors:
- NN (Not Null): When this option is set, the field can’t be left empty. If you try to add a record without providing a value for this field, it will be rejected.
- PK (Primary Key): A field (or group of fields) that provides a unique identifier for the records in the table. This might be a simple numeric value, like the auto-incrementing integer fields we covered above. In a table of user accounts, though, it might be a username. There can only be one primary key in a table.
- AI (Auto-Incrementing): Numeric fields can be automatically filled in, with the next highest unused value. We used this in the “id” field in the example we covered above.
- U (Unique): This field in each record must hold a unique value, meaning there can’t be any duplicates in this field in the table.
- Default: A default value will be provided if a record is added that has no value in this field.
- Check: A field can have a check performed on it when a record is added. For example, you might check that the value in a phone number field has at least 10 characters.
- Collation: Sqlite can use different methods of string comparison. The default is
BINARY. Other options are
NOCASE, which is case-insensitive, and
RTRIM, which ignores trailing white space. In most cases, you can leave this to the default value.
- Foreign Key: A field or group of fields in a record that must match a key in another table. For example, in a database of workshop tools, you might have a table of individual tools and another table of tool categories. If the tool categories are “hammer,” “spanner,” and “screwdriver,” you can’t add a record of type “chisel.”
Click “OK” after you add the fields you want. After your database is created and your first table is added, you can add some records.
In the “Execute SQL” tab, we used the SQL INSERT statement several times to add some records to the table.
We can also use the “Browse Data” tab to see our new records.
If you prefer to add new records through the user interface, click “New Record” on the toolbar. You can then input the values for the new record without understanding SQL.
Other Application’s Databases
You can also use DB4S to see the SQLite databases that belong to other applications. Examining the structure or contents of other databases can be instructive (or just interesting). It’s important that you don’t make changes to databases owned by other applications, though, or you might adversely affect the operation of that application.
Let’s look at one of the SQLite databases Firefox creates and maintains. Click “Open Database” on the toolbar, and a file-open dialog will appear. Firefox keeps its files in a directory called “firefox,” which is inside a hidden directory called “.mozilla” that sits in your home directory.
On our test machine, we found the Firefox SQLite databases at this location: “home/dave/.mozilla/firefox/vpvuy438.default-release”; yours will be in a similar location.
We’re going to open the “cookies.sqlite” database, so we highlight the file, and then click “Open.” With the database opened, you can examine its table structure, field definitions, and data.
Here, we can see a variety of Google and YouTube cookies.
Exporting Provides Insights
Exporting a database (by clicking File > Export > Database to SQL File) is useful, as well. By looking at the SQL dump of a database, you can see the entire schema of the database rendered in SQL statements.