Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab...

50
Database Lab – Tips & Tricks h2p://home.hit.no/~hansha/?lab=database 1 HansPe2er Halvorsen, Telemark University College, 2013.04.08 Note! The Assignment is located here: Design and Implement a System for Logging and PresentaSon of Weather Data Database Weather Sta,on Logging Weather Data Apps for Manipula,on and Presenta,on of Weather Data

Transcript of Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab...

Page 1: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Database  Lab  –  Tips  &  Tricks  

h2p://home.hit.no/~hansha/?lab=database    1  

Hans-­‐Pe2er  Halvorsen,  Telemark  University  College,  2013.04.08  

Note!  The  Assignment  is  located  here:  

Design  and  Implement  a  System  for  Logging  and  PresentaSon  of  Weather  Data  

Database  

Weather  Sta,on  

Logging  Weather  Data  

Apps  for  Manipula,on    and  Presenta,on    of  Weather  Data  

Page 2: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Table  of  Contents  

1. Introduc,on  2. Database  Systems  3. Database  Design  -­‐  Visio  4. SQL  Server  5. LabVIEW  

2  

Page 3: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

1.  IntroducSon  

3  

Page 4: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

4  

(Windows  Server)  

SQL  Server  2012  

(Windows  7/8)  

Client  

LabVIEW  2012  

System  Overview  

Note!  Normally  the  Database  is  installed  on  a  separate  Server,  but  for  simplicity,  we  install  the  SQL  Server  on  our  local  computer  

Server  

Database  

Visio  2012  

Page 5: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

SoXware  

5  

•  MicrosoR  Visio  2010  (Visio  2013  is  not  recommended!)  •  MicrosoR  SQL  Server  2012  (Express)  •  LabVIEW  2012  •  SQL  Toolkit  

MicrosoX  Visio  2010  and  MicrosoR  SQL  Server  2012  are  free  to  use  as  they  are  included  in  the  MicrosoX  DreamSpark  Premium  SubscripSon  available  for  students  at  Telemark  University  College.    In  our  assignment  it  is  recommended  to  use  the  MicrosoX  SQL  Server  2012  Express  version  which  is  free  of  charge  and  can  be  downloaded  here:  hZp://www.microsoR.com/en-­‐us/download/details.aspx?id=29062    

In  this  Assignment  you  need  the  following  SoRware:  

SQL  Toolkit  makes  it  easy  to  access  Database  Systems  from  LabVIEW.  The  Toolkit  is  created  by  Hans-­‐PeZer  Halvorsen  and  is  free  of  charge  from  my  web  site.  

Page 6: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Task  Overview  •  Database  Design  in  MS  Visio  •  Create  Database  and  Tables  in  MS  SQL  Server  •  SQL    Queries  (Structured  Query  Language)  •  Create  &  Use  a  Database  Views  •  Create  &  Use  a  Database  Stored  Procedures  •  Create  &  Use  a  Database  Triggers  •  Create  Applica,ons  in  LabVIEW  that  communicates  with  the  Database  created  

h2p://home.hit.no/~hansha/?lab=database    See  the  Assignment  for  detailed  Task  DescripSons:  

6  

Page 7: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Tutorials,  etc.  •  Structured  Query  Language  •  Database  CommunicaSon  in  LabVIEW  →  Use  these  Tutorials  to  get  a  beZer  understanding  of  the  topics  involved  in  the  assignment  •  Example  Code  →  The  Example  Code  help  you  solve  the  assignment!  

7  h2p://home.hit.no/~hansha/?lab=database    You  find  Tutorials,  Example  Code  and  Addi,onal  Resources  here:  

Page 8: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Hardware  

8  

We  will  use  a  NI  USB-­‐TC01  Thermocouple  Measurement  Device  in  order  to  log  Temperature  Data  into  the  Database  

Page 9: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

NI  USB-­‐TC01  Thermocouple  Measurement  Device  using  LabVIEW  

The  USB-­‐TC01  Device  works  similar  as  the  USB-­‐6008  DAQ  device,  i.e.,  you  use  the  DAQ  Assistant  

9  

Page 10: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Temperature  SimulaSon  

10  

If  you  don’t  have  a  TC01  device,  you  can  create  a  simple  “Temperature  Simulator”  instead.  

A  simple  SubVI  that  simulates  a  Temperature  value  using  a  Random  Generator  

In  this  way  you  can  easily  switch  between  the  real  Temperature  sensor  (TC01)  and  the  Simulator      

Case  Structure  

Case  Structure  

While  Loop  

While  Loop  

Example  of  such  as  Simulator:  

Page 11: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

2.  Database  Systems  

11  

Page 12: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Database  Systems  

12  

A  Database  is  a  structured  way  to  store  lots  of  informa,on.    The  informa,on  is  stored  in  different  tables.  -­‐  “Everything”  today  is  stored  in  databases!    Examples:  •  Bank/Account  systems    •  Informa,on  in  Web  pages  such  as  Facebook,  Wikipedia,  YouTube,  etc.  

•  Fronter,  TimeEdit,  etc.  •  …  lots  of  other  examples!  

Page 13: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Database  Management  Systems  (DBMS)  •  MicrosoX  SQL  Server  

–  Enterprise,  Developer  versions,  etc.  (Professional  use)  –  Express  version  is  free  of  charge  

•  Oracle  •  MySQL  (owned  by  Oracle,  but  previously  owned  by  Sun  

Microsystems)  -­‐  MySQL  can  be  used  free  of  charge  (open  source  license),  Web  sites  that  use  MySQL:  YouTube,  Wikipedia,  Facebook  

•  MicrosoR  Access  •  IBM  DB2  •  Sybase  •  etc.  

13  

We  will  use  SQL  server  because  it  is  very  popular  in  the  industry  today,  and  we  can  use  it  for  free  via  the    MicrosoX  DreamSpark  Premium  SubscripSon  –  which  is  available  for  the  students  and  staff  at  Telemark  University  College,  or  use  the  Express  version  which  is  available  for  free  for  everybody.  

Page 14: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

MicrosoX  SQL  Server  

14  

SQL  Server  consists  of  a  Database  Engine  and  a  Management  Studio.  The  Database  Engine  has  no  graphical  interface  -­‐  it  is  just  a  service  running  in  the  background  of  your  computer  (preferable  on  the  server).  The  Management  Studio  is  graphical  tool  for  configuring  and  viewing  the  informa,on  in  the  database.  It  can  be  installed  on  the  server  or  on  the  client  (or  both).  

The  newest  version  of  MicrosoR  SQL  Server  is  “SQL  Server  2012”  Note!  You  need  to  install  both  

on  your  local  computer!  

Page 15: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

3.  Database  Design  -­‐  Visio  

15  

Page 16: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

MicrosoX  Visio  

16  

Tools  for  Design  and  Modeling  Database  Systems:  •  ERwin  (very  good!  –  But  expensive!  ~$5000)  

Note!  “CA  ERwin  Data  Modeler  Community  Edi,on”  is  a  free  edi,on  that  contains  a  subset  of  the  standard  product  

•  Toad  Data  Modeler  •  MS  Visio  •  …  lots  of  other  tools  

→  We  will  use  MS  Visio  because  it  has  everything  we  need  and  it  is  a  very  popular  part  of  the  MS  Office  package  (and  you  probably  already  have  it  installed).  We  can  also  use  it  for  free  via  the    MicrosoX  DreamSpark  Premium  SubscripSon  available  for  the  students  and  staff  at  Telemark  University  College  

Page 17: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Database  Design  –  ER  Diagram  

17  

ER  Diagram  (En,ty-­‐Rela,onship  Diagram)    •  Used  for  Design  and  Modeling  of  Databases.  •  Specify  Tables  and  relaSonship  between  them  (Primary  Keys  and  Foreign  Keys)  

Primary  Key  Primary  Key  

