Do not insert duplicate data

0

I'm a bit confused, what would be the best practice to not insert duplicate data in a table, I have the following code.

cnn = pyodbc.connect ( 'Driver={SQL Server};Server=**;Database=**;uid=**;pwd=**',autocommit=True )


sqlcmd = ("Select GEOM.STX as lng,GEOM.STY as lat,a.Codigo from Cie a \
                       inner join Inmueble b on a.idInmueble = b.idInmueble \
                       inner join DiviPolitica c on c.idDiviPolitica = b.idDiviPolitica \
                       inner join Dire d on d.idDivisionPolitica = c.idDivisionPolitica where d.geom is not null")

    read = fiona.open(r"\geo-pre\data\ViasActivos.shp")        

    base = cnn.cursor()
    base.execute(sqlcmd)

                for feature in read:
                    coord = feature['geometry']['coordinates'][0]
                    name = feature['properties']['Name']
                    linea = feature['properties']['LINEA']
                    ramal = feature['properties']['RAMAL']
                    cabecera = feature['properties']['CABECERAS']

                    for r in base:
                        longVias = coord[0]
                        latVias = coord[1]

                        cordenadas = '\''+str(longVias)+','+str(latVias)+'\''

                        center_point = [{'lat': latCIE, 'lng': lngCIE}]
                        test_point = [{'lat': latVias, 'lng': longVias}]

                        lat1 = center_point[0]['lat']
                        lon1 = center_point[0]['lng']
                        lat2 = test_point[0]['lat']
                        lon2 = test_point[0]['lng']

                        radius = 5.00 # radio en kilometros

                        a = haversine(lon1, lat1, lon2, lat2)

                        insertVia = ("INSERT INTO [dbo].[atributosCapas] \
                                                                  (idCapaOrigen,[idTipo] \
                                                                  ,[idOrigen] \
                                                                  ,[GeomTipo] \
                                                                  ,[Geom] \
                                                                  ,[Fecha] \
                                                                  ,[Nombre] \
                                                                  ,[Direccion] \
                                                                  ,[Localidad] \
                                                                  ,[Provincia],descripcion,cadena)\
                                        VALUES (null,3,2,'Punto',null,GETDATE(),'%s',null,null,null,null,'Ramal:%s,Linea:%s,cabecera:%s','%s')" % \
                                 (name,ramal,linea,cabecera,cordenadas))

                        base.execute(insertVia)   

The first for goes through a shapefile, the second for goes through an MSSQL query that I later insert into a table, now the only field I have to ask if that data already exists in the table [dbo]. [attributesCapas] is by the name (name), which would be the best practice for not inserting duplicates

Edit: I add to the question that I would like to avoid techniques like NOT IN or EXIST

    
asked by Sebastian 13.03.2018 в 13:20
source

1 answer

1

The best thing in that case is to leave the integrity of the data to the engine, so that from any application that restriction of not duplicating values is complied with - avoid redundancy -

If the table exists then you make an alter:

ALTER TABLE dbo.MiTabla ADD CONSTRAINT
    IX_nombres UNIQUE NONCLUSTERED 
    (
    Nombre
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

The unique makes you work ... In case you are creating the zero table, you include it just after the column:

create table nombres (Id int identity, Nombre varchar(10) unique)
    
answered by 13.03.2018 в 14:25