iPhone 编程 如何创建数据库并从中读取数据
2010-03-25 21:24:00 来源:WEB开发网我们也需要将PersonDatabas 数据库导入Resources目录。请右键点击“Resources”目录,选择“Add > Existing Files…”,在本地目录中选择“/Users/Smitty/Documents/SQLiteDemo/”, 双击PersonDatabase.sql文件,点击弹出窗口中的“Add”按钮将其导入当前项目。
5. 开始编程
首先创建一个Person类。右键点击“Classes”目录,选择“Add > New File…”,在弹出窗口中选择“iPhone OS > Cocoa Touch Class > Objective-C class > NSObject” 并点击“Next”按钮。在下一窗口中,设置File Name为“Person.m”。点击“Finish”按钮完成创建。
修改Classes/Person.h为:
@interface Person : NSObject {
NSString *name;
NSString *title;
NSString *iconURL;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *iconURL;
-(id)initWithName:(NSString *)n title:(NSString *)t url:(NSString *)u;
@end
修改Classes/Person.m为:
#import "Person.h"
@implementation Person
@synthesize name, title, iconURL;
-(id)initWithName:(NSString *)n title:(NSString *)t url:(NSString *)u {
self.name=n;
self.title=t;
self.iconURL=u;
return self;
}
@end
修改Other Sources/main.m为:
#import
#import "Person.h"
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//setup database name.
NSString *databaseName = @"PersonDatabase.sql";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir
更多精彩
赞助商链接