从你的“这是我自己的话,而不是可在一个已经被地方做了一些其他的数据库”的评论,是不是正确的假设你的字对字的替换是一种有效的方法你想达到什么目的? (?没有单独的语言或先例,什么是正确的)
如果是这样,我想你会做这样的事情:
1)从输入字段走串, 2)适用componentsSeparatedByString:给它把它分解成一个数组,然后, 3)取数组中的每一项,搜索你的sqlite数据库中的数据,返回相应的单词,并将其附加到一个字符串(和一个空格)中...... 4)当你'用数组完成,输出结果字符串到'输出'框。你可以从一个大的excel文件开始,然后用一些sqlite管理器(我使用firefox插件),将它导入到一个.sqlite文件中,然后将它添加到你的项目中。你有没有想过如何处理'找不到'的单词?
这是未经测试的代码,只是为了给你的东西入手:
-(void) viewWillAppear:(BOOL)animated{
[self createEditableCopyOfDatabaseIfNeeded];
}
-(sqlite3 *) getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened");
} else {
NSLog(@"Error in opening database");
}
return newDBconnection;
}
-(void)translate{
//take input and break into an array
NSString *clearText = [[NSString alloc] init];
clearText=inputBox.text;
NSArray *words = [[NSArray alloc] init];
words= [clearText componentsSeparatedByString:@" "];
numOfWords=words.count;
NSString *[email protected]"";
//open database
sqlite3 *db = [self getNewDBConnection];
//loop through array
for(i=0;i<numOfWords;i++){
sqlite3_stmt *resultStatement = nil;
NSString *res = [NSString stringWithFormat:@"select * from dictionary where plain='%@'",[[words objectAtIndex:i] stringByTrimmingCharactersInSet:whitespaceCharacterSet]];
if((sqlite3_prepare_v2(db, [res UTF8String], -1, &resultStatement, nil))!=SQLITE_OK){
NSLog(@"Error getting result, maybe word not found\n");
NSLog(@"error: %s", sqlite3_errmsg(db));
}
else{
if(sqlite3_step(resultStatement)==SQLITE_ROW){
//in the line below, 1 is the column number of the replacement word
NSString *add = [[NSString alloc] initWithUTF8String: (char*)sqlite3_column_text(resultStatement,1)]
newText=[newText stringByAppendingString:add];
[add release];
}
}
sqlite3_finalize(resultStatement);
}
//output result
outputBox.text=newText;
sqlite3_close(db);
}
-(void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
//NSLog(@"Creating editable copy of database");
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
好的,好吧,我一直试图实现这个代码一个小时左右,并在事情的日志方面,我不断得到“错误获得结果,也许没有找到字”的消息,我只能认为我的问题与我所做的实际数据库文件一样,我可以在这里排除故障的是什么? – Sam 2011-05-31 12:40:00
是否将'dictionary'更改为您在sqlite文件中使用的实际表名?和'普通'到您使用的列的正确名称? – 2011-05-31 13:38:12
是的,它仍然返回错误信息... – Sam 2011-05-31 14:15:57