從你的「這是我自己的話,而不是可在一個已經被地方做了一些其他的數據庫」的評論,是不是正確的假設你的字對字的替換是一種有效的方法你想達到什麼目的? (?沒有單獨的語言或先例,什麼是正確的)
如果是這樣,我想你會做這樣的事情:
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