본문 바로가기
언어/C++

c++ sqlite ' 포함된 문자열 insert시 쿼리 변환

by 김어찐 2021. 4. 8.
728x90

sqlite ' 포함된 문자열(i'm fine) insert시 에러가 발생함으로 변환 필요

std::string StrConvert::ReplaceAll(std::string& str, const std::string& from, const std::string& to) 
{
    size_t start_pos = 0; //string처음부터 검사
    while ((start_pos = str.find(from, start_pos)) != std::string::npos)  //from을 찾을 수 없을 때까지
    {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); // 중복검사를 피하고 from.length() > to.length()인 경우를 위해서
    }
    return str;
}



std::cout << ReplaceAll(string("Number Of Beans"), std::string(" "), std::string("_")) << std::endl;
std::cout << ReplaceAll(string("ghghjghugtghty"), std::string("gh"), std::string("X")) << std::endl;
std::cout << ReplaceAll(string("ghghjghugtghty"), std::string("gh"), std::string("h")) << std::endl;

#결과
Number_Of_Beans
XXjXugtXty
hhjhugthty

참조

hashcode.co.kr/questions/239/%EC%8A%A4%ED%8A%B8%EB%A7%81%EC%97%90%EC%84%9C-%ED%8A%B9%EC%A0%95-%EB%8B%A8%EC%96%B4-%EA%B5%90%EC%B2%B4%ED%95%98%EA%B8%B0

728x90

'언어 > C++' 카테고리의 다른 글

sqlite3 테이블 확인  (0) 2021.04.12
C++ UTF-16 formatstring 하기(swprintf 함수 사용)  (0) 2021.03.12
C++ 시간 계산  (0) 2021.03.11