Search code examples
pythonsqltools

Unable to get result on multiple databases (SQL-Tools)


I have to get mixed result from the databases but this code is raising an error:

from sql_tools import sqlite

sqlite.connect(["main.db", "base.sqlite3"])
data = sqlite.execute(["SELECT * FROM PREFERENCES", "SELECT * FROM USERS"]).get
sqlite.disconnect()

Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Hunny\AppData\Local\Programs\Python\Python38\lib\site-packages\sql_tools\sqlite\execute.py", line 28, in execute
    obj.execute()
  File "C:\Users\Hunny\AppData\Local\Programs\Python\Python38\lib\site-packages\sql_tools\internals.py", line 188, in execute
    self.__command = self.__parseCommands()
  File "C:\Users\Hunny\AppData\Local\Programs\Python\Python38\lib\site-packages\sql_tools\internals.py", line 270, in __parseCommands
    raise e
  File "C:\Users\Hunny\AppData\Local\Programs\Python\Python38\lib\site-packages\sql_tools\internals.py", line 260, in __parseCommands
    raise exception.UnknownError(
sql_tools.exception.UnknownError: Database and commands are not commuting, n(commands) != n(database)

Can anybody tell me how to solve this?


Solution

  • It's because when you connect multiple databases, the execute function expects you to pass the commands as lists of list where each element of list is the list of commands to be executed on each database. You can read more at documentation.

    Corrected Code:

    from sql_tools import sqlite
    
    sqlite.connect(["main.db", "base.sqlite3"])
    data = sqlite.execute([["SELECT * FROM PREFERENCES"], ["SELECT * FROM USERS"]]).get
    sqlite.disconnect()