Last active
May 26, 2020 20:31
-
-
Save peterVG/36745a2157c56cea83b7ad440e61dd5b to your computer and use it in GitHub Desktop.
Populate MySQL database with AIPscan data for use as Grafana datasource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql.cursors | |
import sqlite3 | |
mysqlConnection = pymysql.connect( | |
host="localhost", | |
user="grafana", | |
password="grafana", | |
db="grafana", | |
charset="utf8mb4", | |
cursorclass=pymysql.cursors.DictCursor, | |
) | |
mysqlCursor = mysqlConnection.cursor() | |
sql = """DROP TABLE aipfiles""" | |
mysqlCursor.execute(sql) | |
mysqlConnection.commit() | |
sql = """CREATE TABLE aipfiles( | |
id INT AUTO_INCREMENT PRIMARY KEY, | |
name VARCHAR(255), | |
uuid VARCHAR(255), | |
type VARCHAR(255), | |
size INTEGER, | |
puid VARCHAR(255), | |
format VARCHAR(255), | |
format_version VARCHAR(255), | |
related_uuid VARCHAR(255), | |
creation_date DATETIME, | |
ingestion_date DATETIME, | |
normalization_date DATETIME, | |
aip_id INTEGER NOT NULL | |
)""" | |
mysqlCursor.execute(sql) | |
mysqlConnection.commit() | |
sqliteDb = sqlite3.connect("aipscan.db") | |
sqliteCursor = sqliteDb.cursor() | |
sqliteCursor.execute("SELECT * FROM files") | |
allaipfiles = sqliteCursor.fetchall() | |
for aipfile in allaipfiles: | |
sql = "INSERT INTO `aipfiles` (`id`, `name`, `uuid`, `type`, `size`, `puid`, `format`, `format_version`, `related_uuid`, `creation_date`, `ingestion_date`, `normalization_date`, `aip_id`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" | |
mysqlCursor.execute( | |
sql, | |
( | |
aipfile[0], | |
aipfile[1], | |
aipfile[2], | |
aipfile[3], | |
aipfile[4], | |
aipfile[5], | |
aipfile[6], | |
aipfile[7], | |
aipfile[8], | |
aipfile[9], | |
aipfile[10], | |
aipfile[11], | |
aipfile[12], | |
), | |
) | |
mysqlConnection.commit() | |
mysqlConnection.close() | |
sqliteDb.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment