IndexError: list index out of range Django and Postgresql DB

527 views Asked by At

I'm getting mad with this issue, so please help hahahahaha.

the error i have is IndexError: list index out of range, when i try do an insert to in table of my DB.

I'm trying insert a svc document with only 30 rows.

any idea?

class CamerasBaleares(TimeStampedModel, SoftDeletableModel):
        title = models.CharField(max_length=50, blank=True)
        lat = models.FloatField(blank=True, null=True)
        lon = models.FloatField(blank=True, null=True)
        url = models.URLField(blank=True)
        is_removed = models.BooleanField(default=False)
        
     
        def __str__(self):
            return self.title

        def  load_data(self):
            import csv
            self.file = open(r'/home/elsamex/Documentos/CamarasBaleares.csv')
            self.row = csv.reader(self.file, delimiter='"')
            self.list = list(self.row)
            self.tupla = tuple(self.list)
        

        def  save_data(self):
            import psycopg2
            self.connection = psycopg2.connect("dbname=camarasdgt user=******* password=++++++++")
            self.cursor = self.connection.cursor()
            self.cursor.executemany ("INSERT INTO api_camerasbaleares (title, lat, lon, url) VALUES (%s, %s, %s, %s)", self.tupla)
            self.connection.commit()
            self.cursor.close()
            self.connection.close()
        
        def select (self):
            self.load_data()
            self.save_data()
            self.cursor = self.connection.cursor()
            self.cursor.execute("""SELECT * FROM api_camerasbaleares""")
            self.rows = self.cursor.fetchall()
            self.cursor.close()
            self.connection.close()
            return self.rows
    
ins_db= CamerasBaleares()
ins_db.load_data()
ins_db.save_data()
ins_db.select()

Here the complete detail's error, i hope that could help for a great answer :)

File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/elsamex/API_Camaras_DGT/CamarasDgt/api/admin.py", line 10, in <module>
    from api.Cameras_Baleares.models import CamerasBaleares
  File "/home/elsamex/API_Camaras_DGT/CamarasDgt/api/Cameras_Baleares/models.py", line 45, in <module>
    ins_db.save_data()
  File "/home/elsamex/API_Camaras_DGT/CamarasDgt/api/Cameras_Baleares/models.py", line 28, in save_data
    self.cursor.executemany ("INSERT INTO api_camerasbaleares (title, lat, lon, url) VALUES (%s, %s, %s, %s)", self.tupla)
IndexError: list index out of range

1

There are 1 answers

0
Gormm1980 On

I finally resolve the problem was in the format of csv...

thank you for all comments.