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

发布于 2024-11-01 17:10:40 字数 345 浏览 1 评论 0原文

我正在 iPhone 中开发一个应用程序来通过 sqlite 访问数据库。我声明了一种从数据库检索数据的方法。该方法的返回类型是否为NSArray?在sqlite select语句之后我们应该做什么来检索数据?

我的意思是在这个查询之后。

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

请帮帮我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓬勃野心 2024-11-08 17:10:40
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 ) {

 } 
  }

while 中,您可以从 sqlite 获取数据并将其添加到数组!

祝你好运 !!!

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 !!!

dawn曙光 2024-11-08 17:10:40
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;

这样就需要从数据库中读取。您可以通过声明类来管理所有这些检索到的字段。例如,如果从登录表中检索字段,您可以将类命名为 LoginClass,并具有 2 个属性 UserName、Password。创建此类的实例并存储从 Db 检索的字段值并将其添加到 Array 并在任何地方使用它们。

希望这有帮助。

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. e.g 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.

心不设防 2024-11-08 17:10:40
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];
}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文