Monday 12 July 2021

load csv into sqlite3 db (django)

I am trying to load the csv files I upload to my sqlite3 db. I am able to upload the csv into a folder but it doesn't appear on my db. The db table structure contains as many columns as the csv with the corresponding data types; but the column names in the db table structure are different as in the csv, as some of the columns names in the csv contain spaces and special characters (like accents for example).

My goal is to allow the user to upload a csv file that will be loaded to the db. I was also wondering if its possible to increment the db each time a new csv is loaded (for example: I upload a file now which is loaded to the db then the second time I add, the newer details are added to the db without deleting the previous ones). Apologies if I am unclear and asking too many things at once.

Here is my model, form, function (handle upload) & views .py files:

models.py 

class UploadForm(models.Model):
    file = models.FileField()

    class Meta:
        db_table = "db_table"

forms.py

class UploadForm(forms.ModelForm):
    class Meta:
        model = UploadForm
        fields = "__all__"
  
functions.py

def handle_uploaded_file(f):
    with open('vps_app/static/upload/'+f.name, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

views.py

def index(request):
    if request.method == 'POST':
        Uploaded = UploadForm(request.POST, request.FILES)
        if Uploaded.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponse("File uploaded successfuly")
    else:
        Uploaded = UploadForm()
        return render(request, "index.html", {'form': Uploaded})

Thanks in advance.



from load csv into sqlite3 db (django)

No comments:

Post a Comment