1

I'm a beginner in c++, this is part of my Task for an interview. Everything else works fine, except that I don't know how to convert String to Date, when it comes to pass the record to the database. Or just pass date (or DateTime) to the database. I found a lot of things in google, but couldn't figure it out how to do this in the most simple way. Also later I'll add HH:mm, because I'll have to calculate with hours/mins too. Here's the part of the code:

DateTime startTime = dateTimePicker1->Value;
String^ startTimeString = startTime.Year.ToString() +"-"+ startTime.Month.ToString() +"-"+ startTime.Day.ToString();

MySqlCommand^ cmd = gcnew MySqlCommand("insert into activity_db values('" + name + "','" + category + "','SELECT STR_TO_DATE(" + startTimeString + ",%Y-%m-%d)')", con);

drescherjm
  • 8,907
  • 5
  • 42
  • 60
zolee1337
  • 13
  • 2

1 Answers1

0

use

MySqlCommand^ cmd = gcnew MySqlCommand("insert into activity_db values('" + name + "','" + category + "',STR_TO_DATE('" + startTimeString + "','%Y-%m-%d'))", con);

So you will get the correct mysql command

insert into activity_db values('name','category',STR_TO_DATE('2020-01-01','%Y-%m-%d'))

And you should urgenty takem alook at prepared statements

nbk
  • 20,484
  • 4
  • 19
  • 35
  • Thank you for your quick answer, it works. About prepared statements: I knew that something like this exists, but didnt know how to look for it. Well I'm still learning. – zolee1337 Mar 02 '20 at 00:58