Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, October 2007
from sqlite to ulonglong


Message 1 in thread

Hello

I have this test program which puts an quint64 into sqlite and then
retrieves it again to check if it is the same. It doesn't seem to work.
Sqlite converts it to some scientific notation and when i use
toULongLong() to convert it back to a quint64, then it is not the same
value. Apparently qt cannot convert this e notation back. How can I
solve this?
Using Qt 4.3.1.
Best regards,
Tom,

  QFile dbfile("testdb.db");
   if(dbfile.exists()) dbfile.remove();
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
   db.setDatabaseName(dbfile.fileName());
   QCOMPARE(db.open(), true);
   QSqlQuery query;
      QVERIFY(query.exec("CREATE TABLE r("
                       "id INTEGER PRIMARY KEY,"
                       "t_occ INTEGER"
                       " );"));
   quint64 iVeryBig = 0xFFFFFFFFFFFFFFFFLL;
   QString str = QString("INSERT INTO r VALUES(0, %1);").arg(iVeryBig);
   qDebug() << str;
   QVERIFY(query.exec(str));

   QVERIFY(query.exec("SELECT t_occ FROM r;"));
   while (query.next()) {
       bool bOk;
       qDebug() << query.value(0).toString();
       qDebug() << QString("%1").arg(query.value(0).toULongLong(), 0,
16, QChar('0'));
       QCOMPARE(iVeryBig, query.value(0).toULongLong(&bOk));
       QVERIFY(bOk);
   }



--
 [ signature omitted ] 

Message 2 in thread

On October 11, 2007 07:56:03 am Tom Deblauwe wrote:
> Hello
>
> I have this test program which puts an quint64 into sqlite and then
> retrieves it again to check if it is the same. It doesn't seem to work.
> Sqlite converts it to some scientific notation and when i use
> toULongLong() to convert it back to a quint64, then it is not the same
> value. Apparently qt cannot convert this e notation back. How can I
> solve this?
> Using Qt 4.3.1.
> Best regards,
> Tom,
>
>   QFile dbfile("testdb.db");
>    if(dbfile.exists()) dbfile.remove();
>       QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
>    db.setDatabaseName(dbfile.fileName());
>    QCOMPARE(db.open(), true);
>    QSqlQuery query;
>       QVERIFY(query.exec("CREATE TABLE r("
>                        "id INTEGER PRIMARY KEY,"
>                        "t_occ INTEGER"
>                        " );"));
>    quint64 iVeryBig = 0xFFFFFFFFFFFFFFFFLL;
>    QString str = QString("INSERT INTO r VALUES(0, %1);").arg(iVeryBig);
>    qDebug() << str;
>    QVERIFY(query.exec(str));
>
>    QVERIFY(query.exec("SELECT t_occ FROM r;"));
>    while (query.next()) {
>        bool bOk;
>        qDebug() << query.value(0).toString();
>        qDebug() << QString("%1").arg(query.value(0).toULongLong(), 0,
> 16, QChar('0'));
>        QCOMPARE(iVeryBig, query.value(0).toULongLong(&bOk));
>        QVERIFY(bOk);
>    }

It looks like Sqlite stores all numeric values as 64-bit floats.  That means 
that you cannot store 64-bit whole numbers.  Looks like the standard way 
around this is to store the values as strings.  See:
http://www.sqlite.org/datatypes.html

--
 [ signature omitted ] 

Message 3 in thread

Chris Thompson wrote:
> It looks like Sqlite stores all numeric values as 64-bit floats.  That means 
> that you cannot store 64-bit whole numbers.  Looks like the standard way 
> around this is to store the values as strings.  See:
> http://www.sqlite.org/datatypes.htm
Thanks for the information!

Apparently I should just store it as timestamp. I was trying to store it 
as a timestamp in milliseconds, but I'll just store it as a date with 
date.toString(Qt:ISODate). I can then retrieve it with 
query.value(0).toDateTime(), which is what I want anyway. However, I'll 
need to add the miliseconds myself to the ISODate which Qt gives me. Or 
is there another way?

best regards,
Tom,


--
 [ signature omitted ] 

Message 4 in thread

On 10/11/07, Chris Thompson <cthompson@xxxxxxxxxxxxxxx> wrote:
> On October 11, 2007 07:56:03 am Tom Deblauwe wrote:
> > Hello
> >
> > I have this test program which puts an quint64 into sqlite and then
> > retrieves it again to check if it is the same. It doesn't seem to work.
> > Sqlite converts it to some scientific notation and when i use
> > toULongLong() to convert it back to a quint64, then it is not the same
> > value. Apparently qt cannot convert this e notation back. How can I
> > solve this?
> > Using Qt 4.3.1.
> > Best regards,
> > Tom,
> >
> >   QFile dbfile("testdb.db");
> >    if(dbfile.exists()) dbfile.remove();
> >       QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
> >    db.setDatabaseName(dbfile.fileName());
> >    QCOMPARE(db.open(), true);
> >    QSqlQuery query;
> >       QVERIFY(query.exec("CREATE TABLE r("
> >                        "id INTEGER PRIMARY KEY,"
> >                        "t_occ INTEGER"
> >                        " );"));
> >    quint64 iVeryBig = 0xFFFFFFFFFFFFFFFFLL;
> >    QString str = QString("INSERT INTO r VALUES(0, %1);").arg(iVeryBig);
> >    qDebug() << str;
> >    QVERIFY(query.exec(str));
> >
> >    QVERIFY(query.exec("SELECT t_occ FROM r;"));
> >    while (query.next()) {
> >        bool bOk;
> >        qDebug() << query.value(0).toString();
> >        qDebug() << QString("%1").arg(query.value(0).toULongLong(), 0,
> > 16, QChar('0'));
> >        QCOMPARE(iVeryBig, query.value(0).toULongLong(&bOk));
> >        QVERIFY(bOk);
> >    }
>
> It looks like Sqlite stores all numeric values as 64-bit floats.  That means
> that you cannot store 64-bit whole numbers.  Looks like the standard way
> around this is to store the values as strings.  See:
> http://www.sqlite.org/datatypes.html

SQLite 2, that is. SQLite 3 supports actual integers up to 8 bytes:
http://www.sqlite.org/datatype3.html

I'm not sure which version Qt uses, but it looks like version 2 given
that this problem is occurring.

-- 
 [ signature omitted ] 

Message 5 in thread

Andrew Medico wrote:
> SQLite 2, that is. SQLite 3 supports actual integers up to 8 bytes:
> http://www.sqlite.org/datatype3.html
>
> I'm not sure which version Qt uses, but it looks like version 2 given
> that this problem is occurring

It is version 3 that Qt uses I'm sure. But... It is a signed integer 
which gets stored as those docs say. So my problem was that I was 
printing my iVeryBig as an unsigned integer in the SQL statement. If I 
make it a qint64 it works.

Thanks,
Best regards,
Tom,



--
 [ signature omitted ]