本文整理汇总了Python中plugins.db_manager.db_plugins.plugin.TableField类的典型用法代码示例。如果您正苦于以下问题:Python TableField类的具体用法?Python TableField怎么用?Python TableField使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TableField类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCreateRenameDeleteFields
def testCreateRenameDeleteFields(self):
if not self.supportsAlterFieldDefn:
return
connection_name = 'testCreateRenameDeleteFields'
plugin = createDbPlugin('spatialite')
uri = QgsDataSourceUri()
test_spatialite_new = os.path.join(self.basetestpath, 'testCreateRenameDeleteFields.spatialite')
shutil.copy(self.test_spatialite, test_spatialite_new)
uri.setDatabase(test_spatialite_new)
self.assertTrue(plugin.addConnection(connection_name, uri))
connection = createDbPlugin('spatialite', connection_name)
connection.connect()
db = connection.database()
self.assertIsNotNone(db)
tables = db.tables()
self.assertEqual(len(tables), 1)
table = tables[0]
field_before_count = len(table.fields())
field = TableField(table)
field.name = 'real_field'
field.dataType = 'DOUBLE'
self.assertTrue(table.addField(field))
self.assertEqual(len(table.fields()), field_before_count + 1)
# not supported in spatialite
# self.assertTrue(field.update('real_field2', new_type_str='TEXT (30)', new_not_null=True, new_default_str='foo'))
field = table.fields()[field_before_count]
self.assertEqual(field.name, 'real_field')
self.assertEqual(field.dataType, 'DOUBLE')
# self.assertEqual(field.notNull, 1)
# self.assertEqual(field.default, "'foo'")
# self.assertTrue(table.deleteField(field))
# self.assertEqual(len(table.fields()), field_before_count)
connection.remove()
示例2: testCreateRenameDeleteFields
def testCreateRenameDeleteFields(self):
if not self.supportsAlterFieldDefn:
return
connection_name = "testCreateRenameDeleteFields"
plugin = createDbPlugin("gpkg")
uri = QgsDataSourceUri()
test_gpkg_new = os.path.join(self.basetestpath, "testCreateRenameDeleteFields.gpkg")
shutil.copy(self.test_gpkg, test_gpkg_new)
uri.setDatabase(test_gpkg_new)
self.assertTrue(plugin.addConnection(connection_name, uri))
connection = createDbPlugin("gpkg", connection_name)
connection.connect()
db = connection.database()
self.assertIsNotNone(db)
tables = db.tables()
self.assertEqual(len(tables), 1)
table = tables[0]
field_before_count = len(table.fields())
field = TableField(table)
field.name = "real_field"
field.dataType = "DOUBLE"
self.assertTrue(table.addField(field))
self.assertEqual(len(table.fields()), field_before_count + 1)
self.assertTrue(field.update("real_field2", new_type_str="TEXT (30)", new_not_null=True, new_default_str="foo"))
field = table.fields()[field_before_count]
self.assertEqual(field.name, "real_field2")
self.assertEqual(field.dataType, "TEXT(30)")
self.assertEqual(field.notNull, 1)
self.assertEqual(field.default, "'foo'")
self.assertTrue(table.deleteField(field))
self.assertEqual(len(table.fields()), field_before_count)
connection.remove()
示例3: testCreateRenameDeleteTable
def testCreateRenameDeleteTable(self):
connection_name = 'testCreateRenameDeleteTable'
plugin = createDbPlugin('gpkg')
uri = QgsDataSourceUri()
test_gpkg_new = os.path.join(self.basetestpath, 'testCreateRenameDeleteTable.gpkg')
shutil.copy(self.test_gpkg, test_gpkg_new)
uri.setDatabase(test_gpkg_new)
self.assertTrue(plugin.addConnection(connection_name, uri))
connection = createDbPlugin('gpkg', connection_name)
connection.connect()
db = connection.database()
self.assertIsNotNone(db)
tables = db.tables()
self.assertEqual(len(tables), 1)
table = tables[0]
self.assertTrue(table.rename('newName'))
self.assertEqual(table.name, 'newName')
connection.reconnect()
db = connection.database()
tables = db.tables()
self.assertEqual(len(tables), 1)
table = tables[0]
self.assertEqual(table.name, 'newName')
fields = []
geom = ['geometry', 'POINT', 4326, 3]
field1 = TableField(table)
field1.name = 'fid'
field1.dataType = 'INTEGER'
field1.notNull = True
field1.primaryKey = True
field2 = TableField(table)
field2.name = 'str_field'
field2.dataType = 'TEXT'
field2.modifier = 20
fields = [field1, field2]
self.assertTrue(db.createVectorTable('newName2', fields, geom))
tables = db.tables()
self.assertEqual(len(tables), 2)
new_table = tables[1]
self.assertEqual(new_table.name, 'newName2')
fields = new_table.fields()
self.assertEqual(len(fields), 3)
self.assertFalse(new_table.hasSpatialIndex())
self.assertTrue(new_table.createSpatialIndex())
self.assertTrue(new_table.hasSpatialIndex())
self.assertTrue(new_table.delete())
tables = db.tables()
self.assertEqual(len(tables), 1)
connection.remove()