访问Ballerina中SQL Select查询返回的数据

时间:2018-07-08 08:41:48

标签: ballerina

我是芭蕾舞女演员的新手。在我的项目中,我想访问SQL选择查询返回的数据吗?

我想做的是

public function calculateCholesterol(int age,string gender,int totalCholesterol,int non_hd,int ldl,int hdl){

json data;

string sqlQuery;

sqlQuery = "SELECT * FROM CHOLESTEROL WHERE gender = ? AND age = ?";

var result = cholesterolDB->select(sqlQuery,(),gender,selectedAge);

table<Cholesterol> cholTable = table{};

match result {

    table dataTable =>{
         //io:println(check <json>dataTable);
        var res = cholTable.add(dataTable);
        io:println(res);
    }

    error err =>{
        data  = {"Status":"Data Not Found","Error":err.message};
    }
}
}

但是它不起作用,并引发运行时错误。

2 个答案:

答案 0 :(得分:2)

match result {

        table dataTable => {

            data = check <json>dataTable;
            correctData[0] = check <int>data[0].totalCholesterol;
            correctData[1] = check <int>data[0].non_hd;
            correctData[2] = check <int>data[0].hdl;
            correctData[3] = check <int>data[0].ldl;


        }

        error err => {
            data  = {"Status":"Data Not Found","Error":err.message};
        }
}

答案 1 :(得分:1)

您还可以执行以下操作。

table<Cholesterol> cholTable;

match result {

    table dataTable => cholTable = dataTable;

    error err => // handle error
}

// Now you can perform any required operation on the cholTable. 
// eg: You can iterate it, convert to JSON/XML. 

foreach row in cholTable {
   io:println("Info:" + row.totalCholesterol);
}

请注意,除非您在loadToMemory操作中设置了select参数,否则无法重新迭代从SQL操作返回的表。如果执行以下操作,则可以多次迭代。

例如:

var selectRet = testDB->select("SELECT * FROM student", Student, loadToMemory = true);

有关更多信息,请参考jdbc client示例。