Thread: SQLite db

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Thumbs up SQLite db


    Hello,

    I am supposed to write a program that based on the input creates or opens a database and updates it.
    We use only simple input and I am able to recognize, what the user wants to do, but the problem comes when I try to open/create a db and write into it. We should use SQLite3 and yes, I have read the documentation, but it does not seem to help me.

    I use the sqlite3.h header.
    Now I would like to create a database and write into it and I donīt even know where to begin. If anyone could point me in the right direction, I would be very grateful. Thanks

    P.S.: I know SQL, just donīt know how to use it here...
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,896
    Rep Power
    481
    Having written a program that connects to oracle, and uses the programming interface, I'll suggest a different method. Run sqlite3 in a separate process and communicate with it using the sql commands you already know. The trick will be to parse sqlite3 output so that you know when it's ready for the next command. I don't have time now to provide an example.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You need to visit a sqllite forum.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    You first have to open the database or create it if it doesn't exist. The sqlite3_open function will do that for you. db is an example of the SQLite db handle; Check the return code to verify that the function executed successfully.

    Code:
    sqlite3* db;
    
    int sqlite3_open(
      const char *filename,   /* Database filename (UTF-8) */
      sqlite3 **ppDb          /* OUT: SQLite db handle */
    );
    Since you already know SQL, write a SQL statement to create your table and use sqlite3_exec to execute the SQL statement.

    Next build a SQL statement to insert you data into the table and again use sqlite3_exec to execute that SQL statement.

    Finally, use sqlite3_close(db) to close the database

IMN logo majestic logo threadwatch logo seochat tools logo