有没有一种优雅的方法可以在sqlite3的帮助下在python中读取SQL-Dump文件?

问题描述 投票:0回答:1

我有一个 sql 转储文件,其中包含我需要查询的数据库。

问题是我没有本地数据库安装,并且由于它的限制而无法安装。

我可以通过 sqlite3 中的 python 读取我的转储吗?我找不到关于如何执行此操作的正确解释。

对 stackoverflow 来说还很陌生。

我使用了在网上找到的这个代码片段来迭代我的转储,并至少获得所有表名作为回报:

table_list=[]
with open(dmp.file ,encoding='cp437') as f:
    for line in f:
        line = line.strip()
        if line.lower().startswith('create table'):
            table_name = re.findall('create table `([\w_]+)`', line.lower())
            table_list.extend(table_name)
            
for x in table_list:
    print(x)   

这工作得很好,但是在我用于创建表等的转储语句中,要跨越多行,所以这种方法不再能很好地工作

python sqlite
1个回答
0
投票

我编写此代码片段是为了将所有语句每行写入一行。

currentLine = ""
with open(File,encoding='cp437') as f:
for line in f:
    line = line.strip()
    currentLine = currentLine + " " + line
    if line.lower().endswith(';') == True:
        with open(NewFileOneLiner.txt', "a", encoding="utf-8") as g:
            g.write(currentLine.lstrip() + '\n')
            currentLine = ""
© www.soinside.com 2019 - 2024. All rights reserved.