c++ mysql 질문드립니다.

-@Naver의 이미지

c++에서 데이터베이스 연동 후 원하는 값을 select하려고 하는데

연동은 되지만 쿼리문실행이 안되는지 에러가 납니다..

에러내용 : Invalid utf8 character string :

찾아보니 utf8로 캐릭터셋이 되어있지 않은거라고 하는데....db 테이블이랑 다른것들 다 확인해봐도

utf8로 셋이 되어있습니다...무엇이 문제인가요...

혹시몰라서 코드도 올립니다. 현재 테스트중이라 코드가 지저분한점 이해부탁드립니다

std::wstring ad = macArr[0];

std::string newad;

newad.assign(ad.begin(), ad.end());

char query[255];

sprintf_s(query, "select * from USER where address='%s'", newad);

query_stat = mysql_query(connection, query);

if (query_stat != 0)

std::wcout << _T("Mysql query error : ") << mysql_error(&conn) << std::endl;

else {

std::cout << "데이터 베이스 연결 성공" << std::endl;

sql_result = mysql_store_result(connection);

while ((sql_row = mysql_fetch_row(sql_result)) != NULL)

std::wcout << sql_row[0] << " " << sql_row[1] << " " << sql_row[2] << std::endl;

mysql_free_result(sql_result);

}

익명 사용자의 이미지

sprintf_s 사용법이 저게 맞나요? 매뉴얼 확인 바랍니다.

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l?view=vs-2019

보아하니 MS Visual C++를 사용중이신 것 같은데 여기서 utf8 string을 다루려면 막 WideCharToMultiByte 같은 거 쓰셔야 될 겁니다 아마. 최근에 해 본 적이 없어서 기억이 잘 안나네요.

https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte

-@Naver의 이미지

찾아보니까 sprintf_s에 sizeof가 들어가도 되고 안들어가도 된다고해서 저렇게 코딩하였습니다!

익명 사용자의 이미지

어디서 그러나요?

익명 사용자의 이미지

아하.

Quote:
In C++, use of these functions is simplified by template overloads; the overloads can infer buffer length automatically, which eliminates the need to specify a size argument, and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.

자동으로 buffer length를 채워넣는 template overloading이 되어 있군요.

-@Naver의 이미지

sprintf_s에서 마지막에는 char * 형을 넣어줘야 실행이 잘 됩니다!

익명 사용자의 이미지

이런, 제가 엉뚱한 데를 보고 있었군요. -_-;