# Accessing data from database

**URL:** <https://forum.inductiveautomation.com/t/accessing-data-from-database/70360>\
**Category:** General Discussion\
**Tags:** ignition81\
**Created:** [February 27, 2023, 2:39pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360 "2023-02-27T14:39:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan\_mueller](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@dan\_mueller](https://forum.inductiveautomation.com/u/dan_mueller)\
**Post date:** [February 27, 2023, 2:39pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360/1 "2023-02-27T14:39:03Z")

</div>

how do i access individual data from a database,ex c:3,r:4

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [February 27, 2023, 2:47pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360/2 "2023-02-27T14:47:30Z")

</div>

Assuming you are trying to access this data via a database query, you'll need to do an intro course online on using SQL in the database flavor that you are using (MySQL, Postgres, etc...).

> **[SQL Tutorial](https://www.w3schools.com/sql/default.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

A very basic SQL query basically is composed with SELECT, FROM, and WHERE keywords.

```sql
SELECT part 
FROM parts_table
WHERE part = 1234

```

Or do you mean accessing the data in an Ignition table that originates from a db query?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 27, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360/3 "2023-02-27T14:49:28Z")

</div>

Regarding the Ignition setting up of database connections (though I highly recommend taking all the courses in the credentials path here)

> **[Inductive University Course List - Learn Ignition Free](https://inductiveuniversity.com/courses/ignition/whats-new-in-ignition-81/8.1)**
>
> Learn the fundamentals and advanced features of Ignition while earning your IU credential

---

<div class="post-metadata">

**Author:** ![dan\_mueller](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@dan\_mueller](https://forum.inductiveautomation.com/u/dan_mueller)\
**Post date:** [February 27, 2023, 2:57pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360/4 "2023-02-27T14:57:00Z")

</div>

can you access the database to get one value. I am trying to do an hour by hour chart. the database only contains one column of production data for two shifts

---

<div class="post-metadata">

**Author:** ![reed.hoffmaster](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@reed.hoffmaster](https://forum.inductiveautomation.com/u/reed.hoffmaster)\
**Post date:** [February 27, 2023, 3:27pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360/5 "2023-02-27T15:27:33Z")

</div>

I do agree to look through the documentation proposed. To do what you're wanting to do there are likely very simple ways by setting up your chart component data source correctly. However, to get you experimenting I'll provide some examples.

Assuming you are doing this with with scripting, I have a couple of examples below. The first is by explicitly grabbing the column that you want by name, and the other is by index. In the example below I've run a query using system.db.runQuery, which returns a PyDataset, and inside of a loop I've declared a variable as the column I need. The query has already been set up to return exactly 1 row. The other method is by accessing it with the index of the 2D dataset ( [0][0] ).

Documentation on scripting method used: [system.db.runQuery - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.db.runQuery)

(Not Tested)

```auto
# Query to return 1 row
resultSet = system.db.runQuery("SELECT top (1) * FROM TableName")

# Prints info about the PyDataset returned by the system.db.runQuery method
print "Result Set:", resultSet

for row in resultSet:
	# Explicit access by column name
	result = row["ColumnName"]
	print "Result:", result

# Access by Index, [0]first row of dataset, [2]third column of dataset
print "Result:", result[0][2]

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [February 27, 2023, 5:48pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360/6 "2023-02-27T17:48:11Z")

</div>

You'll probably want to return a value for each hour from the same query. If you can share an example of what the database table data looks like, and what you expect the final results should look like, you can get more help.

---

<div class="post-metadata">

**Author:** ![dan\_mueller](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@dan\_mueller](https://forum.inductiveautomation.com/u/dan_mueller)\
**Post date:** [March 2, 2023, 1:55pm UTC](https://forum.inductiveautomation.com/t/accessing-data-from-database/70360/8 "2023-03-02T13:55:24Z")

</div>

SELECT sum (actual\_poured) as molds\_poured  
,shift  
,hour  
FROM [RedLions].[dbo].[HWS\_Production]  
where date=cast(getdate()-1 as date) and shift='1' and hour ='6' and part\_number != 'cc9020'  
group by shift, hour
