Database connectivity using MySQL-example
In this tutorial we will see basic example of creating table and inserting record into database [MySQL].
- 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.
Following is the block diagram of the main VI.
- Reinitialize all controls and indicators to default value.
- Open connection to MySQL database by passing Connection string. Set the status of DB Status indicator in the shift register of while loop.
- 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.vi [SubVI] creates table by executing CREATE TABLE query and release the reference (screenshot available at the end of the post)
- When Add User is clicked, first check whether the Username and Password fields are empty.
- Insert into Table.vi adds record into table(screenshot available at the end of the post).
- Check for errors.
- Simple Error Handler.vi handles the error and displays the error.
- 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.
- When UserName or Password is empty, a dialog will appear saying ”UserName and Password should not be empty”
- When stop is clicked or when error comes, exit the while loop and close the DB connection.
Insert into Table.vi