[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: A problem with quoteString in libSQLClient, and a question
From: |
Richard Frith-Macdonald |
Subject: |
Re: A problem with quoteString in libSQLClient, and a question |
Date: |
Wed, 12 Dec 2012 16:35:54 +0000 |
On 12 Dec 2012, at 15:54, Sebastian Reitenbach wrote:
> Hi,
>
> for MPDCon, I use libSQLClient to save some data in a local SQLite database.
> So far, everything works not too bad, I only have a problem with quoting
> strings:
> libSQLClient provides a quoteString: method, which I thought I can use
> to quote ' characters in the string.
> Now, when I don't quote the string, or when I use the libSQLClient method
> quoteString, then MPDCon hangs, when it comes to a, in my case a file name,
> containing a ' character.
>
> When I use NSStrings stringByReplacingOccurrencesOfString: withString:
> as below, then it just quotes it right, and the query executes fine.
>
> Code example is shown below:
>
>
> - (void) setRating: (NSInteger) rating forFile: (NSString *) fileName
> {
> NSString *quotedFileName, *query;
>
> // the following doesn't seem to work, but the line after it does the trick!
> //quotedFileName = [MPDConDB quoteString:fileName];
> quotedFileName = [fileName stringByReplacingOccurrencesOfString:@"'"
> withString:@"''"];
> query = [NSString stringWithFormat:@"INSERT OR REPLACE INTO \
> SongRatings(fileName, rating) values('%@', %i)",
> quotedFileName, rating];
> [MPDConDB execute: query, nil];
> }
The problem is that your code is attempting to quote the string twice ... once
by calling the -quoteString: method, and then again later by using
stringWithFormat to quote it.
Try the following:
[MPDConDB execute: @"INSERT OR REPLACE INTO SongRatings(fileName, rating)
VALUES (",
[MPDConDB quote: fileName], @", ", [MPDConDB quoteInteger: rating], @")",
nil];
> the question I have to libSQLClient is, whether there is a method I can query
> the
> SQLClient object, which database backends it supports?
> For example, on different systems, there may be backends for sqlite available,
> on others not, depending on how libSQLClient was compiled.
> So far I haven't seen anything, but maybe overlooked it.
Not directly... to use a database you need to know what it's type, location,
and login credentials are (since you need to supply those parameters to create
a client object) ... and when you try to access the database, if there is no
backend to support it, you'll get an exception (at the point when you try to
connect to it) so you can use that to detect whether the database backend is
available.
Alternatively, you could check for the existence of the backend bundle in the
normal library locations I suppose, but trapping the exception makes more sense
to me.
If building from source, at configure time, the configure script will tell you
which backends are being built.