20190218-學習python使用shelve遇到raise error, “db type could not be determined”

老漢學IT發表於2019-02-18

照書上敲程式碼,執行了提示raise error, “db type could not be determined”,現場如下:

ubuntu-vm:~/code/massageboard$ python
Python 2.7.6 (default, Nov 13 2018, 12:45:42) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shelve
DATA_FILE=`guestbook.dat`
>>> DATA_FILE=`guestbook.dat`
>>> 
>>> def save_data(name,comment,create_at):
...     database=shelve.open(DATA_FILE)
...     if `greeting_list` not in database:
...         greeting_list=[]
...     else:
...         greeting_list=database[`greeting_list`]
...     greeting_list.insert(0,{
...         `name`:name,
...         `comment`:comment,
...         `create_at`:create_at,
...     })
...     database[`greeting_list`]=greeting_list
...     database.close()
... 
>>> import datetime
>>> save_data(`test`,`test comment`,datetime.datetime(2019,02,18,10,0,0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in save_data
  File "/usr/lib/python2.7/shelve.py", line 239, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/usr/lib/python2.7/shelve.py", line 223, in __init__
    Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
  File "/usr/lib/python2.7/anydbm.py", line 82, in open
    raise error, "db type could not be determined"
anydbm.error: db type could not be determined
>>> 

故障排除過程:

ubuntu-vm:~/code/massageboard$ ls
guestbook.dat  guestbook.py  guestbook.pyc  index.html  main.css  __pycache__
ubuntu-vm:~/code/massageboard$ rm guestbook.dat     #刪除資料儲存檔案
ubuntu-vm:~/code/massageboard$ ls
guestbook.py  guestbook.pyc  index.html  main.css  __pycache__


ubuntu-vm:~/code/massageboard$ python Python 2.7.6 (default, Nov 13 2018, 12:45:42) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import datetime >>> from guestbook import save_data >>> save_data(`test`,`test comment`,datetime.datetime(2019,02,18,10,0,0)) >>>

 

相關文章