Foreign  Key  

Table  Name  

Table  Name  

Rela,onal  Database.  In  a  rela,onal  database  all  the  tables  have  one  or  more  rela,on  with  each  other  using  Primary  Keys  (PK)  and  Foreign  Keys  (FK).  Note!  You  can  only  have  one  PK  in  a  table,  but  you  may  have  several  FK’s.  

Column  Names  

Example:  

Page 18: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

18  

Database  Design  –  ER  Diagram  Example  I  PK  

PK  

PK  

PK  

PK  

PK  

FK  

FK  FK  

FK  FK  FK  

PK  

PK  

FK  FK  

FK  FK  

PK-­‐FK  RelaSonship  

PK-­‐FK  RelaSonships  

Table  Name  

Table  Name  

Table  Name  

Table  Name  Table  Name  

Table  Name  

Table  Name  Table  Name  

PK  –  Primary  Key,  FK  –  Foreign  Key  

This  example  stores  all  informa,on  about  the  students  grade,  etc.  in  the  database  

Page 19: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

19  

Database  Design  –  ER  Diagram  Example  II  Library    Management    System  

Page 20: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Database  Design  –  MicrosoX  Visio  

20  

1

2

3

We  will  use  Visio  to  Design  our  Database  

Select  the  proper  Template  

Page 21: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Visio  -­‐  Example  

21  

Use  the  “En,ty”  Tool    in  order  to  create  Tables  

Use  the  “Rela,onship”  Tool    in  order  to  create  Primary  Key  –  Foreign  Key  Rela,onship  between  two  tables  (lines  with  arrows)  

Table  Name  Primary  Key  (PK)  

Foreign  Key  (FK)  

Page 22: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Database  -­‐  “Best  PracSce”  

22  

•  Tables:  Use  upper  case  and  singular  form  in  table  names  –  not  plural,  e.g.,  “STUDENT”  (not  students)  

•  Columns:  Use  Pascal  nota,on,  e.g.,  “StudentId”  •  Primary  Key:    •  If  the  table  name  is  “COURSE”,  name  the  Primary  Key  column  “CourseId”,  etc.  

•  “Always”  use  Integer  and  Iden,ty(1,1)  for  Primary  Keys  

•  Specify  Required  Columns  (NOT  NULL)  –  i.e.,  which  columns  that  need  to  have  data  or  not  

•  Standardize  on  these  Data  Types:  int,  float,  varchar(x),  date2me,  bit  

It  is  recommended  that  you  follow  these  guidelines!  

Page 23: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

4.  SQL  Server  

23  

Page 24: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

MicrosoX  SQL  Server  –  Create  a  New  Database  

24  

1

2

Name  you  database,  e.g.,  WEATHER_SYSTEM  

Page 25: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

MicrosoX  SQL  Server  

25  

1

2

3

4

5

Write  your  Query  here  

The  result  from  your  Query  

Your  Database  

Your  Tables  

Your  SQL  Server  

Page 26: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

26  

MicrosoX  SQL  Server  

Make  sure  to  uncheck  this  op,on!  

Do  you  get  an  error  when  trying  to  change  your  tables?  

Page 27: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

SQL  –  Structured  Query  language  

27  

•  insert into STUDENT (Name , Number, SchoolId) values ('John Smith', '100005', 1)

•  select SchoolId, Name from SCHOOL

•  select * from SCHOOL where SchoolId > 100

•  update STUDENT set Name='John Wayne' where StudentId=2

•  delete from STUDENT where SchoolId=3

A  Database  Computer  Language  designed  for  Managing  Data  in  Rela,onal  Database  Management  Systems  (RDBMS)  

Query  Examples:  

We  have  4  different    Query  Types:  INSERT,  SELECT,  UPDATE  and  DELETE  

Page 28: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Create  Tables  using  SQL  

28  

