我正在使用SQLite开发iPhone应用程序。我决定使用fmdb。使用fmdb,我如何执行以下操作:-插入,更新,选择,删除?
由于我无法使用FMDatabase开发应用程序。我下载了FMDatabase文件,其中包含以下文件,即=>
但是文件fmdb.m
也包含main
功能。因此它与我的应用程序的MAIN函数冲突。
fmdb.m是包含FMDB示例代码的文件。您不应将此文件包含在Xcode项目中。但是您应该对其进行检查,以了解如何使用FMDB。它包含很多好评的示例。
要在代码中使用fmdb
,只需将您列出的文件添加到项目中即可,但main.m
文件除外。这是一个使用它的示例,当应用程序启动完成时,将创建一个SQLite数据库。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
FMDatabase* db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
if (![db open]) {
NSLog(@"Could not open db.");
}
[db executeUpdate:@"create table test (a text, b text, c integer, d double, e double)"];
NSLog(@"%@",[db databasePath]);
[db beginTransaction];
int i = 0;
while (i++ < 20) {
[db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
@"hi'", // look! I put in a ', and I'm not escaping it!
[NSString stringWithFormat:@"number %d", i],
[NSNumber numberWithInt:i],
[NSDate date],
[NSNumber numberWithFloat:2.2f]];
}
[db commit];
[db close];
}
注意,我只是将fmdb main.m
文件中的一些代码粘贴到applicationDidFinishLaunching
中。