@@ -507,6 +507,43 @@ def testBinaryField(self):
507
507
self .assertIsInstance (features [2 ]['DATA' ], QByteArray )
508
508
self .assertEqual (hashlib .md5 (features [2 ]['DATA' ].data ()).hexdigest (), '4b952b80e4288ca5111be2f6dd5d6809' )
509
509
510
+ def testBlobCreation (self ):
511
+ """
512
+ Test creating binary blob field in existing table
513
+ """
514
+ tmpfile = os .path .join (self .basetestpath , 'newbinaryfield.sqlite' )
515
+ ds = ogr .GetDriverByName ('SQLite' ).CreateDataSource (tmpfile )
516
+ lyr = ds .CreateLayer ('test' , geom_type = ogr .wkbPoint , options = ['FID=fid' ])
517
+ lyr .CreateField (ogr .FieldDefn ('strfield' , ogr .OFTString ))
518
+ lyr .CreateField (ogr .FieldDefn ('intfield' , ogr .OFTInteger ))
519
+ f = None
520
+ ds = None
521
+
522
+ vl = QgsVectorLayer (tmpfile )
523
+ self .assertTrue (vl .isValid ())
524
+
525
+ dp = vl .dataProvider ()
526
+ f = QgsFeature (dp .fields ())
527
+ f .setAttributes ([1 , 'str' , 100 ])
528
+ self .assertTrue (dp .addFeature (f ))
529
+
530
+ # add binary field
531
+ self .assertTrue (dp .addAttributes ([QgsField ('binfield' , QVariant .ByteArray )]))
532
+
533
+ fields = dp .fields ()
534
+ bin1_field = fields [fields .lookupField ('binfield' )]
535
+ self .assertEqual (bin1_field .type (), QVariant .ByteArray )
536
+ self .assertEqual (bin1_field .typeName (), 'Binary' )
537
+
538
+ f = QgsFeature (fields )
539
+ bin_1 = b'xxx'
540
+ bin_val1 = QByteArray (bin_1 )
541
+ f .setAttributes ([2 , 'str2' , 200 , bin_val1 ])
542
+ self .assertTrue (dp .addFeature (f ))
543
+
544
+ f2 = [f for f in dp .getFeatures ()][1 ]
545
+ self .assertEqual (f2 .attributes (), [2 , 'str2' , 200 , QByteArray (bin_1 )])
546
+
510
547
511
548
if __name__ == '__main__' :
512
549
unittest .main ()
0 commit comments