如何将csv导入到sqlite3数据库中,使列相互匹配?

2024-04-26 06:31:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图将一个.csv文件导入到我的Django db.sqlite3中,我完全按照Youtube视频中的说明操作。但是,我的.csv文件中有两行具有相同的纬度和长度值,sqlite3无法导入某些行,因为纬度和长度值已经出现在表中

sqlite> .import venues.csv myapi_venue
venues.csv:17: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:44: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:49: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:60: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:66: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:73: INSERT failed: UNIQUE constraint failed: myapi_venue.name
venues.csv:80: INSERT failed: UNIQUE constraint failed: myapi_venue.name

我试图强迫模型将unique设置为False,但这没有帮助

class venue(models.Model):
    
    name = models.CharField(_("name"), primary_key=True, max_length=300)
    address = models.CharField(_("address"), max_length=300, unique=False)
    categories = models.CharField(_("category"), null=True, max_length=300, unique=False)
    latitude = models.FloatField(_("latitude"), max_length=150, unique=False)
    longitude = models.FloatField(_("longitude"), max_length=150, unique=False)

当我尝试迁移更改时,会显示以下内容:

$ python manage.py makemigrations
  No changes detected

如何将所有行导入数据库

因此我可以证明主键是唯一的: conflicting rows

我想我找到了它显示错误的原因。出于某种原因,模型中的主键与csv中的不同列匹配。 order of columns is messed up