Python SQL Clients
Any programming language or language package with a MySQL client can also connect to Dolt. Here we will discuss Python specific MySQL clients.
Dolt is 99% MySQL compatible, and extends custom SQL functions specific to version controlling. (this blog is a good practical introduction).
The full set of MySQL commands that Dolt supports can be found here.
Integration examples:

Starting a Dolt Server

Install Dolt:
1
$ sudo bash -c 'curl -L https://github.com/dolthub/dolt/releases/latest/download/install.sh | sudo bash'
Copied!
Create or clone a database:
1
$ mkdir new-databasse
2
$ cd new-database
3
$ dolt init
Copied!
Start a server:
1
$ dolt sql-server -l trace --max-connections 10
Copied!
Test connection in a different window:
1
$ mysql --user=root --host=0.0.0.0 -p new_database
2
Enter password:
3
mysql >
Copied!

Clients

PyMySQL

PyMySQL is one of many MySQL Python client libraries. Dolt's version control functions are exposed in SQL, and you can run every Dolt function except dolt clone using its SQL equivalent.
Connecting to a Dolt database looks identical to connecting to MySQL:
1
>>> import pymysql.cursors
2
>>> conn = pymysql.connect(
3
host="localhost",
4
user="root",
5
password="",
6
database="new_database",
7
cursorclass=pymysql.cursors.DictCursor,
8
)
9
>>> with conn.cursor() as cur:
10
cur.execute("select * from dolt_log")
11
cur.fetchone()
12
>>> conn.commit()
13
{'commit_hash': 'bki1pgk63a34ouqj0mvjg5mfqtgf7jh1', 'committer': 'Max Hoffman', 'email': '[email protected]', 'date': datetime.datetime(2021, 6, 24, 20, 9, 56, 82000), 'message': 'Initialize data repository'}
Copied!
Refer to the pymysql docs for more information.

SQLAlchemy

SQLAlchemy sits between Python applications an databases, letting Python libraries support multiple database dialects with a single SQLAlchemy integration.
Libraries that implement an SQLAlchemy integration are also Dolt-compatible.
You would normally let SQLAlchemy generate query strings depending on the database dialect. Below is a simple progression of the pymysql connector with a hardcoded query:
1
>>> from sqlalchemy import create_engine
2
>>> engine = create_engine("mysql+pymysql://[email protected]/new_database")
3
>>> with engine.begin() as conn:
4
... conn.execute("select * from dolt_log limit 1").fetchone()
5
('bki1pgk63a34ouqj0mvjg5mfqtgf7jh1', 'Max Hoffman', '[email protected]', datetime.datetime(2021, 6, 24, 20, 9, 56, 82000), 'Initialize data repository')
Copied!
Refer to the sqlalchemy docs for more information.

Pandas

Pandas is a data manipulation library. Pandas has a feature that moves data between DataFrames and databases using SQLAlchemy connectors.
1
>>> import pandas as pd
2
>>> from sqlalchemy import create_engine
3
>>> df = pd.DataFrame({"name" : ["User 1", "User 2", "User 3"]})
4
>>> engine = create_engine("mysql+pymysql://[email protected]/new_database")
5
>>> df.to_sql("users", con=engine, index=None)
6
>>> pd.read_sql("select * from users", engine)
7
name
8
0 User 2
9
1 User 3
10
2 User 1
Copied!
Refer to the pandas docs for more details.
Last modified 5d ago