B4J Question String toSQL DateTime formatDim Unit As String Dim Weight, Height, Target, BMI, BMIa, BMIb As Int Dim Hft, Hin, Wst, Wlbs, Tst, Tlb As Int Unit = s

Colin Evans

Active Member
Licensed User
Longtime User
Hi, is there any way to convert a date and time strings to a format acceptable to an SQL database holding it in DateTime type / format, i.e. I have two input fields Date and Time which the user can fill in, i.e. Date: 2020-07-27 and Time: 18:33 but I wish to save them to a MySql database which holds the date and time in that field format (datetime type not varchar() type)
usually the data held in the database would resemble 2020-07-27 18:33:00
 

TILogistic

Expert
Licensed User
Longtime User
an alternative is to send a date as string and convert it with mysql
When saving in the database

MySQL.

.

PHP.

 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi once again, I've managed to get the Update and delete functions working in my PHP API along with showing all people in the database with the help of Aeric, DOnManfred, oparra etc the one last bit is the entry of a new person via my app and not the third party app.

I'm basically sending the following

<code>
"Action":"InsertPerson", "_sfm_form_submision_time_":mSQLDate, "Name":NameTF.Text,"Phone"honeTF.Text,"Party"artyTF.Text, "Email":EmailTF.Text
</code>

to the PHP code

<code>
Case "InsertPerson":
$pdate=stripslashes(mysqli_real_escape_string($con,$jsone["_sfm_form_submision_time_"]));
$pname=stripslashes(mysqli_real_escape_string($con,$jsone["Name"]));
$pphone=stripslashes(mysqli_real_escape_string($con,$jsone["Phone"]));
$pparty=stripslashes(mysqli_real_escape_string($con,$jsone["Party"]));
$pemail=stripslashes(mysqli_real_escape_string($con,$jsone["Email"]));
$stmt = $con->prepare("INSERT INTO login (_sfm_form_submision_time_, Name, Phone, Party, Email) VALUES (?, ?, ?, ?, ?)");
$rc=$stmt->bind_param("sss", $pdate, $pname, $pphone, $pparty, $pemail);
$rc=$stmt->execute();
$iid=$stmt->insert_id;
if ($iid == 0)
{
exit (json_encode(array(array('InsertPerson' => 'failed'))));
}
else
{
exit (json_encode(array(array('InsertPerson' => 'ok', 'pid' => $iid))));
}

break;
</code>

the field _sfm_form_submision_time_ contains the date, and I thought it was this that was causing the problem but when I remove it I still get the error InsertPerson Failed

I believe it's something to do with the 'bind_param("sss"' because I changed this to 'bind_param("sssss"' and it now works

From what I can determine I assume 's' equals string and 'i' would be integer are there other codes I should be aware of?

Thanks again for your quick responses
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim JOSQLDateTime As JavaObject
    Dim JSQL As SQL
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    DateTime.DateFormat = "yyyy-MM-dd"
    Dim DT As Long = DateTime.DateTimeParse("2020-07-27","18:33:00")
    Log(DT)
    JOSQLDateTime.InitializeNewInstance("java.sql.Timestamp",Array (DT))
    Log (JOSQLDateTime.RunMethod("toString",Null))
    JSQL.ExecNonQuery2("INSERT INTO mydatetable VALUES(?)",Array As Object(JOSQLDateTime))
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If the date and time is based on insert, what I normally do is set the column as timestamp and default value to CURRENT_TIMESTAMP. Another method is using MySQL now() function to insert into datetime field.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…