有人能解释一下使用 FMDB 在 iPhone 上插入大量数据的最佳方法是什么吗?我看到了使用 beginTransaction 命令之类的东西。老实说,我不确定 this 或 setShouldCacheStatements 是做什么的。到目前为止,我遵循了我的同事所做的代码,这就是它的样子:
BOOL oldshouldcachestatements = _db.shouldCacheStatements;
[_db setShouldCacheStatements:YES];
[_db beginTransaction];
NSString *insertQuery = [[NSString alloc] initWithFormat:@"INSERT INTO %@ values(null, ?, ?, ?, ?, ?, ?, ?);", tableName];
[tableName release];
BOOL success;
bPtr += 2;
int *iPtr = (int *)bPtr;
int numRecords = *iPtr++;
for (NSInteger record = 0; record < numRecords; record++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Some business logic to read the binary stream
NSNumber *freq = aFreq > 0 ? [NSNumber numberWithDouble:100 * aFreq / 32768]: [NSNumber numberWithDouble:-1.0];
// these fields were calculated in the business logic section
success = [_db executeUpdate:insertQuery,
cID,
[NSNumber numberWithInt:position],
[NSString stringWithFormat:@"%@%@", [self stringForTypeA:typeA], [self stringForTypeB:typeB]], // methods are switch statements that look up the decimal number and return a string
[NSString stringWithFormat:@"r%i", rID],
[self stringForOriginal:original],
[self stringForModified:modified],
freq];
[pool drain];
}
[outerPool drain];
[_db commit];
[_db setShouldCacheStatements:oldshouldcachestatements];
这是我能做到的最快速度吗?写是sqlite的限制吗?我看到了这个:http ://www.sqlite.org/faq.html#q19并且不确定这个实现是否是最好的 fmdb,或者我是否可以做任何其他事情。其他一些同事提到了一些关于批量插入和优化的事情,但我不确定这意味着什么,因为这是我第一次遇到 sqlite。有什么想法或方向我可以研究吗?谢谢!