[PyQt] Can't insert Nulls Using QSqlQuery.execBatch

Mark Mordeca mark.mordeca at prosensus.ca
Mon Oct 3 14:22:39 BST 2011


Greetings,

Using PyQt, I am having a problem inserting Nulls into a MySql table using
the execBatch() method of QSqlQuery. From the documentation, a null QVariant
(i.e. QVariant(QVariant.Double)) represents a Null when inserting.  When
doing a regular query.exec_ it does actually insert Nulls, however when I do
a query.execBatch(), zeros get inserted, not Nulls.

query.prepare(INSERT INTO `table` VALUES (?, ?))

var1=[1.0, QVariant(QVariant.Double)]
var2=[QVariant(QVariant.Double), 32.6]

query.addBindValue(QVariant(var1))
query.addBindValue(QVariant(var2))
query.execBatch()

With the above code, I would expect this to be inserted (Nulls should appear
where QVariant objects in the lists are):

1.0  Null
Null 32.6

Instead what actually gets inserted is:

1.0 0
0   32.6

If I were to insert this line by line using just regular exec_ it would
work, but I would rather use execBatch as I have many rows to insert at once
(above is just an example). I have tried making every item in the lists a
QVariant, whether Null or not, but I still get zeros, and I cannot pass in
anything other than a QVariant to addBindValue (which I why I wrap the list
in one when I call the method).

Any help would be greatly appreciated. Thank you.

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20111003/d7e181c8/attachment.html>


More information about the PyQt mailing list