[Qt-interest] mysql, windows and odbc
Nicolas
nicolas at happycontent.com
Wed Oct 7 22:57:45 CEST 2009
Hello.
> i'm using for my first time the QT framework.
> I'm really impressed about this framework and it seem to work fine.
> But i've encountered a problem with MYSQL under windows.
>
> I created my database and did some test with MySQL Query Browser. I did
> an insert and after i did "SELECT @@IDENTITY" and i obtained the last id
> inserted. All OK!
> When i try it from QT nothing. The insert is ok but select @@identity
> doesn't work.
>
> QSqlQuery query;
> if (!query.exec("SELECT @@IDENTITY;")) // no error returned
<snipped>
Is your example truncated, or does your 'query' only does the 'select
@@identity' part?
I think you have to do:
QSqlQuery query;
query.exec('insert into ...');
QVariant newId = query.lastInsertId();
for the thing to work.
From the documentation:
----------------------------------
QVariant QSqlQuery::lastInsertId () const
Returns the object ID of the most recent inserted row if the database supports
it. An invalid QVariant will be returned if the query did not insert any
value or if the database does not report the id back. If more than one row
was touched by the insert, the behavior is undefined.
For MySQL databases the row's auto-increment field will be returned.
--------------------------------------
Nicolas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091007/3ae1b8a1/attachment.bin
More information about the Qt-interest-old
mailing list