SQL in visual components

Hi all,

Is there functionality to read SQL files with Visual Components as input to a simulation?

Hello,
There’s no built-in functionalities for this. However, VC has Python library sqlite3 installed, so executing a SELECT query for a SQLite database and doing the desired things with the resulting database rows is just a few rows of script.

Here’s an example script that opens the SQLite database selected in URI property “DB file” and executes a simple query for this sample DB file: SQLite Sample Database And Its Diagram (in PDF format)

from vcScript import *
import sqlite3

uri_prop = comp.getProperty('DB file')
conn = sqlite3.connect(uri_prop.Value[8:].decode('utf-8'))
query = 'SELECT * FROM customers'
conn = connect()
cursor = conn.cursor()
cursor.execute(query)
# Print all rows
rows = cursor.fetchall()
for row in rows:
  print(row)

edit: I found out later that importing sqlite3 fails with some installations. It can be easily fixed by copypasting sqlite3.dll from VC:s installation folder subfolder /Python 2/DLLs to the root of the installation folder in C:/Program Files/Visual Components/VERSION. A bug has been reported already.

2 Likes

Thank you for the help Kusti!