dbm
dbm(Database management)
dbm object is used to query, insert, delete, update the data in the database. dbm object allows to the user executing a query and creating new tables or setting the value in the database.
Methods
dbm object executes a query by separating history databases and model databases into domains.
(※ DataObject is introduced on 17.3.)
Return Type |
Method |
Description |
Used to insert the data on a database. |
||
Execute the query on a database and return the result. |
||
Connect to other PowerScene servers, execute the query and return the result |
||
boolean |
Save database changes. |
|
int |
Return whether a database is connected. |
|
Return the last inserted data on the table. |
||
boolean |
Cancel database changes. |
|
boolean |
Users can quickly insert or update large amounts of data to a database. |
|
Used to update the data of databases. |
isConnect checks whether a database is connected. ㆍ Return value - TRUE : Return TRUE if the database is connected. - FALSE : Return FALSE if the database is not connected. Example)
result = dbm.isConnect() # Check whether the database is connected. # Connected if( result == 1 ): app.messageBox('Connected.') # Not Connected else: app.messageBox('Not connected.') |
addData inserts the data on the database. ㆍ data : Enter the DataObject object. First, set the data to add on DataObject objects. ㆍ domain : Enter master or history. The default is master. If entering a master, it connects to the model database, and if entering a history, it connects to the history database. Example) Insert data to model databases
dbData = DataObject() dbData.setObjectType(‘PCS_MEMBER’) dbData.setValue('id', ‘admin’) dbData.setValue('pwd', ‘1234’) dbData.setValue('decript',’Administrator’) dbm.addData(dbData)
|
Example) Insert data to history databases
dbData = DataObject() dbData.setObjectType(‘PCS_MEMBER’) dbData.setValue('id', ‘admin’) dbData.setValue('pwd', ‘1234’) dbData.setValue('decript',’Administrator’) dbm.addData(dbData, ‘history’)
|
updataData updates the data to the database. ㆍ data : Enter the DataObject object. First, set the data to add on DataObject objects. ㆍ domain : Enter master or history. The default is master. If entering a master, it connects to the model database, and if entering a history, it connects to the history database. Example) Update the data on model databases
dbData = DataObject() dbData.setObjectType(‘PCS_MEMBER’) dbData.setValue(‘idx’, 1) # Set the column value of primary key dbData.setValue('id', ‘admin’) dbData.setValue('pwd', ‘p1234’) dbData.setValue('desc', ’Main Administrator’) dbm.updateData(dbData)
|
Example) Update the data on history databases
dbData = DataObject() dbData.setObjectType(‘PCS_MEMBER’) dbData.setValue(‘idx’, 1) # Set the column value of primary key dbData.setValue('id', ‘admin’) dbData.setValue('pwd', ‘p1234’) dbData.setValue('desc', ’Main Administrator’) dbm.updateData(dbData, ‘history’)
|
transaction is used to process multiple steps of tasks in a database as one. It is able to insert or update large amounts of data rapidly. If transaction is completed, it is able to save changes with the commit method, and cancel changes with the rollback method. ㆍ domain : Enter master or history. The default is master. If entering a master, it connects to the model database, and if entering a history, it connects to the history database. Below is an example of reading the data of log.csv and insert, update the data on SCADA_MODEL_ANALOG_HISTORY_1.
Example)
import csv c = csv.reader(open("log.csv", "rb")) dbm.transaction('history') for row in c: pid=row[0] time=row[1] curvalue=row[2] deltavalue=row[3] dbm.cmd2("insert into SCADA_MODEL_ANALOG_HISTORY_1(pid, time, curvalue, deltavalue) values(%d, '%s', %f, %f)" %(int(pid), time, curvalue, deltavalue), 'history') dbm.cmd2("update SCADA_MODEL_ANALOG_HISTORY_1 set deltavalue=%f where time='%s' and pid='%s'"%(deltavalue, time, pid), 'history') dbm.commit('history') |
If transation is completed, it is able to save database changes with the commit method. ㆍ domain : Enter master or history. The default is master. If entering a master, it connects to the model database, and if entering a history, it connects to the history database. Example)
dbm.commit() |
If an error occurred, it is able to cancel database changes with the rollback method. If one failure occurred in the middle, it is available. ㆍ domain : Enter master or history. The default is master. If entering a master, it connects to the model database, and if entering a history, it connects to the history database. Example)
dbm.rollback() |