You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telethon+SQLite在Ubuntu服务器启动报错:无法打开数据库文件

Telegram Bot部署Ubuntu服务器报错:sqlite3.OperationalError: unable to open database file

我基于Python/Telethon开发了一个Telegram Bot项目,包含单个Python文件、SQLite数据库和配置文件。该Bot在本地虚拟环境中(三个文件位于同一目录)运行正常,但在Ubuntu服务器上运行Python文件时遇到如下错误:

项目文件路径:/usr/local/bin/project/

Traceback (most recent call last):
  File "/usr/local/bin/project/script.py", line 19, in <module>
    client = TelegramClient(session_name, API_ID, API_HASH).start(bot_token=BOT_TOKEN)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/telethon/client/telegrambaseclient.py", line 289, in __init__
    session = SQLiteSession(session)
              ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/telethon/sessions/sqlite.py", line 47, in __init__
    c = self._cursor()
        ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/telethon/sessions/sqlite.py", line 242, in _cursor
    self._conn = sqlite3.connect(self.filename,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: unable to open database file

script.py 内容

import configparser
import re
from telethon import TelegramClient, Button, events
import sqlite3
from datetime import datetime
import traceback

print("Initializing configurations...")
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf-8')

API_ID = config.get('default','api_id') 
API_HASH = config.get('default','api_hash')
BOT_TOKEN = config.get('default','bot_token')
session_name = "sessions/Bot"

# Start the bot session
client = TelegramClient(session_name, API_ID, API_HASH).start(bot_token=BOT_TOKEN)

@client.on(events.NewMessage(pattern="(?i)/start"))
async def start(event):
    
    sender = await event.get_sender()
    SENDER = str(sender.id)
    
    await event.reply('Hello!.')
    
##### MAIN
if __name__ == '__main__':
    try:
        print("Initializing Database...")
        # Connect to local database
        db_name = 'database.db'
        conn = sqlite3.connect(db_name, check_same_thread=False)
        # Create the cursor
        crsr = conn.cursor() 
        print("Connected to the database")

        # Command that creates the "customers" table 
        sql_command = """CREATE TABLE IF NOT EXISTS customers ( 
            id INTEGER PRIMARY KEY AUTOINCREMENT, 
            lname VARCHAR(200),
            fname VARCHAR(200), 
            note VARCHAR(200));"""
        crsr.execute(sql_command)
        print("All tables are ready")

        print("Bot Started")
        client.run_until_disconnected()

    except Exception as error:
        print('Cause: {}'.format(error))

config.ini 内容

; DO NOT MODIFY THE LINE BELOW!!! ([default])
[default]


; EDITABLE FIELDS:
api_id = 000000
api_hash = 000000000000000000
bot_token = 00000000000000000000000000

文件权限检查结果

-rw-r--rw- 1 root root 10601 Mar  4 00:52 script.py

-rw-rw-rw- 1 root root 716800 Mar  4 00:52 database.db

-rw-r--rw- 1 root root 195 Mar  4 00:52 config.ini

内容的提问来源于stack exchange,提问作者Marcatectura

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 15:13:09