Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-21-2010, 03:10 PM   #1 (permalink)
zaz
Registered Member
 
Join Date: May 2010
Location: USA
Posts: 5
zaz is on a distinguished road
Default Read chinese characters from SQLite...

Hello all,

I am working on an app that has to read Chinese words from an SQLite database. Initially I used a pretty basic way of retrieving the data from the db:

Code:
sqlite3 *Database(void)
{
	//NSString *cards;
	NSString *file = [[NSBundle mainBundle] pathForResource:@"chinese" ofType:@"db"];
	if(sqlite3_open([file UTF8String], &database) == SQLITE_OK)
	{
		if(sqlite3_exec(database, "select chinTraditional from words where chap = '1'", dbCallBack, cards, NULL))
		{
			NSLog(@"Success");
		}
		else
			NSLog(@"Q Fail");
	
	return database;
}
where dbCallBack is :

Code:
int dbCallBack(void *na, int argc, char **argv, char **colName)
{
      for(int i = 0; i < argc; i++)
      {
		NSLog([NSString stringWithFormat:@"Chinese = %C\n", argv[i] ? argv[i] : "NULL"]);
	}
	return 0;
}
I wanted to display the current Chinese word with NSLog, but I get wrong values.

I thought that the problem might be in argv being an array of chars, which are of smaller size than the one needed for unicode. I tried other using sqlite3_prepare_v2 instead and statements instead of this method but I was not sure how to proceed and whether this could get me out of the problem.

Could anyone lend me a hand, or give me a hint in how to proceed with the task of reading the Chinese characters from the database and assigning them to a variable that I can use elsewhere in the code?

Thanks a lot!

I have been looking for solutions for the past 3-4 hours and I could find anything that fixes my problem.
zaz is offline   Reply With Quote
Old 05-21-2010, 04:24 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

%C is definitely not correct in your NSLog statement. That would be for a unicode character; you are dealing with strings, not characters, so you want %S here. Although I'm not sure you can accomplish what you want by using that callback function required by sqlite3_exec.

I would try replacing your sqlite3_exec call with separate calls to sqlite3_prepare_v2 and sqlite3_step. And then look at sqlite3_column_text16 to get your unicode strings after each step call.

SQLite definitely supports what you are trying to do... I'm not using it directly; I'm going through Core Data, but I am having no problems storing and retrieving Kanji.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 05-24-2010, 10:00 AM   #3 (permalink)
zaz
Registered Member
 
Join Date: May 2010
Location: USA
Posts: 5
zaz is on a distinguished road
Default

Thanks dljeffery, I wrote this line while typing my post, and I didn't pay too much attention.

I did as you said. It is certainly worth it to know what is behind the SQLite wrapper functions.

It turned out that my problem was in the database that I was using. I converted it to SQLite from MySQL using a database manager (SQLite2009 pro) which is available for free. I couldn't get this software to work for unicode characters, as it just stores what the unicode characters from the original database would look in ASCII (e``^&* stuff) in the SQLite one. I tried converting these values to UTF-8 in xcode but I couldn't.

Do you usually convert ASCII to UTF8 by using stringWithUTF8String?
zaz is offline   Reply With Quote
Old 07-07-2010, 04:40 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 1
chopgood is on a distinguished road
Default

the following code worked for me with correct character encoding and display,

Code:
// Loop columns of returned record
for (int i=0; i < nColumns; i++) {
    NSString *value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, i)];  
    NSLog(@"%@",value);              
}
chopgood is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 310
10 members and 300 guests
ajay123123, ashaman64, baja_yu, ChrisYates, guusleijsten, HemiMG, newDev, pkIDSF, Sami Gh, Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:52 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0