sqlite3操作错误:没有这样的表:repl.it上的主错误,但PC上没有

2024-10-03 02:44:35 发布

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

我正在用discord.py制作一个discord机器人,它依赖sqlite3数据库来存储公会信息。在我的电脑上制作机器人时,一切都进行得很顺利,我没有收到错误,但是当我上传到repl.it时,它突然出现了错误。为什么会这样

import time
import requests
import random
import asyncio
import datetime
import discord
import sqlite3
import randfacts
from discord.ext import tasks, commands


class FactsCog(commands.Cog, name = "Facts"):
    def __init__(self, bot):
        self.bot = bot
        self.fotd.start()

    @tasks.loop(seconds=20.0)
    async def fotd(self):
        db = sqlite3.connect('main.sqlite')
        cursor = db.cursor()
        cursor.execute(f"SELECT channel_id FROM main")
        channels = cursor.fetchall()
        for ID in channels:
            fact = randfacts.getFact()
            channel = self.bot.get_channel(int(ID[0]))
            embed = discord.Embed(colour = 0x4f92ff, title = "Fact Of The Day", description = fact)
            embed.set_thumbnail(url = 'https://cdn.discordapp.com/attachments/811504534177579038/820069055444156436/TriviaTops.png')
            embed.set_footer(text = "Fact Of The Day - Trivia Tops", icon_url = 'https://cdn.discordapp.com/attachments/811504534177579038/820069055444156436/TriviaTops.png')
            await channel.send(embed = embed)

    @fotd.before_loop
    async def before_start(self):
        await self.bot.wait_until_ready()

def setup(bot):
    bot.add_cog(FactsCog(bot))
    print("Facts cog loaded!")

这是我的代码,我确信没有任何问题,因为它在我的PC上运行得很好,但是repl.it上出现了错误“sqlite3 OperationalError:no-This table:main”

感谢您的阅读,如果您能详细介绍一下您的答案,我将不胜感激,因为我昨天才开始学习python


Tags: importselfmaindefbot错误channel机器人
1条回答
网友
1楼 · 发布于 2024-10-03 02:44:35

repl.it环境中不存在main.sqlite文件。为了解决这个问题,您可以使用:memory:而不是使用文件在内存中创建数据库

请注意,这也意味着当程序完成执行时,db将消失

        db = sqlite3.connect(':memory:')

相关问题 更多 >