首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据库通信

数据库通信
EN

Stack Overflow用户
提问于 2013-11-27 07:58:40
回答 1查看 77关注 0票数 0

我正在处理sqlite数据库从一个视图控制器到另一个视图控制器之间的通信,但是一些我无法在第二个视图控制器上获得数据库的方法。贝娄是我正在使用的代码。

论第一视图控制器

代码语言:javascript
复制
    //Creating a table of MOtivational Thoughts
    dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPath objectAtIndex:0];
    Second.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"S.H.E.D_DB"]];
    NSFileManager *filemgr = [NSFileManager defaultManager];
    if ([ filemgr fileExistsAtPath: Second.databasePath] == NO) {
        const char *dbpath = [Second.databasePath UTF8String];
        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt =
            "CREATE TABLE IF NOT EXISTS THOUGHTS (ID integer ,Motivation_Thaought TEXT)";

            if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
               NSLog(@"Failed to create table");
            }
            else
            {
                NSLog(@" created table");
            }
            sqlite3_close(contactDB);
        } else {


        }
    }

   // Fetching Data from table of MOtivational Thoughts 
   if(sqlite3_open([Second.databasePath UTF8String], &contactDB) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access

        int randomNumber = [self getRandomNumberBetween:0 to:6];

        NSString *queryString = [NSString stringWithFormat:@"Select * FROM THOUGHTS where ID= '%d'",randomNumber];
        const char* sql = [queryString UTF8String];
        NSLog(@"path value : %@", Second.databasePath);
        sqlite3_stmt *compiledStatement;

         if (sqlite3_prepare_v2(contactDB, sql, -1, &compiledStatement, nil)==SQLITE_OK)      {
            // Loop through the results and add them to the feeds array
            NSLog(@"Ready to enter while loop");

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                NSLog(@"reading");
                NSString *aid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                motivationView.text = aid;
                NSLog(@"Thought of the day %@", aid);

            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(contactDB);
       // Do any additional setup after loading the view from its nib.
    }

第二视图控制器研究

代码语言:javascript
复制
 NSLog(@"path value : %@", databasePath);
    if(sqlite3_open([databasePath UTF8String], &contactDB) == SQLITE_OK){
        char *errMsg;
        const char *sql_stmt =
        "CREATE TABLE IF NOT EXISTS SHEDSLIST (SHED_ID integer ,SHED_Name TEXT ,SHED_Description TEXT, SHED_TIME DATETIME)";

        if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {
            NSLog(@"Failed to create table");
        }
        else
        {
            NSLog(@" created table");
       }
        sqlite3_close(contactDB);
    } else {


    }

PLease定位我的错误并给我一个合适的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-27 09:29:05

也许您应该以self.databasePath的形式在SecondViewController中访问数据库路径,因为我相信您已经为此创建了一个属性。相反,您正在尝试访问某些实例变量databasePath。查看FirstViewController中的以下代码:

代码语言:javascript
复制
Second.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"S.H.E.D_DB"]];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20236821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档