Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite, QString and QByteArray
Qt 6.11 is out! See what's new in the release blog

SQLite, QString and QByteArray

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 6.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    p-himik
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p-himik
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p-himik
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fluca1978
              wrote on last edited by
              #6

              [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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved