我刚刚使用flask,想做一个简单的webservice,代码如下:
@app.route('/compareimei',methods=['POST']) def hello_world(): #获取post的imei号 imei = request.form['imei'] sql = sqlite3.connect('UpdateInfo.db') cr = sql.cursor() try: #找到数据库中相应imei号对应的True or False值 value = cr.execute('select Enable from IMEIList where IMEI=?',[imei]).fetchone() if value != None: return value[0] else: return 'none' except sqlite3.OperationalError() as e: return e
逻辑比较简单,就是post imei号给127.0.0.1:8080/compareimei这个URL地址,获取返回值
结果在运行的时候,发现一直报错
OperationalError: no such table: IMEIList
而我的db文件和py文件的结构是这样的
于是我就在当前目录下,建立一个新的py文件,把数据库相关的代码写入,测试发现能正常获取到数据库中的值
再回到flask的py文件中,我发现如果db文件使用绝对路径,就正常了
@app.route('/compareimei',methods=['POST']) def hello_world(): #获取post的imei号 imei = request.form['imei'] #sql = sqlite3.connect('UpdateInfo.db') sql = sqlite3.connect('/Users/lizhao/Documents/untitled/UpdateInfo.db')** cr = sql.cursor() try: #找到数据库中相应imei号对应的True or False值 value = cr.execute('select Enable from IMEIList where IMEI=?',[imei]).fetchone() if value != None: return value[0] else: return 'none' except sqlite3.OperationalError() as e: return e
请问,为什么在flask的py文件中就必须使用绝对路径,而不能使用相对路径呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...