'select path from music1 where path='+''''+'E:\软件作品\媒体播放\music\I'+''''+'m a pig.mp3'
这个sql语句是动态获得的,path是不确定的,我怎么样知道它获得的查询字段是正确的呢? 比如这里的I'm a pig中出现了‘,产生了错误!
不考虑Delphi,单纯SQL串,是这样:select path from music1 where path='E:\软件作品\媒体播放\music\I''m a pig.mp3'
用AnsiQuotedStr函数,给你安全地加引号: function AnsiQuotedStr(const S: string; Quote: Char): string; S是原串,Quote用''''吧,返回的串是已经加了引号的,包括两头。DescriptionUse AnsiQuotedStr to convert a string (S) to a quoted string, using the provided Quote character. A Quote character is inserted at the beginning and end of S, and each Quote character in the string is doubled.Note: This function supports multi-byte character sets (MBCS).
'select path from music1 where path= ''' + E + ''' + ''' + : + ''' + \ + ''' + 软件作品 + ''' + \ + ''' + 媒体播放 + ''' + \ + ''' + music + \ + ''' + I + ''' + ''' + 'm a pig.mp3 + ''';
query.close; query.sql.clear; query.sql.add('select path from music1 where path=:path'); query.parambyname('path').asstring:='E:\软件作品\媒体播放\music\I'm a pig.mp3'; query.open;
比如这里的I'm a pig中出现了‘,产生了错误!
function AnsiQuotedStr(const S: string; Quote: Char): string;
S是原串,Quote用''''吧,返回的串是已经加了引号的,包括两头。DescriptionUse AnsiQuotedStr to convert a string (S) to a quoted string, using the provided Quote character. A Quote character is inserted at the beginning and end of S, and each Quote character in the string is doubled.Note: This function supports multi-byte character sets (MBCS).
query.sql.clear;
query.sql.add('select path from music1 where path=:path');
query.parambyname('path').asstring:='E:\软件作品\媒体播放\music\I'm a pig.mp3';
query.open;
不行啊!还是出错啊!
加的都是双引号,出错啊!