Qpython3, sqlite3 can't connect to database

大憨熊 提交于 2019-12-11 07:38:13

问题


I'm beginner and I just try qpython for android.

I try to connect database at qpython3 by using sqlite3: My code

import sqlite3
conn=sqlite3.connect('mydatabase.db')

But it raises error and unable to open database file.

Any solution for this?? If I try at pc, it automatically create a database if not exists


回答1:


The reason it doesn't work is that QPython programs are run from '/' directory which of course is not writable to non-root users. You can check this with the following code run from the console.

import os
print(os.getcwd())

If you go to the ftp utility in the About menu, you will find a directory path that is being used by QPython3. On my HTC mobile it is:

/storage/emulated/0/com.hipipal.qpyplus

So I changed your example code to:

import os
import sqlite3
RootPath='/storage/emulated/0/com.hipipal.qpyplus'
conn=sqlite3.connect(os.path.join(RootPath,'mydatabase.db'))

and it works fine for me.

I also found that it is necessary to commit changes or they won't be written to the file. That is, end programs with:

conn.commit()
conn.close()


来源:https://stackoverflow.com/questions/42505294/qpython3-sqlite3-cant-connect-to-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!