【问题标题】:Sqlite3 query in objective c目标c中的Sqlite3查询
【发布时间】:2011-02-08 17:06:54
【问题描述】:
  -(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);
    }

这里是菜鸟, 在 NSLog 我可以打印“SELECT AccessCode FROM UserAccess”,假设访问代码是 1234 ,这是我想要的。挑战是执行命令以便我可以检索 1234 来自数据 UserAcess.sqlite 中表 UserAccess 中的列 AccessCode 我的数据库和表名是相同的 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;

              }

请帮忙:(

【问题讨论】:

  • 您应该使用 Objective-C 字符串常量 NSString *sqlns = @"SELECT AccessCode FROM UserAccess"; 而不是从您正在使用的 C 字符串生成 Objective-C 字符串的复杂方法。
  • 我想要 NSLog(@"%@",sqlns); 中 'AccessCode' 列的值我应该得到 1234 而不是:SELECT AccessCode FROM UserAccess

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


【解决方案1】:

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

The sqlite3 quick start 包含您在执行此特定任务时需要了解的所有内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多