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

Qt-interest Archive, May 2008
qt4 + sqlite + kubuntu problem


Message 1 in thread

I was able to replicate the problem somewhat on Windows, but I can't
 seem to get it to write to terminal, which makes it much harder to
 debug.

 Anyway in qt4.4 I try to bind the query as above and end up with:
 "SELECT PrJa, PrTy,PrOnOnaOno,PrMy,PrWy,PrOniOne  FROM verbs WHERE
odczas = ? "

 as the value of query.executedQuery()

 when doing:
 QSqlQuery query;

query.prepare("SELECT PrJa, PrTy,PrOnOnaOno,PrMy,PrWy,PrOniOne  FROM
 verbs WHERE  odczas = :inf ");
  query.bindValue(":inf", " bicie");

 What am I doing wrong?
 thanks,
 Andrzej



 On Mon, Apr 28, 2008 at 9:44 AM, Andrzej Szelc <andrzej.szelc@xxxxxxxxx> wrote:
 > Hi, I have problem with connecting to a sqlite database from qt4. I
 >  used to do this in qt3 without problem, but since I wanted to reuse
 >  the code in windows I had to change to qt4 and now I'm stuck at he
 >  following point:
 >
 >  It boils down to this, I create a main.cpp:
 >  (I am on Kubuntu 7.10, qt 4.3.2, sqlite 2.8.17 )
 >
 >  int main( int argc, char ** argv )
 >  {
 >   QApplication a( argc, argv );
 >
 >   if ( ! createConnections() )
 >            return 1;
 >   PIchangeSlot();
 >
 >   a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
 >   return a.exec();
 >  }
 >
 >
 >
 >  bool createConnections()
 >  {
 >
 >     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
 >     db.setDatabaseName("DB/czasowniki2.db");
 >     bool ok = db.open();
 >
 >
 >    if(ok)
 >       return TRUE;
 >    else
 >        return FALSE;
 >  }
 >
 >
 >
 >
 >
 >
 >
 >
 >
 >
 >
 >  void PIchangeSlot()
 >  {
 >   QTextCodec *codec = QTextCodec::codecForName("UTF-8");
 >   QTextCodec::setCodecForCStrings (codec);
 >   QTextCodec::setCodecForTr(codec);
 >
 >   QSqlQuery query;
 >   query.clear();
 >
 >   query.prepare("SELECT PrJa, PrTy,PrOnOnaOno,PrMy,PrWy,PrOniOne  FROM
 >  verbs WHERE  odczas = 'bicie' ");
 >   // query.bindValue(":inf", " bicie");
 >
 >   if(!query.exec())
 >         std::cerr << QString("Query resulted in error:
 >  %1n").arg(query.lastError().text()).toStdString() << std::endl;
 >
 >   qWarning() << query.executedQuery() << " query ";
 >
 >
 >    while(query.next())
 >    {
 >          qWarning(" %s ", query.value(4).toString());
 >    }
 >
 >
 >  }
 >
 >  //////////////////////////////
 >  I actually have a set of problems. The first is that when I launch the
 >  code in this way, I get an error:
 >  Query resulted in error: file is encrypted or is not a database Unable
 >  to fetch row
 >
 >  (I've checked the query in the DB, as it is written out to the screen,
 >  and it works).
 >
 >  The second problem, is that sometimes I will need to have different
 >  values in the query, so then I do:
 >   query.prepare("SELECT PrJa, PrTy,PrOnOnaOno,PrMy,PrWy,PrOniOne  FROM
 >  verbs WHERE  odczas = :inf ");
 >   query.bindValue(":inf", " bicie");
 >
 >  and to my great surprise, the query according to qt is:
 >  "SELECT PrJa, PrTy,PrOnOnaOno,PrMy,PrWy,PrOniOne  FROM verbs WHERE
 odczas = ? "
 >  and the, error (sensibly) is: Query resulted in error:  Parameter
 >  count mismatchn
 >
 >  The thing is, this code is basically a copy of what worked flawlessly
 >  in qt3. Here I had to play around with the codecs to make the DB read
 >  polish signs, but the problems occur also for queries without any
 >  polish signs.
 >
 >  Am I opening the DB wrong? Cause that changed from qt3. But the
 >  createConnections() returns true...
 >
 >  All I could find in my searches is that it could be a problem with
 >  sqlite3. I've checked, and I do have "libsqlite3-0/gutsy uptodate
 >  3.4.2-1build1" installed besides the other 2.8* version. But If I try
 >  to deinstall it, I'd practically have to deinstall my whole system. H
 >  ow can I check which is linked? and if this is in fact the problem?
 >
 >  Anyone had a similar experience?
 >  Thanks,
 >  Andrzej
 >

--
 [ signature omitted ] 

Message 2 in thread

Hi,

> It boils down to this, I create a main.cpp:
> (I am on Kubuntu 7.10, qt 4.3.2, sqlite 2.8.17 )
> [...]
>     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
> [...]

QSQLITE loads an SQLite 3 driver, not an SQLite 2 driver:
	http://doc.trolltech.com/4.4/qsqldatabase.html#QSqlDatabase-3

-- 
 [ signature omitted ] 

Message 3 in thread

Thanks for the help.
Ok. I tried changing the db to sqlite3 compliant, but that didn't
change the symptoms. I did manage to get it to work under linux using
the QSQLITE2 driver. executed the query and got a result even.

I can't get it to work under windows though. I can't give an error
message, because I can't get it to write anything to terminal. nor
qDebug nor qWarning seem to have any effect.  It seems it goes down at
the connecting to the db phase. It works with QSQLITE driver
(connecting, not query construction - gives the "?" still).

Problem is I need to get it to work under windows as well.
Any further ideas?
thanks,
Andrzej

On Tue, May 13, 2008 at 7:38 AM, Dimitri <dimitri@xxxxxxxxxxxxx> wrote:
> Hi,
>
>
> >
> > It boils down to this, I create a main.cpp:
> > (I am on Kubuntu 7.10, qt 4.3.2, sqlite 2.8.17 )
> > [...]
> >
> >    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
> > [...]
> >
>
>  QSQLITE loads an SQLite 3 driver, not an SQLite 2 driver:
>         http://doc.trolltech.com/4.4/qsqldatabase.html#QSqlDatabase-3
>
>  --
>  Dimitri
>
>  --
>  To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
>  List archive and information: http://lists.trolltech.com/qt-interest/
>
>



-- 
 [ signature omitted ] 

Message 4 in thread

This code, which is a not-very-much modified version of your one, works  
correctly in Windows (haven't tested it in Linux):

#include <QApplication>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QTextCodec>
#include <QVariant>

bool createConnections()
{
     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
     db.setDatabaseName("test.db");
     return db.open();
}

void PIchangeSlot()
{
     QSqlQuery query;
     query.exec("CREATE TABLE verbs (foo TEXT, odczas TEXT)");
     query.exec("INSERT INTO verbs VALUES('bar','bicie')");
     query.clear();

     query.prepare("SELECT * FROM verbs WHERE odczas = :inf");
     query.bindValue(":inf", "bicie");

     const QMap<QString, QVariant>& bv = query.boundValues();
     QMessageBox::information(NULL, "SQL Test", QString("%1 bound  
value(s):\n%2").arg(bv.count()).arg(bv.value(":inf").toString()));

     if(query.exec() && query.next())
     {
         QMessageBox::information(NULL,
                                  "SQL Test",
                                  QString("%1\n\n%2\n%3")
                                      .arg(query.executedQuery())
                                      .arg(query.value(0).toString())
                                      .arg(query.value(1).toString()));
     }
     else
     {
         QMessageBox::warning(NULL,
                              "SQL Test",
                              QString("Query resulted in error:\n%1")
                                  .arg(query.lastError().text()));
     }
}

int main (int argc, char** argv)
{
     QApplication app(argc, argv);
     if (!createConnections())
     {
         QMessageBox::critical(NULL, "SQL Test", "Failed to open database");
         return app.exit(1);
     }
     PIchangeSlot();
     return 0;//app.exec();
}

-- 
 [ signature omitted ] 

Message 5 in thread

Argh. It seem I've been pointlessly going in circles.
Your code works as far as connecting to the db goes. The query though,
is still displayed with a question mark.

I can't trace back to all the tests I did, but even though I checked
the DB under windows, it seems I also had a problem with encoding. And
apparently this was also a part of the problem, that I didn't diagnose
correctly.

Anyway, everything works now. Thank you both for your time and help,
cheers,
Andrzej
> On Thu, May 15, 2008 at 9:48 PM, Constantin Makshin
> <dinosaur-rus@xxxxxxxxxxxxxxxxxxxxx> wrote:
>> This code, which is a not-very-much modified version of your one, works
>> correctly in Windows (haven't tested it in Linux):
>>
>> #include <QApplication>
>> #include <QMessageBox>
>> #include <QSqlDatabase>
>> #include <QSqlError>
>> #include <QSqlQuery>
>> #include <QTextCodec>
>> #include <QVariant>
>>
>> bool createConnections()
>> {
>>    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
>>    db.setDatabaseName("test.db");
>>    return db.open();
>> }
>>
>> void PIchangeSlot()
>> {
>>    QSqlQuery query;
>>    query.exec("CREATE TABLE verbs (foo TEXT, odczas TEXT)");
>>    query.exec("INSERT INTO verbs VALUES('bar','bicie')");
>>    query.clear();
>>
>>    query.prepare("SELECT * FROM verbs WHERE odczas = :inf");
>>    query.bindValue(":inf", "bicie");
>>
>>    const QMap<QString, QVariant>& bv = query.boundValues();
>>    QMessageBox::information(NULL, "SQL Test", QString("%1 bound
>> value(s):\n%2").arg(bv.count()).arg(bv.value(":inf").toString()));
>>
>>    if(query.exec() && query.next())
>>    {
>>        QMessageBox::information(NULL,
>>                                 "SQL Test",
>>                                 QString("%1\n\n%2\n%3")
>>                                     .arg(query.executedQuery())
>>                                     .arg(query.value(0).toString())
>>                                     .arg(query.value(1).toString()));
>>    }
>>    else
>>    {
>>        QMessageBox::warning(NULL,
>>                             "SQL Test",
>>                             QString("Query resulted in error:\n%1")
>>                                 .arg(query.lastError().text()));
>>    }
>> }
>>
>> int main (int argc, char** argv)
>> {
>>    QApplication app(argc, argv);
>>    if (!createConnections())
>>    {
>>        QMessageBox::critical(NULL, "SQL Test", "Failed to open database");
>>        return app.exit(1);
>>    }
>>    PIchangeSlot();
>>    return 0;//app.exec();
>> }
>>
>> --
>> Constantin "Dinosaur" Makshin
>>
>> --
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
>> "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
>>
>
>
>
> --
> Andrzej
>



-- 
 [ signature omitted ] 

Message 6 in thread

Yes, I had "?" in query, too. But values (query.value(x)) are extracted  
and displayed correctly. At least in my test on my system...

On Mon, 19 May 2008 13:28:31 +0400, Andrzej Szelc  
<andrzej.szelc@xxxxxxxxx> wrote:
> Argh. It seem I've been pointlessly going in circles.
> Your code works as far as connecting to the db goes. The query though,
> is still displayed with a question mark.
>
> I can't trace back to all the tests I did, but even though I checked
> the DB under windows, it seems I also had a problem with encoding. And
> apparently this was also a part of the problem, that I didn't diagnose
> correctly.
>
> Anyway, everything works now. Thank you both for your time and help,
> cheers,
> Andrzej
>> On Thu, May 15, 2008 at 9:48 PM, Constantin Makshin
>> <dinosaur-rus@xxxxxxxxxxxxxxxxxxxxx> wrote:
>>> This code, which is a not-very-much modified version of your one, works
>>> correctly in Windows (haven't tested it in Linux):
>>>
>>> #include <QApplication>
>>> #include <QMessageBox>
>>> #include <QSqlDatabase>
>>> #include <QSqlError>
>>> #include <QSqlQuery>
>>> #include <QTextCodec>
>>> #include <QVariant>
>>>
>>> bool createConnections()
>>> {
>>>    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
>>>    db.setDatabaseName("test.db");
>>>    return db.open();
>>> }
>>>
>>> void PIchangeSlot()
>>> {
>>>    QSqlQuery query;
>>>    query.exec("CREATE TABLE verbs (foo TEXT, odczas TEXT)");
>>>    query.exec("INSERT INTO verbs VALUES('bar','bicie')");
>>>    query.clear();
>>>
>>>    query.prepare("SELECT * FROM verbs WHERE odczas = :inf");
>>>    query.bindValue(":inf", "bicie");
>>>
>>>    const QMap<QString, QVariant>& bv = query.boundValues();
>>>    QMessageBox::information(NULL, "SQL Test", QString("%1 bound
>>> value(s):\n%2").arg(bv.count()).arg(bv.value(":inf").toString()));
>>>
>>>    if(query.exec() && query.next())
>>>    {
>>>        QMessageBox::information(NULL,
>>>                                 "SQL Test",
>>>                                 QString("%1\n\n%2\n%3")
>>>                                     .arg(query.executedQuery())
>>>                                     .arg(query.value(0).toString())
>>>                                     .arg(query.value(1).toString()));
>>>    }
>>>    else
>>>    {
>>>        QMessageBox::warning(NULL,
>>>                             "SQL Test",
>>>                             QString("Query resulted in error:\n%1")
>>>                                 .arg(query.lastError().text()));
>>>    }
>>> }
>>>
>>> int main (int argc, char** argv)
>>> {
>>>    QApplication app(argc, argv);
>>>    if (!createConnections())
>>>    {
>>>        QMessageBox::critical(NULL, "SQL Test", "Failed to open  
>>> database");
>>>        return app.exit(1);
>>>    }
>>>    PIchangeSlot();
>>>    return 0;//app.exec();
>>> }
>>>
>>> --
>>> Constantin "Dinosaur" Makshin
>>>
>>> --
>>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
>>> "unsubscribe" in the subject or the body.
>>> List archive and information: http://lists.trolltech.com/qt-interest/
>>>
>> --
>> Andrzej
-- 
 [ signature omitted ]