(4) Working With MYSQL :-
we will talk about :-
  • Query().
  • ADD2DB().
  • RESULT().
  • DELETE().
  • UPDATEDB()

How To connect to MySql Database:-
  • (1) Create An Object from Dragon-mysql-Ext in the route.ring file :-
    oDMySql = New DMySql
  • (Or) Use Importlib(cLibName) in manage.ring file to import the lib to
    "Batch/ImportedLibs" file then load it in Route.ring file...
  • (2) Make Sure To Set This Connection Values Like
    oDMySql = New DMySql
          oDMySql{ mysql_connection = [
          :host     = "localhost" ,
          :port     = null        ,
          :user 	  = "Mohannad"  ,
          :pass 	  = "" 	        ,
          :db_name  = "Dragon_Ring"
        ]
    }

Query(Statement)

This function take one parameter contain sql statement.


ADD2DB(Table_name , Table_values).

This function take two parameters as :-

  • Table_name :- the name of the table and tables columns if you want ...
  • Table_values :- list of values you want to add to mysql database .

For example:-

#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
oDMySql = New DMySql
    oDMySql{ mysql_connection = [
    :host     = "localhost" ,
    :port     = null        ,
    :user 	  = "Mohannad"  ,
    :pass 	  = "" 	        ,
    :db_name  = "Dragon_Ring"
  ]
ADD2DB('employees(id , name , salary)' , [1, "Mohannad" , 1800] )
}

RESULT().
This function do not need any parameters
For example :-
#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
oDMySql = New DMySql
      oDMySql{ mysql_connection = [
      :host     = "localhost" ,
      :port     = null        ,
      :user 	  = "Mohannad"  ,
      :pass 	  = "" 	        ,
      :db_name  = "Dragon_Ring"
    ]
/* This code has written in route.ring (just for testing) */
Query('SELECT * From employees ;')
res = RESULT()
for col=1 to len(res)
    for row=1 to len(res[col])
        html(res[col][row] + TAB) 
    next
    html('<br/>')
next
}
The result is :-
      id        name        salary
      1       Mohannad       1800  

DELETE(table_name , column_name , column_value).

This function take three parameters :-

  • table_name :- The name of the table .
  • column_name :- The parimary key column like ( id ) .
  • column_value :- The Value of the parimary key (column_name).
#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
oDMySql = New DMySql
      oDMySql{ mysql_connection = [
      :host     = "localhost" ,
      :port     = null        ,
      :user 	  = "Mohannad"  ,
      :pass 	  = "" 	        ,
      :db_name  = "Dragon_Ring"
    ]
/* This code has written in route.ring (just for testing) */
DELETE('employees' , 'id' , 1 ) 
/* This code will delete the id,name,salary from database for the employee id 1 */
}
UPDATEDB(table_name,column_name,column_value,columns_names,columns_values) This function take five parameters :-
  • table_name:- The name of the Table .
  • column_name :- the name of the column you want to update by .
  • column_value :-The Value of (column_name).
  • columns_names :- list of columns that you want to update.
  • columns_values :- list of values that you want to update (depending columns_names)
#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
oDMySql = New DMySql
      oDMySql{ mysql_connection = [
      :host     = "localhost" ,
      :port     = null        ,
      :user 	  = "Mohannad"  ,
      :pass 	  = "" 	        ,
      :db_name  = "Dragon_Ring"
    ]
/* This code has written in route.ring (just for testing) */
table_columns = [ "id" , "name" , "salary" ] 
Table_values  = [ 2 , "Mohannad Aldulaimi" , 1700]
UPDATEDB('employees', 'id' , 1 ,table_columns, Table_values)
this function will make a sql statement like this :-
(UPDATE 'employees' SET id=2 ,name="Mohannad Aldulaimi" , salary=1700 where id=1)

I hope it was easy for you ...