我有一个用python编写的警报脚本,它收集来自sqlite数据库的警报。
如果我执行这个脚本: python /home/pi/VRobot/alarm.V2.py,一切都很好。
当我将它放在/etc/profile中的执行中并在引导时运行它时,我会得到以下错误。
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: reminders [SQL: u'SELECT reminders.id AS reminders_id, reminders.activity AS reminders_activity, reminders.time AS reminders_time, reminders.day AS reminders_day, reminders.song AS reminders_song, reminders.timeofday AS reminders_timeofday \nFROM reminders'] (Background on this error at: http://sqlalche.me/e/e3q8)
/etc/profile脚本如下所示:
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
amixer set 'PCM' 100%
python /home/pi/VRobot/alarm.V2.py &对于为什么脚本在正常运行时运行良好,但在引导时执行时却不正常,有什么想法吗?
我这样做:
engine = create_engine('sqlite:///foo.db', echo=True)因此,我使用一个相对路径连接到数据库。
发布于 2018-04-15 18:30:50
您正在使用相对路径连接数据库;这使您依赖于当前的工作目录。从/etc/profile脚本运行时,当前的工作目录不同,此时连接到空数据库。
要么将当前工作目录更改为脚本的工作目录( shell代码中的cd或Python中的os.chdir() ),要么使用绝对路径。
您可以从__file__值生成一个绝对路径:
import os.path
HERE = os.path.dirname(os.path.abspath(__file__))
database_path = os.path.join(HERE, 'foo.db')
database_uri = 'sqlite:///{}'.format(database_path)然后使用create_engine(database_uri, echo=True)进行连接。
https://stackoverflow.com/questions/49843817
复制相似问题