Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having problems with SQLite3 connection #398

Open
juanpuerto23 opened this issue May 2, 2022 · 7 comments
Open

Having problems with SQLite3 connection #398

juanpuerto23 opened this issue May 2, 2022 · 7 comments

Comments

@juanpuerto23
Copy link

Hello there! I have been trying to use the module for a personal project, but I haven't been able to get it to work, I think this is probably a misconfiguration from my environment from what I have been able to see online, but I still want to ask here so maybe I can get some help, as I haven't been able to find anything that can solve this. Of course, I have tried to do the obvious things like reinstalling everything back again, but still nothing changes. I am able to import the package all fine, but whenever I try to use it, it crashes showing the following message:

Traceback (most recent call last):
  File "C:\Users\Juan\Desktop\analisis-meteorologico\src\main.py", line 19, in <module>
    reg = owm.city_id_registry()
  File "C:\Users\Juan\AppData\Local\Programs\Python\Python310\lib\site-packages\pyowm\owm.py", line 96, in city_id_registry
    return cityidregistry.CityIDRegistry.get_instance()
  File "C:\Users\Juan\AppData\Local\Programs\Python\Python310\lib\site-packages\pyowm\commons\cityidregistry.py", line 29, in get_instance
    return CityIDRegistry(CITY_ID_DB_PATH)
  File "C:\Users\Juan\AppData\Local\Programs\Python\Python310\lib\site-packages\pyowm\commons\cityidregistry.py", line 21, in __init__
    self.connection = self.__decompress_db_to_memory(sqlite_db_path)
  File "C:\Users\Juan\AppData\Local\Programs\Python\Python310\lib\site-packages\pyowm\commons\cityidregistry.py", line 52, in __decompress_db_to_memory
    src_conn = sqlite3.connect(tmpf_name)
sqlite3.OperationalError: unable to open database file

Thanks!

@csparpa
Copy link
Owner

csparpa commented May 5, 2022

Hi @juanpuerto23 can you please tell me more about what OS you're running on and what Python version are you using?
Also please paste here the relevan piece of code giving the error. I guess it's when you call reg = owm.city_id_registry() ?

@geofbaum
Copy link

geofbaum commented May 15, 2022

@csparpa Just a note I recently updated to Python 3.10, on a Windows 10 machine and got a similar issue. I tested the newest version of pyowm on 3.8 and also got the same issue. For both I was able to solve it by decompressing the db file and removing the portion of the code in cityidregistry.py that dealt with decompressing it from bz2 and just calls the path of the cities.db file. I didn't have a chance to play around with it since then as I was working on something else with that part of the code or I would have raised an issue myself.

@csparpa
Copy link
Owner

csparpa commented May 17, 2022

I've tried to reproduce the issue on my Ubuntu 20.04 box either with Python 3.8.10 and 3.10 and it works fine for both.
I suspect the problem might be related to Windows setups only ?
@geofbaum SQLite DB compression is a must, otherwise the library size more than doubles!

@m0n1ker
Copy link

m0n1ker commented Aug 29, 2022

I ran into this same issue while trying to look at #404. This is an issue with how NamedTemporaryFile works on Windows. Quoted from the tempfile docs:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows)

In short, you can't call sqlite3.connect(FILE) inside of the with NamedTemporaryFile as FILE block. The easiest solution I can think of here is to pass delete=False into NamedTemporaryFile, do the connect/backup commands outside of the with, and remove the tempfile when finished. To ensure it won't leave temp files around if something goes wrong I would wrap this in a try/finally and put os.remove in the finally block. I can create a PR for this.

@CarlosS7
Copy link

CarlosS7 commented Oct 6, 2022

@m0n1ker I would like to try your solution. Where you able to create the PR?

@vincentxavier
Copy link

In short, you can't call sqlite3.connect(FILE) inside of the with NamedTemporaryFile as FILE block. The easiest solution I can think of here is to pass delete=False into NamedTemporaryFile, do the connect/backup commands outside of the with, and remove the tempfile when finished. To ensure it won't leave temp files around if something goes wrong I would wrap this in a try/finally and put os.remove in the finally block. I can create a PR for this.

If you had already create an PR, could you provide the link ?

@bachittle
Copy link

since the PR has never been made and the issue still exists, I made my own PR that fixes the issue as mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants