Repository: ranaroussi/yfinance

Issue:
DatabaseError('database disk image is malformed')
### Problem
I cannot download historical stock data with yfinance

### Simple code that reproduces your problem
data = yf.download("SPY", start="2017-01-01", end="2017-04-30")

### Full debug logging enabled output
DEBUG    Entering download()
DEBUG     Disabling multithreading because DEBUG logging enabled
DEBUG     Entering history()
ERROR     
          1 Failed download:
ERROR     ['SPY']: DatabaseError('database disk image is malformed')
DEBUG     ['SPY']: Traceback (most recent call last):
            File "C:\Users\harry\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\yfinance\multi.py", line 273, in _download_one
              data = Ticker(ticker, session=session).history(
            File "C:\Users\harry\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\yfinance\utils.py", line 105, in wrapper
              result = func(*args, **kwargs)
            File "C:\Users\harry\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\yfinance\base.py", line 153, in history
              tz = self._get_ticker_tz(proxy, timeout)
            File "C:\Users\harry\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\yfinance\base.py", line 1409, in _get_ticker_tz
              cache = utils.get_tz_cache()
            File "C:\Users\harry\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\yfinance\utils.py", line 1045, in get_tz_cache
              _tz_cache = _TzCache()
            File "C:\Users\harry\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\yfinance\utils.py", line 958, in __init__
              self._tz_db = _KVStore(_os.path.join(self._db_dir, "tkr-tz.db"))
            File "C:\Users\harry\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\yfinance\utils.py", line 900, in __init__
              self.conn.execute('pragma journal_mode=wal')
          sqlite3.DatabaseError: database disk image is malformed
          
DEBUG    Exiting download()

### Version
- `yfinance` version: 0.2.21
- Python version: 3.9.13
- Operating system type: Window 11



Discussion:

Resolving patch:
diff --git a/yfinance/utils.py b/yfinance/utils.py
--- a/yfinance/utils.py
+++ b/yfinance/utils.py
@@ -955,7 +955,11 @@ class _TzCache:
     def __init__(self):
         self._setup_cache_folder()
         # Must init db here, where is thread-safe
-        self._tz_db = _KVStore(_os.path.join(self._db_dir, "tkr-tz.db"))
+        try:
+            self._tz_db = _KVStore(_os.path.join(self._db_dir, "tkr-tz.db"))
+        except _sqlite3.DatabaseError as err:
+            raise _TzCacheException("Error creating TzCache folder: '{}' reason: {}"
+                                    .format(self._db_dir, err))
         self._migrate_cache_tkr_tz()
 
     def _setup_cache_folder(self):
@@ -1044,10 +1048,10 @@ def get_tz_cache():
             try:
                 _tz_cache = _TzCache()
             except _TzCacheException as err:
-                logger.error("Failed to create TzCache, reason: %s. "
-                             "TzCache will not be used. "
-                             "Tip: You can direct cache to use a different location with 'set_tz_cache_location(mylocation)'",
-                             err)
+                get_yf_logger().info("Failed to create TzCache, reason: %s. "
+                                 "TzCache will not be used. "
+                                 "Tip: You can direct cache to use a different location with 'set_tz_cache_location(mylocation)'",
+                                 err)
                 _tz_cache = _TzCacheDummy()
 
         return _tz_cache


Test patch: