简体   繁体   English

目标C中的SQLite3查询

[英]Sqlite3 query in objective c

  -(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 here , At NSLog I am able to print "SELECT AccessCode FROM UserAccess" where as let say the access code is 1234 , which I want .The challenge is to execute the command so that I can retrieve 1234 from the Column AccessCode in the Table UserAccess in the datable UserAcess.sqlite My Database and table name are same oops . Noob在这里,在NSLog中,我能够打印“ SELECT AccessCode FROM UserAccess”,其中的访问代码是1234,这是我想要的。挑战是执行命令,以便我可以从表中的AccessCode列中检索1234。数据源UserAcess.sqlite中的UserAccess和数据库名称是相同的。 In the appdelegate I Have : /// 在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;

              }

Please help :( 请帮忙 :(

You need to make some sqlite3 calls to execute the select statement and retrieve the column values. 您需要进行一些sqlite3调用以执行select语句并检索列值。

The sqlite3 quick start has everything in it you need to know for this particular task. sqlite3快速入门提供了您需要了解的所有特定任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM