Skip to main content

MySQL

MySQL is an open-source relational database engine. In Gredit, this integration allows connecting to a MySQL or MariaDB instance to execute SQL queries from scripts via ODBC.

What it is used for

Configure a MySQL connection so that scripts can execute SQL queries and read or write data in a relational database.

Configuration

Required data

FieldDescription
HostMySQL server address (IP or hostname)
DatabaseDatabase name
PortConnection port (default: 3306)
Username / UIDConnection user
Password / PWDUser password

Add the database in Gredit

Adding the database in Gredit

  1. Go to the Databases section and create a new configuration.
  2. Fill in the host, database, port, username, and password.
  3. In Driver, enter the exact name of the ODBC driver installed on the server (for example, MySQL or MariaDB Unicode). If in doubt, validate with Support.
  4. The Name value will be the identifier used to inject the connection into scripts.

Usage in scripts

Ruby

require 'odbc_utf8'

con = ODBC.connect('TestMYSQL')
stmt = con.run('SELECT id, nombre FROM usuarios')

columns = stmt.columns.keys.map(&:downcase)

stmt.each do |row|
h = columns.zip(row).to_h
puts h['nombre']
end

stmt.drop
con.disconnect

Python

Add the pyodbc library under Libraries in the script settings.

Libraries

import pyodbc

cnxn_sql = gredit.connect('TestMYSQL')
cursor = cnxn_sql.cursor()

cursor.execute("SELECT id, nombre FROM usuarios")

columns = [col[0].lower() for col in cursor.description]

for row in cursor.fetchall():
h = dict(zip(columns, row))
print(h["nombre"])

cursor.close()
cnxn_sql.close()

Relationship with other modules

  • Accounts: The MySQL connection belongs to an account.
  • Scripts: Can be associated with one or more scripts optionally.
  • Permissions: Controlled by assigned roles and permissions.