Re: SQLite and Unicode
Re: SQLite and Unicode
- Subject: Re: SQLite and Unicode
- From: Тимофей Даньшин <email@hidden>
- Date: Tue, 7 Apr 2009 21:49:36 +0400
How do you define "string" (i.e. how is its value set)? There can be
an issue with UTF8 constant strings. Have you called -
canBeConvertedToEncoding: to make sure "string" is utf-8 compatible?
To be on the safe side, i make a local copy of the method's input
string. And the -canBeConvertedToEncoding: returns yes...
I am really at a loss. I open the db with sqlite3_open([path
UTF8String], &db), which as i am lead to understand opens it in the
UTF-8 compatible mode. May there be anything wrong with the way i
create prepared statements? Or anything else?
Here is the method that writes to the db:
- (int) writeToDB:(NSString *)string {
if (!dbIsOpen) {
[self openDatabase];
}
if (sentence_insert_statement == nil) {
static char *sql = "INSERT INTO sentences (sentence, slength)
VALUES(?, ?)";
if (sqlite3_prepare_v2(db, sql, -1,
&sentence_insert_statement, NULL) != SQLITE_OK) {
NSLog(@"Error: failed to prepare statement with message
'%s'.", sqlite3_errmsg(db));
}
}
NSString *string2 = [string copy];
NSLog(@"The string can be converted to utf: %i", [string2
canBeConvertedToEncoding:NSUTF8StringEncoding]);
const char *cString = [string2 UTF8String];
sqlite3_bind_text(sentence_insert_statement, 1, cString, -1,
SQLITE_TRANSIENT);
sqlite3_bind_int(sentence_insert_statement, 2, string.length);
int success = sqlite3_step(sentence_insert_statement);
[string2 release];
sqlite3_reset(sentence_insert_statement);
if (success == SQLITE_DONE) {
return sqlite3_last_insert_rowid(db);
}
NSLog(@"Success is %d, sqlite_ok is %d", success, SQLITE_OK);
NSLog(@"Error: failed to insert sentence with message '%s'.",
sqlite3_errmsg(db));
return 0;
}
I read about SQLite in Python, that one should not pass null-
terminated strings to insert statements, or there will be problems
with non-latin chars. Can that be the issue here? How can i chop off
the NULL-char off the cString just to give it a try?
On Apr 7, 2009, at 9:12 PM, Keary Suska wrote:
On Apr 7, 2009, at 10:39 AM, Тимофей Даньшин wrote:
On Apr 7, 2009, at 8:14 PM, Keary Suska wrote:
Are you properly encoding your C strings with -
cStringUsingEncoding:?
Yes, I think so.
At present, I am using the -UTF8String method, but I also tried the
-cStringUsingEncoding: to no avail.
Here is the line that binds the string to the prepared statement:
sqlite3_bind_text(sentence_insert_statement, 1, [string
UTF8String], -1, SQLITE_TRANSIENT);
and I translate it back into NSString by [string
stringWithUTF8String], although I also tried the -
stringWithCString: encoding: method. The characters _are_ mangled.
And the funny thing is that i am able to add non-latin strings to
it by reading sql files from SQLite in Terminal...
How do you define "string" (i.e. how is its value set)? There can be
an issue with UTF8 constant strings. Have you called -
canBeConvertedToEncoding: to make sure "string" is utf-8 compatible?
Best,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden