Home > Mobile >  "no such table: Sport" on exporting a table from sqlite db (django view)
"no such table: Sport" on exporting a table from sqlite db (django view)

Time:05-05

In my django view, after updating a table, I put this code for exporting that table into csv file:

import sqlite3 as sql
import os
import csv

# export Data
print ("Export data into csv file..............")
conn = sql.connect('sqlite3.db') #  I tried: db.sqlite3 -> same
cursor=conn.cursor()
cursor.execute("select * from Sport")
with open("heartrateai_data.csv", "w") as csv_file:
     csv_writer = csv.writer(csv_file, delimiter="\t")
     csv_writer.writerow([i[0] for i in cursor.description])
     csv_writer.writerows(cursor)
dirpath = os.getcwdb() "/heartrateai_data.csv"
print("Data exported Successfully into {}".format(dirpath))
conn.close()

But it gives me the error: Exception Value: no such table: Sport. I am sure that the table name is correct because is the same in my model.py.

I am not sure if it correct the line with connection and connection close. I am new in this. My browser: enter image description here

Edit 2: I saw that the correct way to write the path is with 'E:\...' or with r'E:...'. I wrote like this in my code conn = sql.connect(r'E:\Work\django\analysisData\db.sqlite3') but I have the same error. "No such table: Sport"

CodePudding user response:

Try this

python manage.py makemigrations

python manage.py migrate

it can be just Django's error

CodePudding user response:

After I wrote this lines in my code:

con = sql.connect(r'E:\Work\django\analysisData\db.sqlite3')
cursor = con.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
print(cursor.fetchall())

I saw that the name of the table isn't the same like in the model.py. The name of tables is: projectName_NameOfTable. I modified the table name, and I don't have that error again.

  • Related