if not exists (select * from dbo.sysobjects where id = object_id(N'[SCHOOL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

CREATE TABLE [SCHOOL] (

[SchoolId] [int] IDENTITY(1, 1) NOT NULL PRIMARY KEY, [SchoolName] [varchar](50) NOT NULL UNIQUE, [Description] [varchar](1000) NULL, [Address] [varchar](50) NULL, [Phone] [varchar](50) NULL, [PostCode] [varchar](50) NULL, [PostAddress] [varchar](50) NULL,

) GO

if not exists (select * from dbo.sysobjects where id = object_id(N'[CLASS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

CREATE TABLE [CLASS] (

[ClassId] [int] IDENTITY(1, 1) NOT NULL PRIMARY KEY, [SchoolId] [int] NOT NULL FOREIGN KEY REFERENCES [SCHOOL] ([SchoolId]), [ClassName] [varchar](50) NOT NULL, [Description] [varchar](1000) NULL,

) GO

Page 29: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Create  Tables  using  the  Designer  Tools  in  SQL  Server  

29  

Even  if  you  can  do  “everything”  using  the  SQL  language,  it  is  some,mes  easier  to  do  something  in  the  designer  tools  in  the  Management  Studio  in  SQL  Server.  Instead  of  crea,ng  a  script  you  may  as  well  easily  use  the  designer  for  crea,ng  tables,  constraints,  inser,ng  data,  etc.  

Select  “New  Table  …”:   Next,  the  table  designer  pops  up  where  you  can  add  columns,  data  types,  etc.  

1 2

In  this  designer  we  may  also  specify  constraints,  such  as  primary  keys,  unique,  foreign  keys,  etc.      

Page 30: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Create  Tables  with  the  “Database  Diagram”  

30  

3

4

5

1 2

You  may  select  exis,ng  tables  or  create  new  Tables  

Create  New  Table  

Enter  Columns,  select  Data  Types,    Primary  Keys,  etc.  

Page 31: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Get  Data  from  mulSple  tables  in  a  single  Query  using  Joins  

31  

select  SchoolName,    CourseName  from  SCHOOL    inner  join  COURSE  on  SCHOOL.SchoolId  =  COURSE.SchoolId    

Example:  

You  link  Primary  Keys  and  Foreign  Keys  together  

Page 32: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

CreaSng  Views  using  SQL  

32  

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CourseData' AND type = 'V') DROP VIEW CourseData

GO CREATE VIEW CourseData AS SELECT SCHOOL.SchoolId, SCHOOL.SchoolName, COURSE.CourseId, COURSE.CourseName, COURSE.Description FROM SCHOOL INNER JOIN COURSE ON SCHOOL.SchoolId = COURSE.SchoolId GO

select * from CourseData

You  can  Use  the  View  as  an  ordinary  table  in  Queries  :  

A  View  is  a  “virtual”  table  that  can  contain  data  from  mul,ple  tables  

Inside  the  View  you  join  the  different  tables  together  using  the  JOIN  operator  

The  Name  of  the  View  

Create  View:  

Using  the  View:  

This  part  is  not  necessary  –  but  if  you  make  any  changes,  you  need  to  delete  the  old  version  before  you  can  update  it  

Page 33: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

CreaSng  Views  using  the  Editor  

33  

1

2

3

4Add  necessary  tables  

Save  the  View  

Graphical  Interface  where  you  can  select  columns  you  need  

Page 34: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Stored  Procedure  

34  

IF  EXISTS  (SELECT  name            FROM      sysobjects            WHERE    name  =  'StudentGrade'            AND        type  =  'P')    DROP  PROCEDURE  StudentGrade  

OG    CREATE  PROCEDURE  StudentGrade  @Student  varchar(50),  @Course  varchar(10),  @Grade  varchar(1)      AS    DECLARE  @StudentId  int,  @CourseId  int    select  StudentId  from  STUDENT  where  StudentName  =  @Student    select  CourseId  from  COURSE  where  CourseName  =  @Course    insert  into  GRADE  (StudentId,  CourseId,  Grade)    values  (@StudentId,  @CourseId,  @Grade)  GO  

execute StudentGrade 'John Wayne', 'SCE2006', 'B'

A  Stored  Procedure  is  like  Method  in  C#  -­‐  it  is  a  piece  of    code  with  SQL  commands  that  do  a  specific    task  –  and  you  reuse  it  

Input  Arguments  

Internal/Local    Variables  

Procedure  Name  

SQL  Code  (the  “body”  of  the  Stored  Procedure)  

Note!  Each  variable  starts  with  @  

Create  Stored  Procedure:  

Using  the  Stored  Procedure:  

This  part  is  not  necessary  –  but  if  you  make  any  changes,  you  need  to  delete  the  old  version  before  you  can  update  it  

Page 35: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Trigger  

35  

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CalcAvgGrade' AND type = 'TR') DROP TRIGGER CalgAvgGrade

GO CREATE TRIGGER CalcAvgGrade ON GRADE FOR UPDATE, INSERT, DELETE AS DECLARE @StudentId int, @AvgGrade float select @StudentId = StudentId from INSERTED select @AvgGrade = AVG(Grade) from GRADE where StudentId = @StudentId update STUDENT set TotalGrade = @AvgGrade where StudentId = @StudentId GO

A  Trigger  is  executed  when  you  insert,  update  or  delete  data  in  a  Table  specified  in  the  Trigger.  

Inside  the  Trigger  you  can  use  ordinary  SQL  statements,  create  variables,  etc.  

Name  of  the  Trigger  

Specify  which  Table  the  Trigger  shall  work  on  

Internal/Local    Variables  

SQL  Code    (The  “body”  of  the  Trigger)  

Specify  what  kind  of  opera,ons  the  Trigger  shall  act  on  

Note!  “INSERTED”  is  a  temporarily  table  containing  the  latest  inserted  data,  and  it  is  very  handy  to  use  inside  a  trigger  

Create  the  Trigger:  This  part  is  not  necessary  –  but  if  you  make  any  changes,  you  need  to  delete  the  old  version  before  you  can  update  it  

Page 36: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

5.  LabVIEW  

36  

Page 37: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Weather  System  in  LabVIEW  

37  

App  #1  Logg  data  from  TC-­‐01  Device  and  save  the  data  into  the  database  

GUI/HMI  Example:  

Code  Example:  

Page 38: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

DAQ  Assistant  for  NI  USB-­‐TC01  

38  

1

2

3

4

5

Acquire  Signals  »  Analog  Input  »  Temperature  »  Thermocouple  

Select  Physical  Channel  ai0  

Use  default  Proper,es  

Click  OK  to  Finish  

Page 39: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Weather  System  in  LabVIEW  

39  

App  #2  Present  weather  data  from  the  database  “con,nuously”  

GUI/HMI  Example:  

Page 40: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Weather  System  in  LabVIEW  

40  

App  #3  Manage  Weather  Parameters  (Create  New,  Edit  exis,ng  or  Delete  exis,ng  Parameters)    

GUI/HMI  Example  

GUI/HMI  Example  

Page 41: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

ODBC  

41  

ODBC  (Open  Database  Connec,vity)  is  a  standardized  interface  (API)  for  accessing  the  database  from  a  client.  You  can  use  this  standard  to  communicate  with  databases  from  different  vendors,  such  as  Oracle,  SQL  Server,  etc.  The  designers  of  ODBC  aimed  to  make  it  independent  of  programming  languages,  database  systems,  and  opera,ng  systems.    

Control  Panel  →  Administra,ve  Tools  →  Data  Sources  (ODBC)  

We  will  use  this  ODBC  Connec,on  later  in  LabVIEW  in  order  to  open  the  Database  Connec,on  from  LabVIEW  

Page 42: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

ODBC  –  Step  by  Step  InstrucSons  

42  

The  Name  of  your  SQL  Server  

The  Name  of  your  ODBC  Connec,on  

Use  either  Windows  or  SQL  Server  authen,ca,on  (Windows  is  simplest  to  use!)    

Select  the  Database  you  are  using  for  the  Library  

Test  your  connec,on  to  see  if  its  works  

Page 43: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

SQL  Server  Configura,on  Manager  

43  

Make  sure  to  Enable  these  Protocols!  

If  not  the  Database  Communica,on  from  LabVIEW  will  not  work  properly!  

Page 44: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

MicrosoX  Excel  -­‐  Example  

44  

1

3

2

45

Select  your  ODBC  connec,on  

Finally,  the  data  from  the  database  is  in  the  Excel  sheet    

Select  Tables  and  Columns  

Page 45: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

LabVIEW  SQL  Toolkit  

45  

Example  1:  Get  Data  from  Database  into  LabVIEW:  

Example  2:  Write  Data  to  Database  from  LabVIEW:  

Easy  Access  to  Database  Systems  from  LabVIEW  

1 2   3

2 31

Your  ODBC  Connec,on  

Page 46: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

LabVIEW  SQL  Toolkit  

46  

If  we  want  to  save  input  data  from  the  user  we  can  use  the  “Format  Into  String”  func,on  

execute  CreateBook  ‘Lord  of  the  Rings',  ‘J.R.R.  Tolkien',  Wiley',  ‘32-­‐2-­‐333-­‐56',  Fantasy'  

The  %s  operator  will  be  replaced  by  the  text  from  the  TextBox  on  the  Front  Panel.  For  Numbers  we  can  use  %d  (Integer)  or  %f  for  Floa,ng-­‐point  Number.  

Resul,ng  SQL  Query:  

1  

2

3

GUI/HMI  

Code:  

Page 47: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

LabVIEW  Example  

47  

This  example  shows  is  a  Library  Management  System,  but  a  system  for  manipula,ng  Weather  Parameters  will  work  the  same  way  

Mul,column  Listbox  

Menu  Ring  

The  following  Window  (SubVI)  Pops  up  when  clicking  the  “Add  Book”  BuZon  

Show  the  Books  available  in  the  Database  using  a  View  

Insert  the  Book  informa,on  in  the  Database  using  a  Stored  Procedure  

Update  the  List  with  the  latest  informa,on  from  the  Database  

1

3

42 Add  more  Books  

Page 48: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

LabVIEW  Block  Diagram  –  State  Machine  

48  

Download  the  Example  Code  and  use  the  following  Example:    

ShiR  Register  

Error  Handling  using  an  Error  Clusters  and  ShiR  Register  

When  you  click  on  the  different  BuZons,  etc.,  the  code  inside  the  Event  Structure  will  be  automa,cally  executed  

The  Programming  Technique  shown  above  is  called  a  State  Machine  and  it  is  recommended  that  you  use  this  method!  

Page 49: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Grading/Submission  -­‐  Checklist  

49  

The  Report  in  PDF  format  is  included  in  the  ZIP  File  

The  LabVIEW  Code  is  included  in  the  ZIP  File  

The  ER  diagram  in  Visio  is  included  in  the  ZIP  File  

The  SQL  Code/SQL  Scripts  (Views,  Triggers,  Stored  Procedure,  etc.)  are  included  in  the  ZIP  File  

Submission  in  Fronter  (one  .ZIP  File  –  Not  .RAR!!!!!!)  

A  Printed  Copy  of  the  Report  is  in  the  Mail  Box  outside  my  Office  

Note!  The  Database  Lab  counts  25%  of  the  final  grade  in  the  course  

Page 50: Database’Lab’–Tips’&’Tricks’home.hit.no/~hansha/documents/lab/Lab Work/Database/Background... · Database’Lab’–Tips’&’Tricks ... • Oracle • MySQL ... •

Hans-­‐Pe2er  Halvorsen,  M.Sc.  Telemark  University  College  Faculty  of  Technology  Department  of  Electrical  Engineering,  InformaSon  Technology  and  CyberneScs  

   E-­‐mail:  [email protected]  Blog:  h2p://home.hit.no/~hansha/    

50