Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 数据库的通信_Ios_Iphone_Objective C_Xcode_Sqlite - Fatal编程技术网

Ios 数据库的通信

Ios 数据库的通信,ios,iphone,objective-c,xcode,sqlite,Ios,Iphone,Objective C,Xcode,Sqlite,我正在处理sqlite数据库从一个viewcontroller到另一个viewcontroller的通信,但是我不知道如何在第二个viewcontroller上获取数据库。下面是我正在使用的代码:- 关于第一视图控制器 //Creating a table of MOtivational Thoughts dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES

我正在处理sqlite数据库从一个viewcontroller到另一个viewcontroller的通信,但是我不知道如何在第二个viewcontroller上获取数据库。下面是我正在使用的代码:-

关于第一视图控制器

    //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.
    }
关于第二视图控制器

 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 {


    }

请找到我的错误并给我可修改的解决方案。

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

Second.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"S.H.E.D_DB"]];

您收到了哪些错误?
sqlite3\u是否打开
失败?您最好只打开数据库一次,并在屏幕之间共享连接(contactDB变量)。我没有发现任何错误,但当我在第二个视图上应用日志以获取数据库路径时,它会返回“null”您是指这一行:
NSLog(@“path value:%@”,databasePath)?如果是这样,您发布的所有数据库代码都与问题无关,您应该发布关于如何切换到第二个视图控制器的代码。此外,我建议您创建一个单独的类来处理数据库,该类可以打开数据库、获取记录、操作记录,然后关闭数据库。