WEB开发网
开发学院手机开发iPhone 开发 iPhone 编程 如何创建数据库并从中读取数据 阅读

iPhone 编程 如何创建数据库并从中读取数据

 2010-03-25 21:24:00 来源:WEB开发网   
核心提示:stringByAppendingPathComponent:databaseName];// Create a FileManager object, we will use this to check the status// of the database and to copy it over if requi
stringByAppendingPathComponent:databaseName];

// Create a FileManager object, we will use this to check the status

// of the database and to copy it over if required

NSFileManager *fileManager = [NSFileManager defaultManager];

// Check if the database has already been created in the users filesystem

BOOL success = [fileManager fileExistsAtPath:databasePath];

// If the database already exists then return without doing anything

if(!success){

// If not then proceed to copy the database from the application to the users filesystem

// Get the path to the database in the application package

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

// Copy the database from the package to the users filesystem

[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}

// Query the database for all person records and construct the "person" array

sqlite3 *database;

// Init the animals Array

NSMutableArray *personArr = [[NSMutableArray alloc] init];

// Open the database from the users filessytem

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

// Setup the SQL Statement and compile it for faster access

const char *sqlStatement = "select * from person";

sqlite3_stmt *compiledStatement;

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

// Loop through the results and add them to the feeds array

while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

// Read the data from the result row

NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

NSString *aTitle = [NSString stringWithUTF8String:(char

上一页  1 2 3 4  下一页

Tags:iPhone 编程 如何

编辑录入:coldstar [复制链接] [打 印]
赞助商链接