I use a commercial data base that is delivered with fields such as:
Super Crew 5 1/2' Bed 4WD // where single quote (') symbolizes feet
When this field is required to be used in an SQL query, it expands out similar to this excerpt:
...WHERE TitleIndex.Title='Super Crew 5 1/2' Bed 4WD'
Resulting in an error message query expression missing operator. The portion of the intended string literal >>> Bed 4WD'<<< is not understood because of the single quote preceding it.
Note: If I manually edit the database field to replace >'< with >ft.<:
'Super Crew 5 1/2ft. Bed 4WD'
the query works. But it will not be practical to edit all fields containing single quotes each time a new database is delivered.
I have looked at parametrization, but that will not work as the field would still require the single quote embedded in the field be escaped with another single quote:
SET @Value = 'Super Crew 5 1/2'' Bed 4WD'
Again, this requires editing, which is not an option for us.
Since I cannot escape a single quote embedded in a provided field, is there a way I can package it into a query string so that it will not be rejected as an illegal query?
EDIT (to show function problem exists in)
Note, in the code below, char *title
is obtained in a previous call, it contains the column data just as it is stored in the database. >>>Super Crew 5 1/2' Bed 4WD<<<
int AMSDB_API ams_get_tid_from_title(char *title, int *titleId)
{
int index, status, iStatus, tid;
char query[500];
sprintf (query, "SELECT TitleIndex.TitleId FROM TitleIndex WHERE TitleIndex.Title=\'%s\'", title);
hstmt = DBActivateSQL(hAmsIndex, query);
//numberRecords = DBNumberOfRecords(hstmt);
status = DBBindColInt (hstmt, 1, &tid, &iStatus);
DBFetchNext(hstmt);
DBDeactivateSQL(hstmt);
*titleId = tid;
return status;
}