使用python的sqlite中的数据库错误

2024-04-29 19:34:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在sqlite数据库中遇到一个错误。该脚本在windows中运行良好,但在linux中运行不好。你知道吗

我试图从两个数据库中获取表名(每个表中只有一个表),并对这些表进行select查询。你知道吗

代码-

db1 = r"C:\Users\X\Documents\sqlitedb\8007.db"
db2 = r"C:\Users\X\Documents\sqlitedb\8008.db"

conn = sqlite3.connect(db1)
conn2 = sqlite3.connect(db2)
conn.execute("ATTACH '{}' AS db2".format(db2))

res = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
for name in res:
    tblname1 = name[0]
    #print(tblname1)

res2 = conn2.execute("SELECT name FROM sqlite_master WHERE type='table';")
for name in res2:
    tblname2 = name[0]
    #print(tblname2)

res1 = conn.execute("""SELECT * FROM main."""+tblname1+"""
                   WHERE IDKey NOT IN
                     (SELECT IDKey FROM db2."""+tblname2+""")
                """).fetchall()
res2 = conn.execute("""SELECT * FROM db2."""+tblname2+"""
                   WHERE IDKey NOT IN
                     (SELECT IDKey FROM main."""+tblname1+""")
                """).fetchall()

错误是-

select_mismatched_files """).fetchall() sqlite3.OperationalError: near "SELECT": syntax error

错误在这行--

res2 = conn.execute("""SELECT * FROM db2."""+tblname2+"""
                   WHERE IDKey NOT IN
                     (SELECT IDKey FROM main."""+tblname1+""")
                """).fetchall()

非常感谢您的帮助。你知道吗


Tags: namefromexecutesqlite错误connwhereselect
1条回答
网友
1楼 · 发布于 2024-04-29 19:34:01

评论文字(已更正):

查德(我):

No. Let me try.

Prithviraj Mitra公司:

If I use single quotes, I'm getting a red mark in my editor. Probably this line is not correct:

res2 = conn.execute('SELECT * FROM db2.'+tblname2+' WHERE IDKey NOT IN(SELECT IDKey FROM main.'+tblname1)).fetchall()

查德:

I think this line should be like this (closing ) bracket wasn't enclosed in '):

res2 = conn.execute('SELECT * FROM db2.'+tblname2+' WHERE IDKey NOT IN(SELECT IDKey FROM main.'+tblname1+')').fetchall()

Prithviraj Mitra公司:

It worked. Many thanks.

我回答这个问题是为了让其他有同样问题的用户可以很容易地确定所提供的答案。你知道吗

相关问题 更多 >