Writing

Introduction

This tutorial will walk through how to put data into a Dolt database using the same interfaces. For each interface we will initially write the following CSV to create some sample data to work with:
1
$ cat great_players.csv
2
name,id
3
rafa,1
4
roger,2
5
novak,3
Copied!
And then update the data with the as follows to illustrate some of Dolt's more unique features:
1
$ cat great_players_with_majors.csv
2
name,id
3
rafa,1
4
roger,2
5
novak,3
6
andy,4
Copied!

CLI

First let's create a new Dolt database:
1
$ mkdir tennis-players && cd tennis-players
2
$ dolt init
3
Successfully initialized dolt data repository.
4
$ ls
5
../ ./ .dolt/
Copied!
This created our database, now let's load in our initial data:
1
$ dolt table import -c --pk id great_players great_players.csv
2
Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0
3
Import completed successfully.
4
$ dolt status
5
On branch master
6
Untracked files:
7
(use "dolt add <table|doc>" to include in what will be committed)
8
new table: great_players
Copied!
Now let's generate a commit for that data:
1
$ dolt add great_players && dolt commit -m 'Added some great players'
2
.
3
.
4
.
Copied!
Now suppose that we would like to add a player:
1
$ cat great_players.csv
2
name,id
3
rafa,1
4
roger,2
5
novak,3
6
andy,4
7
$ dolt table import -u great_players great_players.csv
8
Rows Processed: 4, Additions: 1, Modifications: 0, Had No Effect: 3
9
Import completed successfully.
10
$ dolt diff
11
diff --dolt a/great_players b/great_players
12
--- a/great_players @ c2tpkad9e5345sjq2h7e6d9pdp7383a6
13
+++ b/great_players @ 6sdov20gjulkskcm9t97qmbq9dkj3l5v
14
+-----+------+----+
15
| | name | id |
16
+-----+------+----+
17
| + | andy | 4 |
18
+-----+------+----+
19
$ dolt add great_players && dolt commit -m 'do not forget Andy!'
Copied!
We were able to reimport our file, and let Dolt figure out the differences.
We just saw a simple example of how to create and import data into the Dolt data format. We now use the same example to illustrate alternative write interfaces.

SQL Shell

First let's create a new database:
1
$ mkdir tennis-players && cd tennis-players
2
$ dolt init
3
Successfully initialized dolt data repository.
4
$ ls
5
../ ./ .dolt/
Copied!
Now let's get into the SQL console and create a table.
1
$ dolt sql
2
# Welcome to the DoltSQL shell.
3
# Statements must be terminated with ';'.
4
# "exit" or "quit" (or Ctrl-D) to exit.
5
tennis_players> CREATE TABLE great_players (name VARCHAR(32), id INT, PRIMARY KEY (id));
6
tennis_players> DESCRIBE great_players;
7
+-------+-------------+------+-----+---------+-------+
8
| Field | Type | Null | Key | Default | Extra |
9
+-------+-------------+------+-----+---------+-------+
10
| name | VARCHAR(32) | YES | | | |
11
| id | INT | NO | PRI | | |
12
+-------+-------------+------+-----+---------+-------+
Copied!
Now let's execute some insert statements to get some data in there:
1
tennis_players> INSERT INTO great_players VALUES ("rafa", 1);
2
Query OK, 1 row affected
3
tennis_players> INSERT INTO great_players VALUES ("roger", 2);
4
Query OK, 1 row affected
5
tennis_players> INSERT INTO great_players VALUES ("novak", 3);
6
Query OK, 1 row affected
Copied!
We can go to the command line to check the status of our tables, which will show we have created a new table that is now in our working set:
1
$ dolt status
2
On branch master
3
Untracked files:
4
(use "dolt add <table|doc>" to include in what will be committed)
5
new table: great_players
Copied!
Then we can execute the usual Git-like workflow:
1
$ dolt add great_players
2
$ dolt commit -m 'Added some great players'
3
.
4
.
5
.
Copied!
Now let's append a row and generate another commit
1
$ dolt sql
2
# Welcome to the DoltSQL shell.
3
# Statements must be terminated with ';'.
4
# "exit" or "quit" (or Ctrl-D) to exit.
5
tennis_players> INSERT INTO great_players VALUES ("andy", 4);
6
Query OK, 1 row affected
Copied!
And again we can generate a commit:
1
$ dolt add great_players
2
$ dolt commit -m 'Added Andy!'
3
.
4
.
5
.
Copied!
We just executed an identical set of updates to our database using pure SQL.

Writing using DoltHub

You can also use DoltHub to import files and run SQL queries to update data. This is the lowest barrier to entry for putting data into Dolt. Learn more about those specific steps here.

Next steps

If you're coming from a data science background and want to use Dolt with Pandas or another Python library, check out the Python quickstart guide.
Or dive right into the docs for the SQL interface or the CLI.
Last modified 23d ago