Wednesday, May 29, 2019

Insert byte[] as a direct value to MySQL update operation in Ballerina

Ballerina version used: 0.992.0

import ballerina/io;
import ballerina/mysql;
import ballerina/sql;
mysql:Client testDB = new({
host: "localhost",
port: 3306,
name: "",
username: "root",
password: "root",
poolOptions: { maximumPoolSize: 5 },
dbOptions: { useSSL: false }
});
public function main(string... args) {
createDatabase();
var ret = testDB->update("CREATE TABLE testdb.bittypes(
id INT,
bitVal1 BIT(1),
bitVal2 BIT(64),
PRIMARY KEY (id)
)");
handleUpdate(ret, "Create bittypes table");
int id = 1;
boolean bitVal1 = true;
byte[] bitVal2 = [1, 2];
var ret = testDB->update("INSERT INTO testdb.bittypes(
row_id,
bit_val_1,
bit_val_2
) VALUES (
?,
?,
?
)", id, bitVal1, bitVal2);
handleUpdate(ret, "Update bittypes table");
dropDatabase();
}
function createDatabase() {
var ret = testDB->update("CREATE DATABASE testdb");
handleUpdate(ret, "Create database testdb");
}
function dropDatabase() {
var ret = testDB->update("DROP DATABASE testdb");
handleUpdate(ret, "Drop Database testdb");
}
function handleUpdate(sql:UpdateResult|error returned, string message) {
if (returned is sql:UpdateResult) {
io:println(message + " status: " + returned.updatedRowCount);
} else {
io:println(message + " failed: " + <string>returned.detail().message);
}
}

1 comment:

  1. The main motive of the AWS Big data consultant is to spread the knowledge so that they can give more big data engineers to the world.

    ReplyDelete