SQLite, QString and QByteArray
-
Here is some code:
@QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( ":memory:" );
db.open();
QSqlQuery query( db );
query.exec( "create table q(a,b)" );
query.prepare( "insert into q values(?,?)" );
query.addBindValue( QString( "123" ) );
query.addBindValue( QByteArray( "234" ) );
query.exec();query.exec( "select * from q" );
qDebug() << query.next();query.prepare( "select * from q where a=?" );
query.addBindValue( QString( "123" ) );
query.exec();
qDebug() << query.next();
query.prepare( "select * from q where a=?" );
query.addBindValue( QByteArray( "123" ) );
query.exec();
qDebug() << query.next();
query.prepare( "select * from q where b=?" );
query.addBindValue( QString( "234" ) );
query.exec();
qDebug() << query.next();
query.prepare( "select * from q where b=?" );
query.addBindValue( QByteArray( "234" ) );
query.exec();
qDebug() << query.next();@And here is output:
@true
true
false
false
true @I've just wrote this and realized - are QByteArray values stored as BLOB?
-
I'm not sure, but you haven't specified any type for the columns into your table:
@query.exec( "create table q(a,b)" );@
so it could be a default cast?
-
Specifying type for SQLite is just a recommendation for driver (or at least i remember it so). It determines type automatically. And as i understand somehow SQLite driver thinks that QByteArray( "234" ) is BLOB.
-
Well, QByteArray actually is a BLOB ("The QByteArray class provides an array of bytes."), so why should it be stored as anything else then a BLOB?
-
Yes, i understand it. I think i misunderstood the way values are passed to SQLite backend.
The only question (not related to Qt though) now is why
@SELECT * FROM q WHERE a='123';@
gives nothing while
@SELECT * FROM q WHERE a like '123';@
outputs the right row? It's been a little confusing when i tried to get values from table with the first query and got empty query. -
[quote author="Lukas Geyer" date="1323420960"]Well, QByteArray actually is a BLOB ("The QByteArray class provides an array of bytes."), so why should it be stored as anything else then a BLOB?[/quote]
Correct, even if this example they are not Large...sounds as they should be stored as BOB! Just joking.