sqlite3.OperationalError: unable to open database file

# -*- coding:utf-8 -*-  

'''
Created on 2016年1月12日
@author: Lux
'''

import sqlite3

chromeHistoryDbPath = 'C:\Users\Lux\Desktop\History'

class GetDbContent():
    def __init__(self,path):
        self.dbpath = path

    def ReadTable(self):
        con = sqlite3.connect(self.dbpath)
        cur = con.cursor()
        tableList = cur.execute("select name from sqlite_master where type = 'table' order by name").fetchall()
        for table in tableList:
            print table[0]
        cur.close()

if __name__ == '__main__':
    a = GetDbContent(chromeHistoryDbPath)
    a.ReadTable()

使用python連接sqlite數據庫的文件的時候,不小心出現了錯誤

sqlite3.OperationalError: unable to open database file

百度了一下,錯誤有許多種,這裏稍微列舉一些

  • 數據庫路徑最好寫成絕對路徑,並且目錄要存在,而且 對目錄要有讀寫的權限, 因爲打開數據庫的時候,會產生臨時數據;
  • 在Win 7 enterprise 和 Win Xp Pro上面寫python v2.7時, 'C:\Users\Lux\Desktop\History' 路徑 有時候要寫成 'C:\\Users\\Lux\\Desktop\\History'
  • 有種情況我也是無法解釋的,某些時候你的數據庫文件後綴名不是 db 也不行,需要改名爲 xxx.db
  • 對數據庫文件要有讀寫的權限;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章