Re: [sqlite] sample code in C++ to get the data from sqlite database loop thru the record set

2007-12-18 Thread John Stanton

Here is a code fragment in C.  You add the C++.

Joanne Pham wrote:

Hi All,
If you have the sample code in C++ to run the command "Select column1, column2 from 
tablea" from C++ code and able to loop thru the record set to get each rows please 
share with me.
Thanks in Advance,
Joanne


  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping


/*Code fragment to compile an SQL statement and read the result set.
this does not have code to handle an altered schema.*/

  int  col_cnt; 
  sqlite3  *db;  
  int  rc;
  cosnt char   *sqlst_end;
  sqlite3_stmt *sqlst;
  
  /*Compile the SQL.*/
  rc = sqlite3_prepare_v2(db, sqstmnt, -1, &sqst, &sqst_end);
  if (rc != SQLITE_OK) {
wrapup("9: SQL compile error");
return(TRUE);
  }  /*if*/

  /*Execute the SQL.  It could be busy.  Count busy polls to break 
  a deadlock.  When busy reset the current step, pause and relaunch it.*/

  /*Count columns in statement and access declared types.*/
  col_cnt = sqlite3_column_count(sqst);

  finished = FALSE;
  while (!finished) {
rc = sqlite3_step(sqst);
switch (rc) {
  case SQLITE_DONE: /*Execution finished.*/
finished = TRUE;
sqlite3_reset(sqst);  /*Resets the compiled statement for re-use.*/

/*Execute logic for end of data set.*/
/*!!!*/
break;
  case SQLITE_ROW:  /*We have a row.*/
if (rowcount == 0) {
  /*Execute code for start of data set*/
  /**/
}

/*Scan all the columns.  Return value in "strg"*/
for (a = 0; a < col_cnt; a++) {
  /*Get actual type.*/
  switch (sqlite3_column_type(sqst, a)) {
case SQLITE_INTEGER:
  result = sqlite3_column_int(sqst, a);
  sprintf(strg, "%d", result);
  break;
case SQLITE_FLOAT:
  {
/*Float to string.*/
double  dub;
dub = sqlite3_column_double(sqst, a);
sprintf(strg, "%f", dub);
  }
  break;
case SQLITE_TEXT:
  vp = sqlite3_column_text(sqst, a);
  p = strg;
  while (*vp > 0) *p++ = *vp++;
  break;
case SQLITE_BLOB:
  /*Write to a file, dynamic memory ...*/
  break;
case SQLITE_NULL:
  strg[0] = 0;
  break;
  }  /*switch*/
}/*for*/

rowcount++;
break;
  case SQLITE_BUSY:  /*DB is busy, try again*/
int errc = sqlite3_reset(sqst);
if (busy_count++ == MAX_BUSY_COUNT) {
  sqlite3_finalize(sqst);
  wrapup("9: Database Deadlocked");
  break;
}  /*if*/

/*To make the polling less intensive give up the time slice.*/
#if IS_WIN32
sleep(0);  /*Win32 has a different yield call.*/
#else
yield();   /*Relinquish time slice to limit overhead of polling.*/
#endif
break;
  default:/*A nasty error.*/
sqlite3_finalize(sqst);
wrapup("9: Fatal SQL EXEC error");
break;
}  /*switch*/
  }/*while*/

  /*This will clear the compiled statement so that the DB can be closed.*/
  sqlite3_finalize(sqst);

-
To unsubscribe, send email to [EMAIL PROTECTED]
-

[sqlite] sample code in C++ to get the data from sqlite database loop thru the record set

2007-12-18 Thread Joanne Pham
Hi All,
If you have the sample code in C++ to run the command "Select column1, column2 
from tablea" from C++ code and able to loop thru the record set to get each 
rows please share with me.
Thanks in Advance,
Joanne


  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping

Re: [sqlite] sample code

2007-12-11 Thread Clay Dowling

Tom Parke wrote:
> Where can I find some sample C code for stepping thru a result set and
> for binding variables to columns?  I am just beginning to experiment
> with Sqlite3 and I am having a hard time getting aquainted.

http://www.lazarusid.com/sqlite3

>From a Linux Journal article several years ago, when SQLite 3 first came out.

Clay

-- 
Lazarus Registration
http://www.lazarusid.com/registration.shtml


-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] sample code

2007-12-11 Thread Tom Parke
Where can I find some sample C code for stepping thru a result set and
for binding variables to columns?  I am just beginning to experiment
with Sqlite3 and I am having a hard time getting aquainted.

Thanks,

Tom



Re: [sqlite] Sample code

2005-10-26 Thread Alfredo Cole
El Miércoles, 26 de Octubre de 2005 16:17, John Stanton escribió:
 > Alfredo, this simple example may may help
 >
(...)

This is good! Thank you and regards.

-- 
Alfredo J. Cole
Grupo ACyC


Re: [sqlite] Sample code

2005-10-26 Thread John Stanton

Alfredo, this simple example may may help

/*These global definitions are the uncompiled SQL for accessing the
database.*/
static char *dbst_cart_wt_in_ins =
   "INSERT INTO CART_WT_IN VALUES (?, ?, ?, ?)";
static sqlite3_stmt *dbcmp_cart_wt_in_ins;
static const char   *dbend_cart_wt_in_ins;

static char *dbst_cart_wt_in_sel =
   "SELECT * FROM CART_WT_IN WHERE cart_id = ?";
static sqlite3_stmt *dbcmp_cart_wt_in_sel;
static const char   *dbend_cart_wt_in_sel;

static char *dbst_cart_wt_in_del =
   "DELETE FROM CART_WT_IN WHERE cart_id = ?";
static sqlite3_stmt *dbcmp_cart_wt_in_del;
static const char   *dbend_cart_wt_in_del;

/*-- db_prep ---
Prepare the SQL statements.*/
BOOLEAN db_prep (void) {

  if (sqlite3_prepare(db, dbst_cart_wt_in_ins,
  -1,
  &dbcmp_cart_wt_in_ins,
  &dbend_cart_wt_in_ins) != SQLITE_OK) {
errormet("908", (char *)sqlite3_errmsg(db)); /*Fatal DB Error.*/
return(TRUE);
  }  /*if*/

  if (sqlite3_prepare(db, dbst_cart_wt_in_sel,
  -1,
  &dbcmp_cart_wt_in_sel,
  &dbend_cart_wt_in_sel) != SQLITE_OK) {
errormet("908",  (char *)sqlite3_errmsg(db)); /*Fatal DB Error.*/
return(TRUE);
  }  /*if*/

  if (sqlite3_prepare(db, dbst_cart_wt_in_del,
  -1,
  &dbcmp_cart_wt_in_del,
  &dbend_cart_wt_in_del) != SQLITE_OK) {
errormet("908", (char *)sqlite3_errmsg(db)); /*Fatal DB Error.*/
return(TRUE);
  }  /*if*/

  return(FALSE);
}/*db_prep*/
.
/*-- db_add_cart_weight ---
Ad a record of CARTID, TIME, DATE and WEIGHT to database.
This function uses pre-compiled SQL statements and binds the
incoming variables.*/
BOOLEAN db_add_cart_weight (CART_WT_IN *cartr) {

  int  busy_count;
  int  rc;
  char strg[256];

  /*First we bind the variables.  The -1 length specifies the
  actual string length.*/
  if (sqlite3_bind_text(dbcmp_cart_wt_in_ins, 1,
cartr->cart_id, -1,
SQLITE_STATIC) != SQLITE_OK) {
errormet("908", (char *)sqlite3_errmsg(db)); /*Fatal DB Error.*/
return(TRUE);
  }  /*if*/

  if (sqlite3_bind_text(dbcmp_cart_wt_in_ins, 2,
cartr->wt, -1,
SQLITE_STATIC) != SQLITE_OK) {
errormet("908", (char *)sqlite3_errmsg(db));  /*Fatal DB Error.*/
return(TRUE);
  }  /*if*/

  if (sqlite3_bind_text(dbcmp_cart_wt_in_ins, 3,
cartr->tm, -1,
SQLITE_STATIC) != SQLITE_OK) {
errormet("908", (char *)sqlite3_errmsg(db));  /*Fatal DB Error.*/
return(TRUE);
  }  /*if*/

  if (sqlite3_bind_text(dbcmp_cart_wt_in_ins, 4,
cartr->dt, -1,
SQLITE_STATIC) != SQLITE_OK) {
errormet("908", (char *)sqlite3_errmsg(db));  /*Fatal DB Error.*/
return(TRUE);
  }  /*if*/

  /*Now we execute the SQL statement.  Handle the possibility that
  sqlite is busy, but drop out after a number of attempts.*/
  busy_count = 0;
  while (TRUE) {
rc = sqlite3_step(dbcmp_cart_wt_in_ins);

switch (rc) {
  case SQLITE_BUSY:/*We must try again, but not forever.*/
if (busy_count++ > MAX_BUSY_TRIES) {
  sqlite3_reset(dbcmp_cart_wt_in_ins);
  errormet("908", "DB locked busy");
  return(TRUE);
}  /*if*/
sleep(0);  /*For a gentler poll.*/
break;
  case SQLITE_DONE:/*Success.*/
sqlite3_reset(dbcmp_cart_wt_in_ins); /*Ready for next step.*/
return(FALSE);
  case SQLITE_ROW: /*A row is ready.  Should never happen.*/
sqlite3_reset(dbcmp_cart_wt_in_ins);
return(TRUE);
break;
  case SQLITE_ERROR:   /*Run time error, discard the VM.*/
sqlite3_reset(dbcmp_cart_wt_in_ins);
sprintf(strg, "SQL INSERT error: %s", sqlite3_errmsg(db));
errormet("908", strg);   /*Fatal Error.*/
return(TRUE);
  case SQLITE_MISUSE:  /*VM should not have been used.*/
sqlite3_reset(dbcmp_cart_wt_in_ins);
errormet("908", "SQLITE_MISUSE");  /*Fatal DB Error.*/
return(TRUE);
break;
  default:
sqlite3_reset(dbcmp_cart_wt_in_ins);
errormet("908", "Unexpected return from sqlite3_step");
return(TRUE);
}  /*switch*/
  }/*while*/

  return(FALSE);
}/*db_add_cart_weight*/


Alfredo Cole wrote:

Hi:

I have searched, but have not found samples of actual uses of sqlite_compile, 
sqlite_step and sqlite_finalize. Could anybody point the way to where I can 
find samples with source code to use these functions?


Thank you.





Re: [sqlite] Sample code

2005-10-26 Thread Alfredo Cole
El Miércoles, 26 de Octubre de 2005 08:53, escribió:
 > On 10/26/05, Alfredo Cole <[EMAIL PROTECTED]> wrote:
 > > I have searched, but have not found samples of actual uses of
 > > sqlite_compile, sqlite_step and sqlite_finalize. Could anybody point the
 > > way to where I can find samples with source code to use these functions?
 >
 > It's c++ but here's some:
 >
(...)

Great! Thank you.

-- 
Alfredo J. Cole
Grupo ACyC


Re: [sqlite] Sample code

2005-10-26 Thread Jay Sprenkle
On 10/26/05, Alfredo Cole <[EMAIL PROTECTED]> wrote:
> I have searched, but have not found samples of actual uses of sqlite_compile,
> sqlite_step and sqlite_finalize. Could anybody point the way to where I can
> find samples with source code to use these functions?

It's c++ but here's some:

sqlite3*  dbInput = NULL;
sqlite3*  dbOutput = NULL;

// get database names
string dbNameInput = argv[1];
string dbNameOutput = dbNameInput + ".backup";

sqlite3_stmt *pStmt;
int   rc;
bool  Loop;

// a list of tables
vector tables;

//--
// Get a list of every table in the source database
//--
string sql = "SELECT name, sql"
 " FROM sqlite_master"
 " WHERE type = 'table'";

// connect to database
rc = sqlite3_open( dbNameInput.c_str(), &dbInput );
if ( rc )
  throw "Can't open source database";

if ( sqlite3_prepare( dbInput, sql.c_str(), sql.size(),
&pStmt, NULL ) != SQLITE_OK )
  throw "Cannot prepare sql";

Loop = true;
while ( Loop )
  switch ( sqlite3_step( pStmt ) )
{
  case SQLITE_ROW:
{
  char* p;
  stringName;

  // get results ( 0 based index!!! )
  p = (char *) sqlite3_column_text( pStmt, 0 );
  if ( ! p )
throw "blank table names in source database";
  Name = p;

  p = (char *) sqlite3_column_text( pStmt, 1 );
  if ( ! p )
throw "blank sql in source database";
  sql = p;

  // copy table to list
  tables.push_back( table( Name, sql ) );
}
break;
  case SQLITE_DONE:
Loop = false;
break;
  default:
throw "Cannot execute sql";
break;
}

// clean up when finished
sqlite3_finalize( pStmt );
sqlite3_close( dbInput );
dbInput = NULL;


[sqlite] Sample code

2005-10-26 Thread Alfredo Cole
Hi:

I have searched, but have not found samples of actual uses of sqlite_compile, 
sqlite_step and sqlite_finalize. Could anybody point the way to where I can 
find samples with source code to use these functions?

Thank you.

-- 
Alfredo J. Cole
Grupo ACyC


Re: [sqlite] Sample code

2004-10-29 Thread Paul Pigott
Thanks, this is just what I was looking for.

Paul

- Original Message - 
From: "Clay Dowling" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, October 29, 2004 9:27 AM
Subject: Re: [sqlite] Sample code


>
> Paul Pigott said:
> > I'm putting together a small test program based on some code I found in
> > "C/C++ Users Journal".  The original code was in sqlite2, and I'm using
> > sqlite3.  I'm having a couple of issues with the sqlite3_stmt definition
> > and its use in sqlite3_prepare and sqlite3_step.  Does anyone have a
small
> > example that I could refer to?  I would greatly appreciate it.
>
> http://www.linuxjournal.com/article.php?sid=7803
>
> Links at the end lead to a full app with source and Makefile
>
> Clay
>
> -- 
> Lazarus Notes from Lazarus Internet Development
> http://www.lazarusid.com/notes/
> Articles, Reviews and Commentary on web development



Re: [sqlite] Sample code

2004-10-29 Thread Clay Dowling

Paul Pigott said:
> I'm putting together a small test program based on some code I found in
> "C/C++ Users Journal".  The original code was in sqlite2, and I'm using
> sqlite3.  I'm having a couple of issues with the sqlite3_stmt definition
> and its use in sqlite3_prepare and sqlite3_step.  Does anyone have a small
> example that I could refer to?  I would greatly appreciate it.

http://www.linuxjournal.com/article.php?sid=7803

Links at the end lead to a full app with source and Makefile

Clay

-- 
Lazarus Notes from Lazarus Internet Development
http://www.lazarusid.com/notes/
Articles, Reviews and Commentary on web development


Re: [sqlite] Sample code

2004-10-29 Thread Ulrik Petersen
Paul,

You wrote:

> Greetings all,
>
> I'm putting together a small test program based on some code I found in
> "C/C++ Users Journal".  The original code was in sqlite2, and I'm using
> sqlite3.  I'm having a couple of issues with the sqlite3_stmt definition
> and its use in sqlite3_prepare and sqlite3_step.  Does anyone have a small
> example that I could refer to?  I would greatly appreciate it.
>
> Thanks,
>
> Paul


I wrote:

> Otherwise, for a smaller example, you can look at my Emdros project, which
> is under the GPL:

Oops, I forgot that the online version does not use SQLite 3.  Email me
off-list if you want a copy of the code that uses SQLite 3.

Ulrik


-- 
Ulrik Petersen, Denmark
Homepage: 




Re: [sqlite] Sample code

2004-10-29 Thread Ulrik Petersen
Paul,

> Greetings all,
>
> I'm putting together a small test program based on some code I found in
> "C/C++ Users Journal".  The original code was in sqlite2, and I'm using
> sqlite3.  I'm having a couple of issues with the sqlite3_stmt definition
> and its use in sqlite3_prepare and sqlite3_step.  Does anyone have a small
> example that I could refer to?  I would greatly appreciate it.
>
> Thanks,
>
> Paul

The src/shell.c program in the SQLite distribution serves such a purpose.

Otherwise, for a smaller example, you can look at my Emdros project, which
is under the GPL:

http://emdros.org/preview/index.php?dir=&file=emdros-1.2.0.pre79.tar.gz

You'll want to concentrate on EMdF/sqliteconn.cpp and EMdF/conn.cpp (the
header files are include/sqliteconn.h and include/conn.h)

HTH

Ulrik P.

-- 
Ulrik Petersen, Denmark




Re: [sqlite] Sample code

2004-10-29 Thread Christian Smith
On Fri, 29 Oct 2004, Paul Pigott wrote:

>Greetings all,
>
>I'm putting together a small test program based on some code I found in
>"C/C++ Users Journal".  The original code was in sqlite2, and I'm using
>sqlite3.  I'm having a couple of issues with the sqlite3_stmt definition
>and its use in sqlite3_prepare and sqlite3_step.  Does anyone have a
>small example that I could refer to?  I would greatly appreciate it.

http://www.sqlite.org/capi3.html
http://www.sqlite.org/capi3ref.html


>
>Thanks,
>
>Paul

-- 
/"\
\ /ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
 X   - AGAINST MS ATTACHMENTS
/ \


[sqlite] Sample code

2004-10-29 Thread Paul Pigott
Greetings all,

I'm putting together a small test program based on some code I found in "C/C++ Users 
Journal".  The original code was in sqlite2, and I'm using sqlite3.  I'm having a 
couple of issues with the sqlite3_stmt definition and its use in sqlite3_prepare and 
sqlite3_step.  Does anyone have a small example that I could refer to?  I would 
greatly appreciate it.

Thanks,

Paul