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


addData( dataObject, domain )

Used to insert the data on a database.

list[DataObject]

cmd2( query, domain )

Execute the query on a database and return the result.

list[DataObject]

cmd2Remote( query, ip , domain )

Connect to other PowerScene servers, execute the query and return the result

boolean

commit( domain )

Save database changes.

 int

isConnect()

Return whether a database is connected.

  DataObject

lastInsertedData( tableName, domain )

Return the last inserted data on the table.

boolean

rollback( domain )

Cancel database changes.

boolean

transation( domain )

Users can quickly insert or update large amounts of data to a database.


updateData( dataObject, domain )

Used to update the data of databases.


  int isConnect()

 

  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.')





  list[DataObject] cmd2( query:Query , domain:master or history )

   

 cmd2 executes the query on a database and returns the result. If the type is select, it returns a DataObject list.


        ㆍ query : Enter a query such as CREATE, SELECT.

        ㆍ 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) Create the table on model databases

   


        ddl = 'CREATE TABLE [PCS_MEMBER] (  \

         [idx] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  \

          [id] NVARCHAR(51) DEFAULT (NULL) , \

          [pwd] NVARCHAR(51) DEFAULT (NULL) , \

          [desc] NVARCHAR(51) DEFAULT (NULL), \

             [date] DATETIME(0) DEFAULT (0) , \

        [memo] BLOB(0) DEFAULT(0) )  ;'

         dbm.cmd2(ddl)

       




  Example) Select the table of model databases

   


        dataList = dbm.cmd2('SELECT * FROM PCS_MEMBER')

        for data in dataList:

       print data.value('id'), data.value('pwd'), data.value('desc'), data.value('date')

       




  Example) Create the table on history databases

   


        ddl = 'CREATE TABLE [PCS_MEMBER] (  \

         [idx] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  \

          [id] NVARCHAR(51) DEFAULT (NULL) , \

          [pwd] NVARCHAR(51) DEFAULT (NULL) , \

          [desc] NVARCHAR(51) DEFAULT (NULL), \

             [date] DATETIME(0) DEFAULT (0) , \

        [memo] BLOB(0) DEFAULT(0) )  ;'

         dbm.cmd2(ddl , 'history' )




  Example) Select the table of history databases

   


        dataList = dbm.cmd2('SELECT * FROM PCS_MEMBER' , 'history')

        for data in dataList:

       print data.value('id'), data.value('pwd'), data.value('desc'), data.value('date')






  list[DataObject] cmd2Remote( query:Query , ip:IP address of other PowerScene server, domain:master or history )



  Example)

   

        # Connect the PowerScene server executing on 222.222.60.13 and obtain the data.

        dataList = dbm.cmd2Remote('SELECT * FROM PCS_MEMBER','222.222.60.13','master')

        for data in dataList:

       print data.value('id'), data.value('pwd'), data.value('desc'), data.value('date')

       






  addData( data:DataObject, domain:master or history )

 

  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’)


       



  updateData( data:DataObject, domain:master or 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’)


       



  DataObject lastInsertedData( table:Table name, domain:master or history )

   

  When entering the table name and database, it returns the last inserted data.


        ㆍ table : Enter the name of tables.

        ㆍ 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.


        ㆍ Return value

                - Return DataObject objects.

                - DataObject object has the last inserted data.



  Example) Obtain the last inserted data of model databases

   


        dbData = dbm.lastInsertedData(' PCS_MEMBER’)

        if dbData.isEmpty() == 0:

               app.messageBox('The last inserted data.')

        else:

               app.messageBox('No inserted data.')


           



  Example) Obtain the last inserted data of history databases

   


        dbData = dbm.lastInsertedData(' PCS_MEMBER’, ‘history’)

        if dbData.isEmpty() == 0:

               app.messageBox('Te last inserted data.')

        else:

               app.messageBox('No inserted data.')


         



  boolean transaction( domain:master or 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')





  boolean commit( domain:master or 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()





  boolean rollback( domain:master or history )

 

  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()