Working with Relational Databases via SQL
From ADS scripts, the built-in object SQL
can be used to connect to relational databases through SQL.
This may be used to implement batch importers or update scripts.
Two functions are provided right now
SQL.query
for Querying SQL Databases from ADSSQL.update
for Updating SQL Databases with ADS
Querying SQL Databases from ADS
The following example walks through all rows of the table called country
in the database called world
and creates one instance of the RDFS class g:Country
for each, based on the values of the Code
and Name
columns.
1let conn = {
2 url: 'jdbc:mysql://localhost:3306/world',
3 user: 'newuser',
4 password: '...'
5}
6let sql = `
7 SELECT Code, Name
8 FROM country;
9`
10let results = SQL.query(conn, sql);
11results.forEach((row) => {
12 g.createCountry({
13 isoCountryCode3: row.Code,
14 prefLabel: row.Name
15 })
16})
Updating SQL Databases with ADS
The following example shows the syntax of SQL update commands. As usual, you may want to use JavaScript string substitution to produce a suitable SQL string from your data.
1let conn = {
2 url: 'jdbc:mysql://localhost:3306/world',
3 user: 'newuser',
4 password: '...'
5}
6let sql = `
7 DROP TABLE country;
8`
9SQL.update(conn, sql);