如果sqlite数据库中有任何数据,如何检查iphone应用程序

时间:2012-09-05 10:14:18

标签: iphone xcode web-services

如何在应用程序开始时检查是否存在sqlite中的数据,如果数据在数据库中可用,那么它应该通过其他方式上传正常工作应用程序

我将使用post上传此数据并使用php将数据上传到服务器。

     + (void) getInitialDataToDisplay:(NSString *)dbPath {

CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    const char *sql = "select response_Id,question_id,answer_option,answer_text,update_date_time from survey_question_responses";



    sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

        while(sqlite3_step(selectstmt) == SQLITE_ROW) {

            NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
            Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
            coffeeObj.question_Id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
            coffeeObj.answer_text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)];

            coffeeObj.update_date_time = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
            int count=[appDelegate.coffeeArray count];

            NSLog(@"count is beofore getting values %d",count);




            [appDelegate.coffeeArray addObject:coffeeObj];



            int countone=[appDelegate.coffeeArray count];

            NSLog(@"count is after getting values %d",countone);

            [coffeeObj release];
        }
       }
     }
     else
    sqlite3_close(database); //Even though the open call failed, close the    database connection to release all the memory.
       }

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式获取数据库中存在的行数:

SELECT COUNT(*) FROM table;

如果它返回0,则表格为空。

//更新...................

      const char *sqlQuery="Select count(*)from yourTable";
    if(sqlite3_prepare_v2(database, sqlQuery, -1, &queryStatement, NULL)==SQLITE_OK)
    {
        while (sqlite3_step(queryStatement)==SQLITE_ROW) 
        {

            const char *count=(const char *)sqlite3_column_text(queryStatement, 0);
           NSString *rowCount=[NSString stringWithCString:count encoding:NSASCIIStringEncoding];

       //here you will get the number of rows in string format;


        }

    }