dimanche 12 avril 2015

Sqlite3, C - Prepared Statement Binding

I've got this function which pulls down some data from a sqlite database:



void AdvancedSsDConfirmation(uint32_t seq_num)
{
char *zErrMsg = 0;
int rc;
char *sql;
char data;
sqlite3_stmt *stmt;
const char *pzTail;

printf(" Running Seq Num: %" PRIu32 "\n", seq_num);

sql = "SELECT T.srcIP AS source_ip, Count(*) AS ack_count " \
"FROM " \
"(SELECT DISTINCT srcIP, srcPort " \
"FROM resetackpackets " \
"WHERE ack = '?' + 1) " \
"AS T GROUP BY T.srcIP;";

rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &pzTail);
sqlite3_bind_int64(stmt, 1, seq_num); // Binding here

printf("\nSTART-RESULTS:\n");
/* callbackAdvancedSsDConfirmation just prints out the results, works when variable is manually set */
rc = sqlite3_exec(db, sql, callbackAdvancedSsDConfirmation, &data, &zErrMsg);
printf("END-RESULTS:");

if(rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "\n[INFO][AdvancedSsDConfirmation] Operation done successfully\n");
}
}
-----Console Output:-----------------------------------------
| srcIP = 192.168.56.1
| seq = 180681059
| count = 50
Running Seq Num: 180681059 // Correct sequence number

START-RESULTS:
END-RESULTS:
[INFO][AdvancedSsDConfirmation] Operation done successfully


But I'm getting no results set when I call sqlite3_exec. I've tried the SQL query manually in the terminal and it works, and I've tried manually adding the seq_num value in the place of the ? and then that works, but as I have it now, It's not returning any results.


Am I binding the seq_num (a 32-bit value) to the query wrong?


Aucun commentaire:

Enregistrer un commentaire