Database connectivity using MySQL-example

In this tutorial we will see basic example of creating table and inserting record into database [MySQL].

Tools required:

  • LabVIEW
  • Database connectivity Toolkit
  • MySQL server.

Following is the front panel of the main VI.

  • TableName is the name of the table.
  • UserName [Primary key] and Password are the fields in the table.
  • DB Status is an indicator which displays the status of this application.

Front panel

Following is the block diagram of the main VI.

DB timeout

  1. Reinitialize all controls and indicators to default value.
  2. Open connection to MySQL database by passing Connection string. Set the status of DB Status indicator in the shift register of while loop.
  3. When Create Table is clicked, first check for table existence [DB Tools List Tables.vi]. If it is already there, drop [delete] the table and create new table with the UserName field set as primary key.

 Create table

  1. Create Table.vi [SubVI] creates table by executing CREATE TABLE query and release the reference (screenshot available at the end of the post)
  1. When Add User is clicked, first check whether the Username and Password fields are empty.

Insert

  1. Insert into Table.vi adds record into table(screenshot available at the end of the post).
  1. Check for errors.
  1. Simple Error Handler.vi handles the error and displays the error.
  1. Clears the error, this is because LabVIEW gives error when we try to insert duplicate record into primary field column.  If error is carried out of event structure, application will stop.
  1. When UserName or Password is empty, a dialog will appear saying ”UserName and Password should not be empty”

Empty field

  1. When stop is clicked or when error comes, exit the while loop and close the DB connection.

Create Table.vi

Create table subvi

Insert into Table.vi

Insert vi

Error Check.vi

 Error check vi

 

Leave a Reply