简体   繁体   English

在iPhone中通过sqlite从数据库检索数据

[英]Retrieving the data from database through sqlite in iphone

I am developing an application in iphone to access the database through sqlite. 我正在开发iPhone中的应用程序,以通过sqlite访问数据库。 I declared a method to retrieve the data from database. 我声明了一种从数据库检索数据的方法。 Whether the return type is an NSArray for this method? 此方法的返回类型是否为NSArray After the sqlite select statement what should we do to retrieve the data? 在sqlite select语句之后,我们应该怎么做才能检索数据?

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 !!! 在这里, 你可以从sqlite的数据,并把它添加到阵列!

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. 例如,如果从Login表中检索字段,则可以将类设为LoginClass,并带有2个属性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. 创建此类的实例并存储从Db检索到的字段值,并将其添加到Array并在类似的任何地方使用它们。

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];
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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