错误没有这样的表在iphone sqlite选择查询?

时间:2014-01-27 10:05:25

标签: ios iphone sqlite

我正在选择iPhone应用程序。在第一个选项卡中,我创建了sqlite表并将值插入表中,这些都成功完成。在第二个选项卡中,我可以从表中获取值但在第三个类中执行选择查询,我得到没有这样的表异常。 即使我从模拟器中删除了应用程序并清理了项目,但我遇到了同样的问题。 请给我解决方案。

用于创建和插入的编码:

-(void)InsertLoginDetails:(NSString *)user fun:(NSString *)pwd{

sqlite3_stmt    *statement;
const char *dbpath = [_databasePath UTF8String];

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{

    NSString *insertSQL = [NSString stringWithFormat:
                           @"INSERT INTO Login ( USERNAME, PASSWORD) VALUES (\"%@\", \"%@\")",
                           user, pwd];

    const char *insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(_contactDB, insert_stmt,
                       -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        NSLog(@"Inserted");

    } else {
        NSLog(@"Failed to insert");
    }
    sqlite3_finalize(statement);
    sqlite3_close(_contactDB);
}

}

-(void)createLoginTable{
NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = dirPaths[0];

// Build the path to the database file
_databasePath = [[NSString alloc]
                 initWithString: [docsDir stringByAppendingPathComponent:
                                  @"login.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt =
        "CREATE TABLE IF NOT EXISTS Login (ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT, PASSWORD TEXT)";

        if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {

            NSLog(@"Failed to create table");

        }
        sqlite3_close(_contactDB);
        NSLog(@"Table created");
    } else {
        NSLog(@"Failed to open/create database");

    }
}

}

获取值的代码:

-(void)checkLoginSession{
///////////////GET DB DATAS///////////////////////

const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt    *statement;

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
    NSString *querySQL = @"SELECT USERNAME, PASSWORD FROM Login";


    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(_contactDB,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            strDBUsername = [[NSString alloc]
                             initWithUTF8String:
                             (const char *) sqlite3_column_text(
                                                                statement, 0)];
            strDBPwd = [[NSString alloc]
                        initWithUTF8String:(const char *)
                        sqlite3_column_text(statement, 1)];
            NSLog(@"Settings page Login Details %@ %@", strDBUsername , strDBPwd );
            //                _address.text = addressField;
            //
            //                _phone.text = phoneField;
            //                _status.text = @"Match found";
        } else {
            //                _status.text = @"Match not found";
            //                _address.text = @"";
            //                _phone.text = @"";
        }
        sqlite3_finalize(statement);
    }
    else{
        NSLog(@"SQLITE NOT OK");
        NSLog(@"Error %s ", sqlite3_errmsg(_contactDB));
    }
    sqlite3_close(_contactDB);
}

0 个答案:

没有答案