Connecting to MySQLΒΆ

This code was contributed by Toms Baugis

import rdflib
import surf

# mysql connection string that will be passed to rdflib's mysql plugin
DB_CONN = 'host=localhost,user=surf,password=password,db=rdfstore'

def get_rdflib_store():
    store = rdflib.plugin.get('MySQL','rdfstore')

    # rdflib can create necessary structures if the store is empty
    rt =, create=False)
    if rt ==
    elif rt ==, create=True)
    elif rt ==
        store.destroy(DB_CONN), create=True)

    return store

store = surf.Store(reader='rdflib',
                   rdflib_store = get_rdflib_store())
session = surf.Session(store)

Previous topic

Loading a public remote RDF file using rdflib

Next topic

Connecting to AllegroGraph

This Page