Announcement

Collapse
No announcement yet.

Need to store the database result in a variable so that I can use the variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need to store the database result in a variable so that I can use the variable

    Hello ,
    I am working on a requirement where in I am running a query with the help of javascript and now i am stuck.

    I want to store the output of that query into a variable so that I can use the same variable to other destinations. Following is the script that i have written

    var dbConn;
    var insID;
    var pid = $('PID');
    //logger.info(pid);

    {
    dbConn = DatabaseConnectionFactory.createDatabaseConnection ('org.postgresql.Driver','jdbcostgresql://localhost:5432/mirthdb','postgres','27483914');


    var sql = "SELECT insuranceid FROM public.insurance where patientid = '+pid+'";

    I want to store the output of this query into a variable. Can you p lease help me with the same.

  • #2
    This should work, it's a basic skeleton for handling query results.
    note: you can expand on this to process more results.
    each result will be result.getString(1) then result.getString(2) etc
    will be the values from multiple columns in the result, then it will loop to the next row of the result.
    Code:
    var dbConn;
    var insID;
    var pid = $('PID');
    //logger.info(pid);
    
    dbConn = DatabaseConnectionFactory.createDatabaseConnection('org.postgresql.Driver','jdbc:postgresql://localhost:5432/mirthdb','postgres','27483914');
    
    
    var sql = "SELECT  insuranceid FROM public.insurance where patientid = '+pid+'";
    
      result = dbConn.executeCachedQuery(sql);    //Run the query and get the results 
        var cnt = result.size();    //get the number of rows returned in the query 
        for (i = 0; i < cnt; i++) {     //loop it up 
            result.next();      //go to the first row     
         
            var foundInsuranceID = result.getString(1);    //get the first item in the results 
        }

    Comment


    • #3
      !!Thank you !!

      Comment

      Working...
      X