# Getting the name of the Database Dynamically

**URL:** <https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147>\
**Category:** Ignition\
**Tags:** database, ignition81, perspective\
**Created:** [September 18, 2024, 7:22pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147 "2024-09-18T19:22:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farhan\_Basheer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/farhan_basheer/32/66759_2.png) [@Farhan\_Basheer](https://forum.inductiveautomation.com/u/Farhan_Basheer)\
**Post date:** [September 18, 2024, 7:22pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/1 "2024-09-18T19:22:35Z")

</div>

I have a Insert Query execution method. Which is being used to insert the data in the database.

```python
def onMessageReceived(self, payload):
	user = self.session.props.auth.user.userName
	if payload['confirm']:
		data = payload.get('data', [])
        
        # Prepare the insert data
        insert_data = [
            {
                'BrassTag': row['BrassTag'],
                'BrassTag_Desc': row['BrassTag_Desc'],
                'user': str(user)
            } for row in data if row['BrassTag']
        ]
        
        # Build the query and values
        q, v = build_insert_string("BrassTag", insert_data)
        
        # Debugging: Print the query and values
        system.perspective.print("Query: {}".format(q))
        system.perspective.print("Values: {}".format(v))
                
        # Specify the database name, it is important.
        database_name = 'test_mdcmaster'
        
        # Execute the query
        if q:
            system.db.runPrepUpdate(q, v, database_name)
            #self.refreshBinding('props.data')
        # Clear the table data after insert
        self.getChild("Tbl_Insert_New_BrassTag").custom.BrassTag_Ins = []
        self.getChild("Tbl_Insert_New_BrassTag").props.data = [{
            "BrassTag": {
                "editable": True,
                "style": {
                    "backgroundColor": "white"
                },
                "value": ""
            },
            "BrassTag_Desc": {
                "editable": True,
                "style": {
                    "backgroundColor": "white"
                },
                "value": ""
            }
        }]
        system.perspective.closePopup('Add_BrassTag')

```

In this Method I have Hardcoded the name of my Database, which is not a good practice because if I change my database then I have to update it in every script.  
`database_name = 'test_mdcmaster'`

I'm trying to get the name of the default database using this approach

```python
connectionInfo = system.db.getConnectionInfo()
dbType = connectionInfo.getValueAt(0, "Name")
system.perspective.print(dbType )

```

But not getting any results. Upon examining `connectionInfo` I have received `Perspective.Designer.Workspace -- Dataset [0R ? 10C]` What could be the issue here. Do I have to enable any settings?

---

<div class="post-metadata">

**Author:** ![calimero100582](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/calimero100582/32/18630_2.png) [@calimero100582](https://forum.inductiveautomation.com/u/calimero100582)\
**Post date:** [September 18, 2024, 7:32pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/2 "2024-09-18T19:32:10Z")

</div>

Depending on the scope, if you are in Vision or Perspective, Database is optional, and use the default.

Edit: link to the docs [system.db.runPrepUpdate | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runPrepUpdate#syntax---vision-and-perspective)

---

<div class="post-metadata">

**Author:** ![Farhan\_Basheer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/farhan_basheer/32/66759_2.png) [@Farhan\_Basheer](https://forum.inductiveautomation.com/u/Farhan_Basheer)\
**Post date:** [September 18, 2024, 7:34pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/3 "2024-09-18T19:34:41Z")

</div>

While creating this script, the query wasn't working without specifying the database name, that's why I had to hard code it in the script.

---

<div class="post-metadata">

**Author:** ![flexware](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/flexware/32/66243_2.png) [@flexware](https://forum.inductiveautomation.com/u/flexware)\
**Post date:** [September 18, 2024, 7:38pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/4 "2024-09-18T19:38:57Z")

</div>

There are a few ways you can do this and it depends on how you want your system to react when you change the database name.

1. You can create a new session custom property called dbConn and place your database name in there. Then, set your Named Query Database Connection to `<Parameter>` and pass in the session custom property as a parameter into your named query, something like

```python
params = {
    "database": self.session.custom.dbConn,
    .... other params here ...
}
system.db.runNamedQuery("path", params)

```

1. You can set a default database connection for a project, in the Project properties window. Then, you would specify the NQ Database Connection to be `<Default>`.  

2. You can create a memory tag that will hold your database connection name. This is especially useful if you need to execute a query in a global scope (e.g. tag change script) and want that database connection name to be dynamic as well.

---

<div class="post-metadata">

**Author:** ![Farhan\_Basheer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/farhan_basheer/32/66759_2.png) [@Farhan\_Basheer](https://forum.inductiveautomation.com/u/Farhan_Basheer)\
**Post date:** [September 18, 2024, 7:44pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/5 "2024-09-18T19:44:55Z")

</div>

What I did, I have defined a function in the `Scripting.Project Library`

```python
def projConName():
	connectionInfo = system.db.getConnections()
	dbType = connectionInfo.getValueAt(0, "Name")
	return dbType

```

and accessing the name wherever is needed.

```python

database_name = defaultDatabaseConnection.projConName()

```

Is this a good approach.

I have also figured out that the issue was with the function name, I had to use `getConnections()` not `getConnectionInfo()`

---

<div class="post-metadata">

**Author:** ![flexware](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/flexware/32/66243_2.png) [@flexware](https://forum.inductiveautomation.com/u/flexware)\
**Post date:** [September 18, 2024, 7:51pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/6 "2024-09-18T19:51:46Z")

</div>

I don't think `getConnections()` is getting the default project database... From what I can see in the [docs](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runPrepUpdate?gad_source=1&gclid=EAIaIQobChMInf7vvaHNiAMVmlz_AR2P_AXaEAAYASAAEgI0BfD_BwE#syntax---vision-and-perspective), it gets all of the connections that are defined on the gateway. It looks like you may only have one, so in this case it works, but if what I'm assuming is true, I don't believe this is a good future-proof way to accomplish your task.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/2/f2b0c838ef03f97eb18b1bbd5e88d088a9b0b802.png)

I'm interested to see why you have to pass in a database connection name if you have a project default database specified and you're calling `system.db.runPrepUpdate` from the Perspective scope. Can you add a screenshot of your project configuration, specifically the `"Default Database"` portion?

---

<div class="post-metadata">

**Author:** ![Farhan\_Basheer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/farhan_basheer/32/66759_2.png) [@Farhan\_Basheer](https://forum.inductiveautomation.com/u/Farhan_Basheer)\
**Post date:** [September 18, 2024, 7:55pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/7 "2024-09-18T19:55:08Z")

</div>

Sure, but where can i find that configuration in `Status` or `Config`?

---

<div class="post-metadata">

**Author:** ![flexware](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/flexware/32/66243_2.png) [@flexware](https://forum.inductiveautomation.com/u/flexware)\
**Post date:** [September 18, 2024, 7:57pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/8 "2024-09-18T19:57:08Z")

</div>

In the Designer, go to Project -\> Project Properties. Then take a screenshot of the Project / General page.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/4/14c8cdb14f8061446349ba0d7e88a127ce918730.png)

---

<div class="post-metadata">

**Author:** ![Farhan\_Basheer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/farhan_basheer/32/66759_2.png) [@Farhan\_Basheer](https://forum.inductiveautomation.com/u/Farhan_Basheer)\
**Post date:** [September 18, 2024, 7:59pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/9 "2024-09-18T19:59:11Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/f/ef0f242621c233053f03a188c4e6c37904a0504a.png)

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [September 18, 2024, 8:03pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/10 "2024-09-18T20:03:55Z")

</div>

If you notice, your default database connection is in italics, so it is not valid. That is probably why your calls were failing.

---

<div class="post-metadata">

**Author:** ![flexware](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/flexware/32/66243_2.png) [@flexware](https://forum.inductiveautomation.com/u/flexware)\
**Post date:** [September 18, 2024, 8:04pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/11 "2024-09-18T20:04:50Z")

</div>

So is `db` different from `test_mdcmaster`? Are those two different database connections? Also, as bschroeder pointed out, you either just changed those values and they haven't been saved yet or that field is not valid.

---

<div class="post-metadata">

**Author:** ![Farhan\_Basheer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/farhan_basheer/32/66759_2.png) [@Farhan\_Basheer](https://forum.inductiveautomation.com/u/Farhan_Basheer)\
**Post date:** [September 18, 2024, 8:06pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/12 "2024-09-18T20:06:36Z")

</div>

No, Db is nothing

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/1/41fbb71adfd07f6586e56f8a277a51cf5a7e1ab5.png)  
I only have 1 database connection established

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [September 18, 2024, 8:07pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/13 "2024-09-18T20:07:55Z")

</div>

Then you need to change your default database connection in the proj cr properties from db to the connection you want.

Then you should be able to remove the database name from your script and it'll use the default connection defined in the project properties.

---

<div class="post-metadata">

**Author:** ![flexware](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/flexware/32/66243_2.png) [@flexware](https://forum.inductiveautomation.com/u/flexware)\
**Post date:** [September 18, 2024, 8:08pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/14 "2024-09-18T20:08:53Z")

</div>

I would try to select that `test_mdcmaster` database in your Project Properties - it should show up in that dropdown. Its interesting that you don't have a connection called `db` but it appears there. Almost like it came from a gateway backup or a project export.

---

<div class="post-metadata">

**Author:** ![Farhan\_Basheer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/farhan_basheer/32/66759_2.png) [@Farhan\_Basheer](https://forum.inductiveautomation.com/u/Farhan_Basheer)\
**Post date:** [September 18, 2024, 8:23pm UTC](https://forum.inductiveautomation.com/t/getting-the-name-of-the-database-dynamically/96147/15 "2024-09-18T20:23:04Z")

</div>

@bschroeder @flexware Thanks to both of you the issue is fixed now. I don't have to specify the Database name in the script now.
