简体   繁体   中英

Retrieving the data from database through sqlite in iphone

I am developing an application in iphone to access the database through sqlite. I declared a method to retrieve the data from database. Whether the return type is an NSArray for this method? After the sqlite select statement what should we do to retrieve the data?

I mean after this query.

NSString *selectStmt=[NSString stringWithFormat:@"select * from %@" , tableName];
sqlite3_prepare_v2(db, [selectStmt UTF8STRING] , -1, &statement, NULL);

please help me out.

NSString *selectStmt=[NSString stringWithFormat:@"select * from %@", tableName];

if( sqlite3_prepare_v2(db, [selectStmt UTF8STRING] , -1,&statement, NULL) == SQLITE_OK ) {
while( sqlite3_step(statement) == SQLITE_ROW ) {

 } 
  }

Here in while you can get data from sqlite and add it to array !!!

Good luck !!!

const char *sqlStatement = "your query";

if(sqlite3_prepare_v2(database, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK) 
{
    // Loop through the results and add them to the feeds array
    while(sqlite3_step(selectStmt) == SQLITE_ROW) 
    {
        NSString *stringField = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 1)];
        NSInteger intField = sqlite3_column_int(selectStmt, 0);
    }
}

sqlite3_finalize(selectStmt);
selectStmt = nil;

This way you need to read from the database. You can manage all those retrieved fields by declaring a Class. eg if retrieving fields from Login table you can have class as LoginClass with 2 Properties UserName,Password. Create instance of this class and store fields values retrieved from Db and add it to Array and use them any where Like that.

Hope this helps.

const char *sql = "select * from stocks";

sqlite3_stmt *output;

if(sqlite3_prepare_v2(dbase, sql, -1, &output, NULL)==SQLITE_OK)
{
[temp removeAllObjects];
[temp1 removeAllObjects];
while(sqlite3_step(output)==SQLITE_ROW)
{

    NSMutableString *str = [NSString stringWithUTF8String:(char  *)sqlite3_column_text(output, 0)];
    [temp1 addObject:str];
    str = [NSString stringWithUTF8String:(char *)sqlite3_column_text(output, 1)];
    [temp addObject:str];
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM