John Stanton wrote:
[EMAIL PROTECTED] wrote:
John Stanton a écrit :
[EMAIL PROTECTED] wrote:
Hello,
I got an error when I try to read some data outside the while{},
inside the while{} it's ok, an idea ?
test.db have just one "table1" and a "field1" with values "one",
"two", "three".
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
sqlite3 *db;
sqlite3_stmt *pStat;
const char *dbname = "test.db";
const char *sql = "SELECT * FROM table1";
const unsigned char *my_array[3];
int i=0;;
sqlite3_open(dbname, &db);
sqlite3_prepare_v2(db, sql, -1, &pStat, 0);
while(sqlite3_step(pStat) == SQLITE_ROW)
{
my_array[i] = sqlite3_column_text(pStat, 0);
printf ("%s\n",my_array[i]); // ok
i++;
}
for (i = 0; i<3; i++);{
printf ("%s\n", my_array[i]); // error
}
sqlite3_finalize(pStat);
sqlite3_close(db);
return 0;
}
Fred.
Your program is wrongly structured. Try this layout:
sqlite3_prepare_V2
while (TRUE) {
rc = sqlite3_step
switch (rc) {
case SQLITE_ROW:
/*Get each column*/
for (count = 0; count > sqlite3_column_count; count++) {
switch (sqlite3_column_type) {
case SQLITE_TEXT:
pt = sqlite3_column_text
/*Move text into your output*/
sprintf(outval, "%s", pt); /*Or some other move.*/
break;
case SQLITE_INTEGER:
outnbr = sqlite3_column_int;
or
sprintf(outval, "%d", sqlite_column_int(..));
break;
....
add other types
}
}
break;
case SQLITE_DONE:
sqlite3_finalize
return from function
case SQLITE_BUSY:
/*Handle BUSY condition.*/
break;
default:
/*Handle error condition.*/
break;
} /*switch*/
} /*while*/
Now you can handle errors, busy conditions and the return of differring
types from Sqlite. When you get a pointer to a text value from
Sqlite it is the programmer's responsibility to move data from that
pointer into data in your program.
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------
Hello John,
Thanks for reply,
I just tested with your code that seem to be more useful, but
something is wrong :
#include <stdio.h>
#include <sqlite3.h>
int main(void) {
sqlite3 *db;
sqlite3_stmt *pStat;
const gchar *dbname = "test.db";
int colcnt;
int rc;
int finished = 0;
rc = sqlite3_open(dbname, &db);
if (rc != SQLITE_OK) {
printf("error\n");
exit(1);
}
rc = sqlite3_prepare_v2(db, "SELECT * FROM TABLE1", -1, &pStat, 0);
if (rc != SQLITE_OK) {
printf("error\n");
exit(1);
}
while(!finished) {
rc = sqlite3_step(pstat);
switch (rc) {
case SQLITE_ROW:
/*Get each column*/
for (colcnt = 0; colcnt < sqlite3_column_count(pStat);
colcnt++) {
/*Handle returned data according to type.*/
switch (sqlite3_column_type(pStat, 0)) {
case SQLITE_TEXT:
printf("%s %s ", sqlite3_colimn_name(pstat, colcnt),
sqlite3_column_text(pStat,0));
break;
case SQLITE_INTEGER:
printf("%s %d ", sqlite3_column_name(pstat, colcnt),
sqlite3_column_int(pstat);
break;
/*!!!There are more types like SQLITE_NULL ...*/
} /*switch*/
} /*for*/
printf("\n");
break;
case SQLITE_BUSY:
/*Busy logic*/
break;
case SQLITE_DONE:
sqlite3_finalize(pstat);
finished = 1;
break;
default:
printf("Error\n");
break;
} /*while*/
sqlite3_close(db);
exit(0);
}
You left out quite a bit of the example. I have corrected it, but not
tested the code. This should print out a table of what you read. Take
note that Sqlite does not have fixed types, so the programmer must be
aware of that.
You also need to be aware of error conditions and busy states.
Whoops, I left out the sqlite3_step. Corrected.
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------