PostgreSQL large object problem

This is a discussion on PostgreSQL large object problem within the C Programming forums, part of the General Programming Boards category; I want to create a database of images, the table is like this: Code: +--------------+ | user_image | +--------------+ | ...

  1. #1
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82

    PostgreSQL large object problem

    I want to create a database of images, the table is like this:
    Code:
    +--------------+
    | user_image   |
    +--------------+
    | id (integer) |
    | img (oid)    |
    +--------------+
    I created a GUI application that displays an image from a webcam,
    so when I push the button the display will freeze and the app will
    insert the image data to the database and the id will be
    incremented automatically (id starts at 0).
    how can I insert the data?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cph View Post
    the id will be
    incremented automatically (id starts at 0).
    You should just set id to the PostgreSQL equivalent of INTEGER PRIMARY KEY AUTOINCREMENT -- and id will start at 1 not 0.

    You could write the image to a file and put the filepath into the db rather than the image itself.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Just use bigserial as the column type for your primary key. bigserial types are incremented automatically, so you don't need to adjust them. Just do:
    INSERT INTO user_image (img) VALUES (img_value);
    The id will be inserted for you.

  4. #4
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    I've tried to write something like this
    Code:
    #include <libpq-fe.h>
    #include <libpq/libpq-fs.h>
    #include <stdio.h>
    
    int main (void)
    {
        Oid    lobjId;
        PGconn *conn = PQsetdb(NULL, NULL, NULL, NULL, "cpns");
    
        if (conn != NULL) {
            PGresult *result = PQexec(conn, "begin");
    
            PQclear(result);
            lobjId = lo_creat(conn, INV_WRITE | INV_READ);
            if (lobjId == 0) {
                printf("Unable to create large object\n");
            } else {
                printf("Large object successfully created\n");
            }
            PQexec(conn, "end");
            PQfinish(conn);
        }
    
        return(0);
    }
    it compiles fine, but when I try to execute it, an error occurred.
    what's wrong with it?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    it compiles fine, but when I try to execute it, an error occurred.
    Please be a little more specific. What error?

  6. #6
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    I'm sorry, actually it crashes immediately.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cph View Post
    I'm sorry, actually it crashes immediately.
    What did it hit?

    Quote Originally Posted by bithub View Post
    Please be a little more specific.
    So by "crash" you mean seg fault, or making a sudden loud noise?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    a seg fault

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> actually it crashes immediately

    I've never used this API before, but one thing I noticed is that you aren't checking to see if 'result' is NULL.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    One more thing, it looks like the proper way to check the return value of PQsetdb is:

    Code:
    if (PQstatus(conn) != CONNECTION_BAD)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    I've modified the source code to check the return value, but it's still not working
    well, I guess I can't insert the actual image data

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  2. string object problem
    By ssharish2005 in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:21 AM
  3. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  4. deleting dynamic object problem
    By eth0 in forum C++ Programming
    Replies: 17
    Last Post: 05-19-2004, 01:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21