1
votes

How to bind simple char char_type; and read it back from the sqlite3 database?

 char char_type;
 char_type = 'V';
 sqlite3_bind_text(stmt, 1, char_type); // error: invalid conversion from 'char' to 'const char*'

/usr/local/include/sqlite3.h:3430: error: too few arguments to function 'int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int, void ()(void))'

 // read it back?
 char_type = reinterpret_cast<char*> ( sqlite3_column_text(stmt, 1) );
1
Well first is that sqlite3_bind_text() takes a pointer, so you can do &char_type for that. Also, why are you passing three arguments rather than 5? - 0x499602D2
@0x499602D2 Yes, seems need to pass also -1, , SQLITE_TRANSIENT. And I will try &char_type as suggested. Thanks. - abrahab

1 Answers

3
votes

SQLite doesn't have a char type, only a string type. So you have to make it look like a string:

// bind
sqlite3_bind_text(stmt, /* column */ 1, &char_type, 1, SQLITE_TRANSIENT);

// read
char_type = sqlite3_column_text(stmt, 1)[0];

Alternatively, if you know all your data is always going to be one character, it's almost certainly more efficient to use an integer instead and simply cast it on the way in and out:

// bind
sqlite3_bind_int(stmt, /* column */ 1, (int)char_type);

// read
char_type = (char)sqlite3_column_int(stmt, 1);