本文介绍了如何在iphone中连接sqlite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iphone中的应用程序并且是iphone的新手。
请告诉我使用sqlite在iphone中打开/关闭/更新/删除数据库记录的代码?

I am developing an application in iphone and am new to iphone.Kindly let me know the code for open/close/update/delete database record in iphone using sqlite?

问候,
Vijaya

Regards,Vijaya

推荐答案

import



在.h文件中

import

In .h file

- (void)createEditableCopyOfDatabaseIfNeeded;
+ (sqlite3 *) getNewDBConnection;

appdelegate.m中添加以下代码 didFinishLaunching 方法:

[self createEditableCopyOfDatabaseIfNeeded];

- (void)createEditableCopyOfDatabaseIfNeeded {
    NSLog(@"Creating editable copy of database");

    // First, test for existence.

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Scores.sqlite"];

    success = [fileManager fileExistsAtPath:writableDBPath];

    if (success) return;

    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Scores.sqlite"];

    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message ‘%@’.", [error localizedDescription]);
    }
}

+ (sqlite3 *) getNewDBConnection {
    sqlite3 *newDBconnection;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Scores.sqlite"];

    // Open the database. The database was prepared outside the application.
    if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
        NSLog(@"Database Successfully Opened ");
    } else {
        NSLog(@"Error in opening database ");
    }

    return newDBconnection;
}

在你的视图控制器中调用插入方法等这里我称之为方法这给我带来了表中的数据

In your view controller call the method of insertion etc. here i called a method which brings me the data from table

sqlite3* db =[iMemory_SharpenerAppDelegate getNewDBConnection];
sqlite3_stmt *statement = nil;
const char *sql = "select * from HighScore";

if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK) {
    NSAssert1(0,@"error prepearing statement",sqlite3_errmsg(db));
} else {
    while (sqlite3_step(statement)==SQLITE_ROW) {
        //dt= [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
        dt = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement, 0)];
        //dt = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
        NSLog(@"%@score==",dt);
        [nmber addObject:dt];
    }

}

sqlite3_finalize(statement);
sqlite3_close(db);

这篇关于如何在iphone中连接sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:39
查看更多