Simon Davies a écrit :
On 09/10/2007, [EMAIL PROTECTED] <[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".
.
.
.
const unsigned char *my_array[3];
.
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
}
Hi Fred,
sqlite3_column_text is returning a pointer to a text string stored
within sqlite's private address space. If you want to access that data
after calling sqlite_step() again, then copy the data to some storage
of your own.
i.e
unsigned char* my_array[3];
while(sqlite3_step(pStat) == SQLITE_ROW)
{
my_array[i] = malloc( sqlite3_column_bytes(pStat, 0) );
memcpy( my_array[i], sqlite3_column_text(pStat, 0));
printf ("%s\n",my_array[i]);
i++;
}
for (i = 0; i<3; i++);{
printf ("%s\n", my_array[i]);
free( my_array[i] );
}
(or something like this - have not tested this code...)
Rgds,
Simon
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------
Thanks for your reply Simon.
I tried that code but it same :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
int main(void)
{
sqlite3 *db;
sqlite3_stmt *pStat;
const char *dbname = "test.db";
const char *sql = "SELECT * FROM table1";
char *row;
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)
{
row = (char *)sqlite3_column_text(pStat, 0);
my_array[i] = malloc( sizeof row * sizeof *my_array[i]);
memcpy (my_array[i], row, sizeof row * sizeof *my_array[i]);
printf ("%s\n",my_array[i]); // ok
i++;
}
for (i = 0; i<3; i++);{
printf ("%s\n", my_array[i]); //error
free (my_array);
}
sqlite3_finalize(pStat);
sqlite3_close(db);
return 0;
}
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------