Join us today!

Notifications
Clear all

Database Server Write DateandTime

2 Posts
1 Users
1 Reactions
477 Views
runtimevictor
Posts: 156
Topic starter
(@runtimevictor)
Estimable Member
Joined: 2 years ago

Hello,

I am writing in MariaDB database similar to MySQL with TwinCAT and I define the string with:

TYPE ST_Register :
STRUCT
Id : DINT;
Name : STRING(30);
Age : INT;
Height : LREAL;
Glasses : STRING(6);

dt_DateAnd_Time : DT;

END_STRUCT
END_TYPE

VAR

stReg : ST_Register;

fbCommand : FB_FormatString;

sCommandAddReg : STRING(255);

END_VAR

fbCommand(
sFormat:= 'INSERT INTO `people` VALUES(%D,$'%S$',%D,%1.2F,%B,?????)',
arg1:= F_DINT(stReg.Id),
arg2:= F_STRING(stReg.Name),
arg3:= F_INT(stReg.Age),
arg4:= F_LREAL(stReg.Height),
arg5:= F_BYTE(stReg.Glasses),

arg6:= ????? (stReg.dt_DateAnd_Time), // date and time
sOut=> sCommandAddReg);

.- My question is I would like to add the date and time but I don't know how I have to add inside VALUES for the date and time format?
.- And my other question is that I don't have a Helper function like F_DT, for the FB_FormatString the input T_Arg, how could I put it?

Thanks you, 😀 

Reply
1 Reply
1 Reply
runtimevictor
(@runtimevictor)
Joined: 2 years ago

Estimable Member
Posts: 156

Hello,

I answer to myself...

A possible solution would be to pass dateandtime to string format:

TYPE ST_Register :
STRUCT
Id : DINT;
Name : STRING(30);
Age : INT;
Height : LREAL;
Glasses : STRING(6);

dt_DateAnd_Time : DT;

END_STRUCT
END_TYPE

VAR

stReg : ST_Register;

fbCommand : FB_FormatString;

sCommandAddReg : STRING(255);

sDateAndTime : T_MaxString;
END_VAR

sDateAndTime := DELETE(DT_TO_STRING(dt_DateAnd_Time,3,1);
fbCommand(
sFormat:= 'INSERT INTO `people` VALUES(%D,$'%S$',%D,%1.2F,%B,$'%S$')',
arg1:= F_DINT(stReg.Id),
arg2:= F_STRING(stReg.Name),
arg3:= F_INT(stReg.Age),
arg4:= F_LREAL(stReg.Height),
arg5:= F_BYTE(stReg.Glasses),
arg6:= F_STRING(sDateAndTime), // date and time format string
sOut=> sCommandAddReg);

another more elegant and modern solution would be to use the Tc3_Database library instead of the Tc2_Database library,

with the Tc3_Database library we have improvements and among them we have the FB_PLCDBWriteEvt that has 3 methods,

one of them is WriteStruct, which will allow us to write a structure of data in the database,

I have also been testing FB_PLCDBCmdEvt and its Execute method with this I can write to the database but the fields in the database come out empty and I don't know why,

if someone can tell me how to solve it, I would be It's good that I mention it, so I also learn more things...

Reply
Share: