iPhone:How to store apostrophe(') sign in sqlite database - iphone

In UITextView I write my text and on the button click this text store in database but when I use apostrophe(') sign in my text. My query shows me error near apostrophe sign then please tell me how to insert apostrophe sign in sqlit database.
thanx in advance...

You could try to double the apostrophe sign in your text (i.e. '' instead of ').

This may be of some help as well when you want to replace a single quote with two double quotes.
Method:
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
Example:
[someString stringByReplacingOccurrencesOfString:#"'" withString:#"''"]

You should double the apostrophe ' to be double ''

A string constant is formed by enclosing the string in single quotes
('). A single quote within the string can be encoded by putting two
single quotes in a row - as in Pascal. C-style escapes using the
backslash character are not supported because they are not standard
SQL.
http://www.sqlite.org/lang_expr.html

Related

In mongodb - Can the document content contain curly braces?

While inserting a document in mongodb, one of the variables in the object that I am trying to insert contains curly braces '{abc123}' as a part of value.
This is causing the inserts to fail. Any way around it?
The simplest way is to use both double and single quotes: "'abc123'". Double quotes will mark the JavaScript string and single quotes will be handled as literal values, as if it was escaped with backslash.

SQLite table name invalid characters

I want to give name "http://192.168.1.2/abc/xyz_values" to a table in SQLite.
I am creating the table dynamically. How can i achieve this, as SQLite is not allowing me to create a table with this name?
Any help will be appreciated.
use square brackets [http://192.168.1.2/abc/xyz_values] when creating your table
Choose a different name for your table. It is not possible.
Why does the name have to be like that in the first place? It sounds a little dodgy...
There are three methods to create identifiers with special characters: double quotes, backticks, and square brackets.
Double quotes are standard SQL, while backticks and square brackets are supported only for compatibility with MySQL and SQL Server, so it's recommended to use the former:
CREATE TABLE "http://192.168.1.2/abc/xyz_values"(...)
Double quotes and backticks that occur inside the identifier can be escaped by doubling them; this is not possible with the closing square bracket:
CREATE TABLE """"(...) -- table name is one double quote

ObjectiveC sqlite3 issue

I'm finding that I have an issue when updating/inserting into my table in my iPhone app because I have a TEXT column, and when that text includes a ' symbol, things get messed up. What is the best way to handle this?
Should I check before I use a string that has an apostrophe? Is there a quick way to add formatting that will add an escape character in front of each apostrophe?
Does this question even make sense? lol.
sqlite requires the ' symbol to be escape by two ''.
Look at this from the official sqlite FAQ:
(14) How do I use a string literal that contains an embedded single-quote (') character?
The SQL standard specifies that single-quotes in strings are escaped by putting two single quotes in a row. SQL works like the Pascal programming language in the regard. SQLite follows this standard. Example:
INSERT INTO xyz VALUES('5 O''clock');
hey forget all this stuff. If you want your db to contain ' . Just replace your string with %27 & when fetching it back convert it back . You will get what you want. Check below :
// while Inserting into db
str = [str stringByReplacingOccurrencesOfString:#"'" withString:#"%27"];
// while fetching it back
text = [text stringByReplacingOccurrencesOfString:#"%27" withString:#"'"];
Enjoy programming :) :)
There's three ways to solve this:
Do the formatting yourself. Don't do this. (Well, not unless this string is part of your code rather than user input. In that case, this approach is fine.)
Use sqlite3_mprintf("%Q") to have SQLite do this. (%q does quote replacement; %Q does quote replacement and inserts NULL for a null pointer.)
Use bindings in your statement that you fill in with sqlite3_bind_text. This is the best way to do this, since it doesn't require recompiling the statement for every string and doesn't open you to SQL Injection.
Using a binding would look like this:
sqlite3_prepare(db, "INSERT INTO Table(Column) VALUES(?);", -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [str cStringUsingEncoding:NSUTF8StringEncoding],
-1, SQLITE_TRANSIENT);
// stepping, etc
(Don't forget to do error checking.)
There is a function provided with SQLite that can escape characters as needed. Take a look at:
sqlite3_mprintf
http://www.sqlite.org/c3ref/mprintf.html

How to parse special characters in XML for iPad?

I am getting problem while parsing xml files that contains some special characters like single quote,double quote (', "")etc.I am using NSXMLParser's parser:foundCharacters:method to collect characters in my code.
<synctext type = "word" >They raced to the park Arthur pointed to a sign "Whats that say" he asked Zoo said DW Easy as pie</synctext>
When i parse and save the text from above tag of my xml file,the resultant string is appearing,in GDB, as
"\n\t\tThey raced to the park Arthur pointed to a sign \"Whats that say\" he asked Zoo said DW Easy as pie";
Observe there are 2 issues:
1)Unwanted characters at the beginning of the string.
2)The double quotes around Whats that say.
Can any one please help me how to get rid of these unwanted characters and how to read special characters properly.
NSString*string =[string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
The parser is apparently returning exactly what's in the string. That is, the XML was coded with the starting tag on one line, a newline, two tabs, and the start of the string. And quotes in the string are obviously there in the original (and it's not clear in at least this example why you'd want to delete them).
But if you want these characters gone then you need to post-process the string. You can use Rams' statement to eliminate the newline and tabs, and stringByReplacingOccurrencesOfString:WithString: to zap the quotes.
(Note that some XML parsers can be instructed to return strings like this with the leading/trailing stuff stripped, but I'm not sure about this one. The quotes will always be there, though.)

read double quotes and single quotes from Sqlite database

In my application I am trying to read some data from sqlite database into UITextView.
This text has double quotes as well as single quotes, for ex:
she said : "Katie's purse has been
lost"
when I do this, I get strange characters in place of double and single quotes. Please help me with a solution to scrub these characters off.
Thanks.
If you get one strange character (or set of characters) replacing " and another replacing ', you can just scan the string and replace. If you aren't, then:
That's very strange, and
You should post your code.