您的位置:首页 > 数据库 > SQL

sqlite3.OperationalError: unable to open database file

2016-01-21 14:43 661 查看
# -*- 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


对数据库文件要有读写的权限;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sqlite3 数据库 python