我的SQLite數據庫,並與查詢導出一些結果保存在一個文件中的NSMutableArray所有內容的iOS
sqlite3 *MyDB;
const char *dbpath = [StringPathDB UTF8String]; // Convert NSString to UTF-8
NSString *querySQL = @"select * from chat order by id desc limit 20";
if (sqlite3_open(dbpath, &MyDB) == SQLITE_OK)
{
const char *sql = [querySQL UTF8String];
sqlite3_stmt *searchStatement;
if (sqlite3_prepare_v2(MyDB, sql, -1, &searchStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(searchStatement) == SQLITE_ROW)
{
NSString *nickname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)];
NSString *time = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 1)];
NSString *text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];
然後我把所有的NSMutableArray
NSMutableArray *list = [[NSMutableArray alloc] init];
[list addObject:nickname];
[list addObject:time];
[list addObject:text];
當我試圖挽救人的文件與此代碼
for(int i = 0; i < [list count]; i++) {
NSLog(@"%@",list);
[list writeToFile:filePATH atomically:YES];
break;
}
它在NSLog中正確郵戳,但在文件只保存最後一個記錄不是所有陣列年。 請幫幫我!
什麼是'filePATH'?你是如何創建它的? – Undo
是否所有在'list'由'NSLog()'驗證? – zaph
您需要實際積累所有記錄才能將它們全部寫入。您的列表將只包含一條記錄。 (如果你寫了多個記錄,就沒有什麼可以將它們分開,你需要重新考慮你想要做什麼。) –