Database package: Difference between revisions

1,590 bytes added ,  23 April 2020
no edit summary
No edit summary
No edit summary
Line 36: Line 36:
===Connecting to a Database===
===Connecting to a Database===


The user running octave must have permissions to access the pg db, or you can pass the username and pass via pg_connect. Here is an example where the user has access to pg without passing credentials.
conn = pq_connect (setdbopts ("dbname", "mydatabase"));
Here, you are specifying a key-value pair. So you want to fill in a key (dbname), and that dbname is mydatabase.
Now, you are ready to read the db:
N = pq_exec_params (conn, "select * from Table1;")
Values are returned in a struct, e.g.:
N =


  scalar structure containing the fields:


    data =
    {
      [1,1] = 3895
      [2,1] = 3942
      [3,1] = 3919
      [4,1] = 3866
      [5,1] = 3923
      [6,1] = 3969
      [1,2] = 1324612180367
      [2,2] = 1324612180369
      [3,2] = 1324612188073
      [4,2] = 1324612190313
      [5,2] = 1324612191841
      [6,2] = 1324612192922
    }
    columns =
    {
      [1,1] = value
      [1,2] = unixtime
    }
    types =
      1x2 struct array containing the fields:
        name
        is_array
        is_composite
        is_enum
        elements
Display specific elements e.g.:
display(N.data)
Convert to a format which can be plotted/graphed on an x,y axis:
p = cell2mat (N.data)
Graph:
plot (p(:, 2), p(:, 1))
==Tips/Tricks==
===Database Cell Types===
Note that you must have all database cells be the same type in order for the values to not get potentially changed. In the example above, if you have the UnixTime column above be a bigint, and the Value column be an Int, the UnixTime will get truncated. Changing the Value column to a bigint will resolve this.




14

edits