[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Extracting the bytes from a Unicode QString
From:       Luciano Montanaro <mikelima () cirulla ! net>
Date:       2005-10-02 21:45:26
Message-ID: 200510022345.32821.mikelima () cirulla ! net
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


On Sunday 02 October 2005 21:58, Roberto Cappuccio wrote:

> I don't know why it does not work with sqlite3.
> If I pass directly myStr.utf().data(), it saves garbage.
> If I copy the stuff in a newly created char* it works fine. This is the
> code that works:
>
>     QString qsValue = "スポーツ";
>
>     int len = qsValue.utf8().length();
>     char* cUTF8 = new char[ len + 1 ];
>     memcpy( cUTF8, qsValue.utf8().data(), len );
>     cUTF8[ len ]= '\0';
>     int nRes = sqlite3_bind_text( mpVM, nParam, cUTF8, -1, SQLITE_STATIC );
>
> This is the code that doesn't work:
>
>     int len = qsValue.utf8().length();
>     int nRes = sqlite3_bind_text( mpVM, nParam,
>     (const char*)qsValue.utf8().data(), -1, SQLITE_STATIC );
>
> I really don't understand why the second way does not work for me.
>
> Thanks

I don't know how SQLite uses the string but QString.data() is only valid as 
long as the QString is not destroyed. And it is no longer valid if you do any 
operation on the string. If the sqlite call just keeps a pointer to your 
string, which is flushed only on a commit, the data pointer will no longer be 
valid. The char* cUTF8 = new char[ len + 1 ]; line ensures the data is 
preserved.

Luciano

-- 
Luciano Montanaro //
                \X/ mikelima@cirulla.net

[Attachment #5 (application/pgp-signature)]

 =

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscrib=
e <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic