나의질문답

mysql datetime형식의 칼럼에 insert를 하려고 합니다.

예쁜꽃이피었으면 2015. 7. 28. 09:58

http://www.devpia.com/Maeul/Contents/Detail.aspx?BoardID=50&MAEULNo=20&no=956122&ref=956122


mysql DB에 MFC에서 CString형식으로 받은 값을 넣어주려고 합니다.

DB의 칼럼형식은 datetime입니다.

editbox에서 입력받은 형식은 CString이고 입력은 20150720102030 이렇게 하려고 합니다.

editbox의 날짜와시간 값을 가지고 있는 변수는 edit_date입니다.

 

소스는 이렇습니다.


COleDateTime dateTime;
 
CString strDate;
CString strYear, strMonth, strDay, strHour, strMinute, strSec;
strDate = edit_date;
 
for (int i = 0; i < 6; i++)
{
switch (i)
{
case 0:
strYear = strDate.Mid(0, 4);
AfxMessageBox(strYear);
break;
 
case 1:
strMonth = strDate.Mid(4, 2);
AfxMessageBox(strMonth);
break;
 
case 2:
strDay = strDate.Mid(6, 2);
AfxMessageBox(strDay);
break;
 
case 3:
strHour = strDate.Mid(8, 2);
AfxMessageBox(strHour);
/*strDate = strDate.Mid(GUBUN);
AfxMessageBox(strDate);*/
break;
case 4:
strMinute = strDate.Mid(10, 2);
AfxMessageBox(strMinute);
break;
 
case 5:
strSec = strDate.Mid(12, 2);
AfxMessageBox(strSec);
break;
}
}
edit_date.Format(_T("%s-%s-%s %s:%s:%s"), strYear, strMonth, strDay, strHour, strMinute, strSec);
AfxMessageBox(edit_date); // 2015-07-20 10:20:30 찍힘
 
CString str1 = edit_date;
COleDateTime t;
t.ParseDateTime(str1);
CString str2 = t.Format(_T("%Y-%m-%d %H:%M:%S"));
ASSERT(str1.Compare(str2) == 0);
// count가 0 이므로 수식이 참이다. 따라서 오류상자가 나오지 않는다. 
 
AfxMessageBox(str2); //2015-07-20 10:20:30 찍힘
 
 
//DB에 넣으려고 하는 부분입니다.
pszValue = (TCHAR*)Cmd.GetValue(3);
wsprintf(pszValue, _T("%s"), str2);
Cmd.SetLength(3, lstrlen(pszValue) * 2);
Cmd.SetStatus(3, DBSTATUS_S_OK);
 
 
hr = Cmd.Insert();
 
 
if (FAILED(hr))
{
AfxMessageBox(_T("ERROR: Failed to Insert new record!"));
}//실패하면서 Failed to Insert new record!이문구가 뜹니다.
 
Cmd.Close();
Cmd.ReleaseCommand();
 
 

 

왜 안들어가 가는 건가요?

 

mysql에 직접 데이터를 넣어보니

문자열('20150720102030')과 숫자(20150720102030) 형식모두 자동포맷이 되서 들어가고

포맷을해서 문자열('2015-07-20 10:20:30')로 넣어도 Db에 들어가는 것을 확인 했습니다.

 

답변부탁드리겠습니다..

 

 

+) 

프로젝트명Set.h에

DBTIMESTAMP m_date; 이렇게 나와있습니다. m_date는 DB에서 날짜시간의 칼럼명입니다.



'20150720102030' 형식은 ParseDateTime 메소드로 파싱이 안됩니다.

"2015-07-21 13:10:20" 와 같이 알려진 형식일 경우에만 파싱이 됩니다.

 

나머지는 직접 파싱하셔야되요.


반응형