Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Closed Thread
 
Thread Tools Display Modes
Old Aug 27th, 2009, 6:51 AM   #1
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Sqlite Starter Program

This is a little program I worked up to get started with Sqlite. Hope it helps someone someday.
// Before you compile this program you have to 
// dowload the Sqlite source code and header files from 
// http://www.sqlite.org/download.html
// Then you have to compile the sqlite3.c program
// along with this one and link the two object files
// together as a single program.  Or you could compile sqlite3.c
// into a library and link this program with that library.

#include <iostream>
#include <string>
#include "sqlite3.h"

// Delete this pragma if you are not using
// a Microsoft compiler.
#pragma warning(disable: 4996)

using std::cout;
using std::cin;
using std::string;

// Function prototypes
void fill(sqlite3* db, string tbname);
void CreateDatabase(sqlite3** db);
void CreateTable(sqlite3* db, string& tbname);
void display(sqlite3* db, string tbname);


// This is a callback function that is called by Sqlite to process the 
// resultset of a query.  There are other ways to do it without using
// a callback function, such this is the simplest to implemnent.
//
// Parameters are:
//      argc -- the number of rows in the resultset
//      argv[] -- the data for each row
//      azColName -- the name of each column
int callback(void *NotUsed, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i<argc; i++){
    cout << azColName[i] 
          << " = ";
    if( argv[i] )
        cout << argv[i];
    else
        cout << "NULL";
     cout << "\n";
  }
  return 0;
}

// This function just display an error message that
// may have been returned by the various Sqlite functions.
void dsperr(char**db_err)
{
    if( *db_err )
    {
        cout << *db_err << "\n";
        sqlite3_free(*db_err); // release (free) memory
        *db_err = 0; // reinitialize the caller's pointer
    }
}

// This function is called from several places to get 
// the table name.  
void GetTablename(string& tbname)
{
    cout << "Enter the table name\n";
    cin >> tbname;
}


int main()
{
    sqlite3* db = 0; // database connection
    string tbname;   // table name
    bool done = false;
    while(!done)
    {
        string answer;
        cout << "\n\nPlease select one of the folowing options\n";
        cout << "1. Create a new or open an existing database\n";
        cout << "2. Create a new table\n";
        cout << "3. Add some data\n";
        cout << "4. List all data\n";
        cout << "5. Quit\n";
        cin >> answer;
        cin.ignore();
        switch(answer[0])
        {
            case '1':
                CreateDatabase(&db);
                break;
            case '2':
                CreateTable(db, tbname);
                break;
            case '3':
                fill(db, tbname);
                break;
            case '4':
                display(db, tbname);
                break;
            case '5':
                done = true;
                break;
        }

    }
    sqlite3_close(db);
}

// Create a new or open an existing database.
// If the database does not exist it will be
// created.  Note that you can specif the full
// path and filename which may, or may not, contain spaces.
void CreateDatabase(sqlite3** db)
{
    string dbname;
    cout << "Please enter the name of the database\n";
    getline(cin, dbname);
    int n = sqlite3_open(dbname.c_str(), db);
    if( n != SQLITE_OK )
    {
        cout << "Error opening database.\n";
    }
    else
        cout << "Database open ok\n";
}

// Add a new table to the database.  If the table
// name already exists then an error message will be
// displayed.
void CreateTable(sqlite3* db, string& tbname)
{
    char* db_err = 0;
    string statement;
    GetTablename(tbname);
    if( tbname == "" )
        return;
    statement = "CREATE TABLE " + tbname + " (id integer);";

    int n = sqlite3_exec(db, statement.c_str(), NULL, 0, &db_err);
    dsperr(&db_err);
    if( n != SQLITE_OK )
    {
        cout << "Error executing \"" << statement << "\"\n";
    }
    else
        cout << "Table created successfully\n";

}


// Just dump some data into the table.  You don't
// have a choice of the kind or quantity of data to be entered.
//
void fill(sqlite3* db, string tbname)
{
    char* db_err = 0;
    if( tbname.length() == 0)
    {
        GetTablename(tbname);
        if( tbname == "" )
            return;
    }
    for(int i = 1; i < 100; i++)
    {
        char buf[80];
        sprintf(buf,"insert into %s values(%d);", tbname.c_str(), i);
        int n = sqlite3_exec(db, buf, NULL, 0, &db_err);
        dsperr(&db_err);
        if( n != SQLITE_OK )
        {
            cout << "Error inserting value " << i << "\n";
            break;
        }
    }

}

// Query the database for all the data in the table and
// display it in the callback function.  
void display(sqlite3* db, string tbname)
{
    char* db_err = 0;
    string select;
    if( tbname.length() == 0)
    {
        GetTablename(tbname);
        if( tbname == "" )
            return;
    }
    if( tbname.length() > 0)
    {
        select = "select * from " + tbname + ";";
        sqlite3_exec(db, select.c_str(), callback, 0, &db_err);
        dsperr(&db_err);
    }

}
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline  
Old Aug 27th, 2009, 10:28 AM   #2
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 13 Arla is on a distinguished road
Re: Sqlite Starter Program

Thanks for this Ancient, definitely might be something I'll be perusing.

Out of interest (and knowing little about C++) why is sqlite3.h in quotes, but the rest in <> brackets?
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline  
Old Aug 27th, 2009, 10:51 AM   #3
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 13 Arla is on a distinguished road
Re: Sqlite Starter Program

Also note I had to add

#include <cstdio>

to get it to compile as a C++ program, not sure if it's a C++ or C program (I'm not yet cognizant enough of the differences to know)
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline  
Old Aug 27th, 2009, 11:38 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 21 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: Sqlite Starter Program

Quote:
why is sqlite3.h in quotes, but the rest in <>
That's very basic C/C++. Have a look here.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline  
Old Aug 27th, 2009, 1:05 PM   #5
CoderGuru
Professional Programmer
 
Join Date: Jan 2009
Location: Dallas, Texas, USA
Posts: 328
Rep Power: 9 CoderGuru is on a distinguished road
Re: Sqlite Starter Program

In your experience how stable is SQLite? I just started to use it but before I deploy it with companies who spend 10,000 a year, as apposed to having to install SQL server each time, I wanna make sure its extremely stable (database doesn't corrupt every).

Thanks for the advice.
CoderGuru is offline  
Old Aug 27th, 2009, 6:45 PM   #6
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: Sqlite Starter Program

Quote:
Originally Posted by CoderGuru View Post
In your experience how stable is SQLite? I just started to use it but before I deploy it with companies who spend 10,000 a year, as apposed to having to install SQL server each time, I wanna make sure its extremely stable (database doesn't corrupt every).

Thanks for the advice.
IMO I would not use it in a commercial production environment because its just too slow, and probably not multi-user safe. As far as the code goes, its been around for quite a few years, so it should be pretty stable.

If you have a large number of users accessing the database at the same time, then use something like MySQL (free).
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline  
Old Aug 27th, 2009, 6:53 PM   #7
CoderGuru
Professional Programmer
 
Join Date: Jan 2009
Location: Dallas, Texas, USA
Posts: 328
Rep Power: 9 CoderGuru is on a distinguished road
Re: Sqlite Starter Program

Anchient, It wouldn't be to much concurrency as we do a lot of Kios applications where they enter their contact and it simply dumps to a database, then behind the scenes every 5 minutes syncs to my server. so in reality even if we have at most the 30 second window you get on a web request is enough for it to release the process. I am just trying to avoid always setting up a new database / sql server on the boxes for our clients. Plus its a lot easier to backup the database file as it is a SQL database (we use msSQL, not mySQL).

As of right now it has been running real stable on the small campaign apps we developed as testing, but I don't want to put it into a production environment till I know for sure the database wont just corrupt or anything.

Thank you for the feedback though. I appreciate it.
CoderGuru is offline  
Old Aug 27th, 2009, 6:56 PM   #8
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: Sqlite Starter Program

I really don't know the answer to your question. You could join their email list and ask them.
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline  
Old Aug 27th, 2009, 7:11 PM   #9
CoderGuru
Professional Programmer
 
Join Date: Jan 2009
Location: Dallas, Texas, USA
Posts: 328
Rep Power: 9 CoderGuru is on a distinguished road
Re: Sqlite Starter Program

K thanks again
CoderGuru is offline  
Old Sep 24th, 2009, 8:33 AM   #10
Tajn
Newbie
 
Join Date: Sep 2009
Posts: 3
Rep Power: 0 Tajn is on a distinguished road
Re: Sqlite Starter Program

hello!

I'm stuck even before the program itself- I'm stuck with comment:

Before you compile this program you have to
// dowload the Sqlite source code and header files from
// http://www.sqlite.org/download.html
// Then you have to compile the sqlite3.c program
// along with this one and link the two object files
// together as a single program. Or you could compile sqlite3.c
// into a library and link this program with that library.


i have downloaded sqlite3 and added sqlite3.c, sqlite3.h, sqlite3ext.h to the source files of the program, but i dont know how to link the two object files
together as a single program, niether how I you should compile sqlite3.c
into a library and link this program with that library..

And also, how can I see which compiler I have?

thank you
Tajn is offline  
Closed Thread

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with input of a program. lrh9 C++ 7 May 29th, 2009 12:36 AM
Will pay for LC-3 assembly language program help lilfindley Paid Job Offers 0 Mar 17th, 2009 10:24 PM
Language display in program Prm753 C++ 3 May 30th, 2006 5:45 PM
Creating a program to test a program sixstringartist C 8 Jan 21st, 2006 1:15 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC