目标c中的Sqlite3查询

时间:2010-04-16 06:31:08

标签: objective-c iphone iphone-sdk-3.0 sqlite

  -(IBAction)ButtonPressed:(id)sender
    { 
        const char *sql = "SELECT AccessCode FROM UserAccess";
        NSString *sqlns;
        sqlns = [NSString stringWithUTF8String:sql];
        if([Password.text isEqual:sqlns])
        {
            NSLog(@"Correct");
        }
            else {
                NSLog(@"Wrong");
            }

    NSLog(@"%@",sqlns);
    }

Noob在这里, 在NSLog,我能够打印“SELECT AccessCode FROM UserAccess”,其中我想要的是访问代码是1234.挑战是执行命令以便我可以检索 1234来自表User User中的列AccessCode中的可数据UserAcess.sqlite 我的数据库和表名是相同的oops。 在appdelegate我有: ///

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after app launch 
    [self createEditableCopyOfDatabaseIfNeeded];    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

///

 - (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:@"UserAccess.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:@"UserAccess.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:@"UserAccess.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;

              }

请帮助:(

1 个答案:

答案 0 :(得分:1)

您需要进行一些sqlite3调用来执行select语句并检索列值。

The sqlite3 quick start包含了您需要知道的针对此特定任务的所有内容。