# Executing the command

To execute any command we have to use the `execute` class of the package. This class contains several tools for a proper & efficient execution of our command. To execute command:

```python
sqlite.execute(
    [
        ["command for table1_db1", "command for table2_db1"],
        ["command for table1_db2", "command for table2_db2"]
    ]
)
```

**Output:**

```python
result = [
    [
        "Result for table1_db1",
        "Result for table2_db1"
    ],
    [
        "Result for table1_db2",
        "Result for table2_db2"
    ]
]
```

**Analysis:**

The execution command will return a `numpy` array for faster processing & execution. This array consist of database wise result. For example `result[0]` will be the result for first database:

```python
[
    "Result for table1_db1",
    "Result for table2_db1"
]
```

This is the result for first database in the resultant `numpy` array. Further, the database array consist of results of commands executed on table. For example: The `result[0][1]` will be the result of second table of first database.

### Using the tools of execution for better analysis

The `execute` class provides several tools to help execute our command selectively and properly. These tools are often used to get different outputs on the same command execution. Sounds weird 😊, let's understand these tools.

#### returnDict

This parameter is used when we have to get the result in the form of dictionary where keys are the names of the databases. For example:

```python
sqlite.execute(
    "SELECT * FROM WORLD WHERE SOMEONE LIKE %YOU%",
    db="datab.db",
    returnDict=True
).get
```

It will return something like:

```python
{"datab.db": [["You"]]}
```

This can also be achieved by using the `dict` attribute:

```python
sqlite.execute(
    "SELECT * FROM WORLD WHERE SOMEONE LIKE %YOU%",
    db="datab.db"
).dict
```

It will return the same result. This is useful when we don't know the position of database and we want to get the result of database(s) by their name(s). By default it is `False`.

#### verbose

This parameter is used when we have to debug our query or database. This will log all the steps that are taken internally by the library so as to provide a good debug experience and let you know where your program crashed (in case of any bugs). For example:

```python
sqlite.execute(
    "SELECT * FROM WORLD WHERE SOMEONE LIKE %YOU%",
    db="datab.db",
    verbose=True
).get
```

It will log something like:

```
[8120] SQL-Tools: Starting execution
[8120] SQL-Tools: Parsing database(s)
[8120] SQL-Tools: Parsing commands
[8120] SQL-Tools: Connected
[8120] SQL-Tools: Creating the pointer
[8120] SQL-Tools: Executing command database: datab.db
[8120] SQL-Tools: Preparing results
[8120] SQL-Tools: Calculating time
```

Pattern: `[process id] SQL-Tools: Step`

&#x20;By default it is `False`.

#### err

Sometimes this parameter is life saver when we don't want our program to break in between due to any error. This will prevent all the errors raised by any function in the respective function. Surely, we can do this by using `exception handling` but it will take `at least` 4 lines of code and makes the program messy. It's good to provide this parameter and get rid of it. ***This parameter is available in every function of the library*****.** For example:

```python
sqlite.execute(
    "SHOW TABLES",
    db="datab.db"
)
```

This will raise error because the `SHOW` command is not available in SQLite. To prevent this we can use two approaches:

**Approach 1:**

```python
try:
    sqlite.execute(
        "SHOW TABLES",
        db="datab.db"
    )
except Exception:
    pass
```

**Approach 2:**

```python
sqlite.execute(
    "SHOW TABLES",
    db="datab.db",
    err=False
)
```

`Now, you can decide by yourself which approach is better.`  By default it is `True`.

#### simplify

This is not very useful parameter but can be used to prevent the first slicing of our result when we have only one database to work upon. It will automatically show the result for first database only. By default it is `False`. For example:

```python
sqlite.execute(
    "SELECT * FROM WORLD WHERE SOMEONE LIKE %YOU%",
    db="datab.db",
    simplify=True
).get
```

It will return something like:

```python
[
    "Result for table1_db1",
    "Result for table2_db1"
]
```

Instead of:

```python
[
    [
        "Result for table1_db1",
        "Result for table2_db1"
    ]
]
```

#### asyncExec

This parameter prove to be very useful when we have big queries and we want to process it in background. There are multiple approaches to do this. But all of them requires at least 6 lines of code to be written by you & makes the code messy (as always).

This can be done by simply passing this parameter as `True` and a `callback` function. This function us called when the command(s) get executed with the result as parameter.By default it is `False`.

```python
def callback(r):
    print(f"Result from callback: {r}")

sqlite.execute(
    "SELECT * FORM WORLD WHERE SOMEONE LIKE %YOU%",
    db="datab.db",
    asyncExec=True,
    callback=callback
)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yogesh-aggarwal.gitbook.io/sql-tools/sqlite/executing-the-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
