“没有这样的桌子:运动”从SQLite DB导出表(Django View)

发布于 2025-01-25 23:43:36 字数 1079 浏览 2 评论 0原文

在我的django视图中,更新了表格后,我将此代码放入CSV文件中:

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()

但是它给了我错误:例外值:没有这样的表:Sport 。 我确定表名是正确的,因为我的model.py中是相同的。

我不确定是否可以使用连接和连接关闭来纠正线路。我是新手。 我的浏览器: “在此处输入图像描述”

编辑2: 我看到编写路径的正确方法是“ e:\ ...'或r'e:...'。我在我的代码conn = sql.connect(r'e:\ work \ django \ Analysis \ Analysisdata \ db.sqlite3')中写下了这样的书。 “没有这样的桌子:运动”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2025-02-01 23:43:37

尝试一下

python manage.py makemigrations

python manage.py迁移

这可能只是Django的错误

Try this

python manage.py makemigrations

python manage.py migrate

it can be just Django's error

柏拉图鍀咏恒 2025-02-01 23:43:37

在我的代码中编写了这一行之后:

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())

我看到表的名称与Model.py中的名称不同。表的名称为:projectName_nameoftable。
我修改了表名称,但我没有这个错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文