Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) - GDevCon#2

106 views

Published on

Sam Sharp's presentation from GDevCon#2 on Efficient Working with Databases in LabVIEW.

This presentation discusses some best practice hints & tips for working with databases in LabVIEW and uses Yii's ActiveRecord implementation as an example of how we can work more efficiently with databases in LabVIEW.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) - GDevCon#2

  1. 1. Efficient Working with Databases Sam Sharp MediaMongrels Ltd sam@mediamongrels.com
  2. 2. WebSockets API • New version (V2.X) coming soon! – New OO-based API – Available on NI Tools Network Client: Server: WSS Support?!
  3. 3. Introduction • Considerable amount of LabVIEW & Database exposure: – PHP/MySQL Web Development (4-5 years) – UK Fuel Cell Company • Test Station Monitoring System (~100 Test Stations) • 60s Update Resolution (Test/Asset Status, PC Health) – Swiss Light Source Company • Design & Rollout of Production Test Database (10-15 tables) • TestStand Results Processing Plugin & LabVIEW API • Reporting & Analysis Tool in LabVIEW • Legacy Data Import • Writing VIs to talk to databases in LabVIEW is tedious.
  4. 4. Databases Overview • Database Advantages: – Store large amounts of information – Sharing of information • Concurrent multi-user access – Quick access to information – Eliminate duplication of data – Information Security – Backup/Restore • Common DBMS: – Relational • Linked Tables (Rows & Columns) • Examples: MySQL, MS-SQL, Oracle, PostgreSQL, SQLite – NoSQL • Unstructured/Semi-Structured Data • Examples: MongoDB, Redis, Cassandra • LabVIEW <-> Database – Database Connectivity Toolkit – 3rd Party Toolkits (SQLite, GDatabase)
  5. 5. Database Toolkit Basics ODBC Drivers MySQL: MySQL ODBC X.X ANSI Driver MS-SQL: SQL Native Client LV 32-bit
  6. 6. Best Practice Hints & Tips • Security – Use parameter binding for user input (SQL injection) – Passwords in VIs? – Limit application access to databases by user roles • Performance – Do not repeatedly open/close the connection – Database design… …but what if the username & password is: “ OR “1”=“1 SELECT * FROM operator WHERE username = ““ OR “1”=“1” AND password = ““ OR “1”=“1” Any username/password will grant access! Looks harmless (and I have seen this done in many places!) https://xkcd.com/327/
  7. 7. Database Design • Database design is important – Poorly designed database issues: • Poor quality data • Poor performance – Especially when volume of data increases • Redundant data • Poor data integrity • Difficult to extend/improve • Normalisation Principles – 1NF, 2NF, 3NF etc. – Guide good relational database design • Each relation adds complexity – sometimes it is sensible to break to rules. – e.g. execution_id in step_measurement – Storing JSON configuration (for audit purposes only)
  8. 8. Question: How long would it take to write code to Create, Read, Update & Delete rows for these five tables?
  9. 9. In ~90 seconds* we have: • Configured a database connection • Generated model classes for 5 tables – One model class per table – Columns as model properties – Basic validation (e.g. required fields) • Created a user interface to create, view, update and delete rows from our Operator table … without writing a single SQL query or line of PHP code …leaving more time to develop the ‘meat’** of the application …the joy of frameworks! A prize for anyone that can rewrite as a Haiku! *sequences have been shortened (no lawsuits please!) **or your favourite soy-based substitute
  10. 10. Yii PHP Framework • Popular framework for developing web-based applications – PHP & MySQL – Based on Model-View-Controller (MVC) architecture • Model classes represent object/table data (e.g. customer, order) • Views display data to the user • Controllers implement the ‘business logic’ by interacting with the models and preparing the views – Contains tools for auto-generating code from a database schema
  11. 11. What if we could do some of this in LabVIEW?
  12. 12. ActiveRecord • ActiveRecord implements the Models in Yii’s MVC – ActiveRecord objects contain persistent data and behaviours operating on that data • Class = Table • Object = Row • Properties = Columns • Methods = Behaviours (e.g. create, read, update, delete, business logic) • Purpose: – Representing models and their data – Representing relations between models (e.g. foreign keys) – Validating models – Performing database operations +Insert() +Update() +Delete() +GetByID() +CheckPermission() -id -username -full_name -level Operator
  13. 13. In ~150 seconds* we have: • Configured a MySQL connection (ish) • Generated model classes for 5 2 tables – One model class per table – Columns as model properties – Basic validation (e.g. required fields) • Created a user interface VI to create, view, update and delete rows from our Operator Test Execution table … without writing a single SQL query or much LabVIEW code …leaving more time to develop the ‘meat’** of the application …the joy of frameworks! *sequences have been shortened (no lawsuits please!) **or your favourite soy-based substitute
  14. 14. Current Features • ActiveRecord base class – Create, Read, Update & Delete of Objects – Automatic creation of model classes using Scripting • Supporting Libraries / Tools – Database Connection Class • Database Connectivity Toolkit – Query Command Library • Builds SQL queries from VIs – Integrated into LV Tools folder
  15. 15. Future Functionality • Query Options – FindAll, FindByAttributes etc. • Model Validation • Foreign Key Relations – Test_Execution.Test_Steps • User Interface Binding?
  16. 16. Thanks for listening! Questions?

×