From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Database Connectivity Toolkit (multi row insert)

Solved!
Go to solution

Hi all. I recently got the Database Connectivity Toolkit (DBCT) on LabVIEW 2016 and I'm trying to insert multiple rows into a database.

 

Using a sine signal input I attempted to insert 50 rows in one iteration, but every method I tried returned 50 values in one row. Using the end of line constant and array to spreadsheet string, returned what looked like 50 rows, but when I queried "SELECT COUNT(*) FROM.." the no of rows was equal to the number of iterations.

 

On another forum post, someone suggested it is not possible to insert multiple rows using the DBCT. Can anyone confirm this?

 

I have written some code which allows me to insert 3 rows in one iteration, under a single connection. However, this method is very tedious and wouldn't work for large amounts of data.

 

DB Connectivity (Multi Row Insert).png

 

Can anyone think of a better way to do this?

 

Thanks in advance,

 

Lisa

0 Kudos
Message 1 of 31
(6,053 Views)
Solution
Accepted by topic author lisam88

Build your own query and use the execute query VI. 

Example_VI_BD.png

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 2 of 31
(6,019 Views)

Also, you misunderstand what "1 row" means on your returned results.  That doesn't mean there were 1081 entries in one row.  Your query was to return the count of rows in the table.  The count is the one result.  That doesn't mean there is only one row. 

 

You should consider some Labview and SQL tutorials before getting too far into this.  A couple of things to note:

  • The sequence structure is not needed.  Think dataflow.  If you don't know what that means, there is a lot of information about it.  Labview is built around dataflow.
  • Don't convert an array to a cluster in order to index the data with an unbundle node.  Use the index array VI or use a for loop to autoindex the data.
  • It doesn't make sense to generate a sine wave and then only save 3 points from the wave.  I'm not sure of your intent here.  And it certainly doesn't make sense to write each point along the sine wave to its own row within the database. 
aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 3 of 31
(6,006 Views)

On another forum post, someone suggested it is not possible to insert multiple rows using the DBCT. Can anyone confirm this?

 

I can confirm that it definitely is possible. The way I did it was to build a parametric query (using ? as placeholders) where I repeat the VALUES part multiple times (depending on SQL server - e.g. "INSERT INTO t1 (name, value, timestamp) VALUES (?,?,?),(?,?,?)" ) and then assign the parameters in a for loop (as it's better than just writing the values in the query as it stops you from accidentally sending a string that could wipe your DB!) and then finally execute as a single query.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 4 of 31
(5,999 Views)

hi aputman,

 

Thanks for your useful tips and suggestions.

 

The row count I understand. In this case the loop did 360 iterations and three values were inserted in each iteration which gives a total number of 1081 rows. Which is what I was looking for. Previously I created a model with a sine wave generating 50 samples per iteration. I did this example with the system exec.vi and 50 samples with 14 iterations then returned 700 rows. Using the connectivity toolkit a 50 sample sine wave only returned 879 rows in 879 iterations - so 50 values are on the same row.

 

In the above example, I only plotted 3 points from the wave in attempting to insert multiple rows. As the only method I could come up with was inserting the values into separate insert functions, I only did three to see if it worked. But of course this is not a very effective method. This is just a basic model to see how I can generate data and insert them into a postgreSQL database. In the application it will be used for, it will require each piece of data to be inserted in different rows and columns. This is why I'm trying to avoid having multiple data points on the same row. 

 

Thanks again.

Lisa

 

 

0 Kudos
Message 5 of 31
(5,987 Views)

Hi Sam, thanks for your comment. I had a look at the LabVIEW Database Insert.vi example. I attempted the example to insert data into my database. But I get the error:

"Error 91 ocurred at NI Database_API.lvlib:Cmd Set Parameter Value (C).vi-> Database Insert.vi

Possible reason(s):

ADO Error: 0x0000005B

Variant To Data in NI_Database_API.lvlib:CMd Set Parameter Value (C).vi->Database Insert.vi"
Can this also work with a sine wave input?

 

Database Insert vi.png

 

0 Kudos
Message 6 of 31
(5,965 Views)

Just tried the query you suggested and it works perfectly. Thank you!

0 Kudos
Message 7 of 31
(5,957 Views)

Hey,

I've tried your solution for my case but I'm getting a syntax error and I can figure out why.

Do you have any ideas about this ?

Thank you in advance;

0 Kudos
Message 8 of 31
(5,039 Views)

I can't read the error message but I'm fairly certain that it has to do with putting single quotes around the float value.  This is only necessary for strings and datecodes.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 9 of 31
(5,034 Views)

Hello,

Thank's for your reactivity.

I removed them but I'm getting the same error.

0 Kudos
Message 10 of 31
(5,028 Views)