Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3968498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:51:01+00:00 2026-05-20T03:51:01+00:00

Background All day long I’ve been trying to solve a problem, I read all

  • 0

Background

All day long I’ve been trying to solve a problem, I read all the articles and documentation that I could find on the Internet, but I can’t solve this. I’m writing an application for iPhone and I need to work with a sqlite database (sqlite3).

Main Problem

I have created my database and all is going good until I wanted to get a count of the rows in my table. The Table name is ARTICLES, so I wrote

SELECT COUNT(*) FROM ARTICLES

My program does nothing and writes in the log: Unknown Error.

const char *query = "SELECT COUNT (*) FROM ARTICLES";
sqlite3_stmt *compiledQuery;
sqlite3_prepare_v2(database, query, -1, &compiledQuery, NULL);

Program gives message "Unknown Error" in the above code, and I can’t get the count of rows.
Who can help me to solve this problem… or may be something with sqlite is not correct?

Code

- (int) GetArticlesCount
{
    if (sqlite3_open([self.dataBasePath UTF8String], &articlesDB) == SQLITE_OK)
    {
        const char* sqlStatement = "SELECT COUNT(*) FROM ARTICLES";
        sqlite3_stmt *statement;

        if( sqlite3_prepare_v2(articlesDB, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
        {
            if( sqlite3_step(statement) == SQLITE_DONE )
            {
   
            }
            else
            {
                NSLog( @"Failed from sqlite3_step. Error is:  %s", sqlite3_errmsg(articlesDB) );
            }
        }
        else
        {
            NSLog( @"Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(articlesDB) );
        }

        // Finalize and close database.
        sqlite3_finalize(statement);
        sqlite3_close(articlesDB);
    }

    return 0;
}

In this line the unknown error appears:

NSLog( @"Failed from sqlite3_step. Error is:  %s", sqlite3_errmsg(articlesDB) );

What must I add to the code or what must I do to get the count of rows? Please help…

Working Code (Not effective)

const char* sqlStatement = "SELECT * FROM ARTICLES";
sqlite3_stmt *statement;
if( sqlite3_prepare_v2(articlesDB, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
{
    int count = 0;
    while( sqlite3_step(statement) == SQLITE_ROW )
        count++;
}

I get the right count of rows! But I don’t think it is an effective method… I think that something with sqlite is not going right…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:51:01+00:00Added an answer on May 20, 2026 at 3:51 am

    Thank you for the update, I believe the problem is your check against SQLITE_DONE instead of SQLITE_ROW, so I have updated your method below:

    - (int)getArticlesCount {
      int count = 0;
      if (sqlite3_open([self.dataBasePath UTF8String], &articlesDB) ==
          SQLITE_OK) {
        const char* sqlStatement = "SELECT COUNT(*) FROM ARTICLES";
        sqlite3_stmt *statement;
    
        if (sqlite3_prepare_v2(articlesDB, sqlStatement, -1, &statement, NULL) ==
            SQLITE_OK) {
          // Loop through all the returned rows (should be just one)
          while(sqlite3_step(statement) == SQLITE_ROW) {
            count = sqlite3_column_int(statement, 0);
          }
        } else {
            NSLog(@"Failed from sqlite3_prepare_v2. Error is:  %s",
                  sqlite3_errmsg(articlesDB));
        }
    
        // Finalize and close database.
        sqlite3_finalize(statement);
        sqlite3_close(articlesDB);
      }
    
      return count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background A project installs some files that contain all the elements to define a
I am writing an application that downloads large files in the background. All clients
I have been struggling with this all day and have tried going through the
BACKGROUND I have been working on a tool that is supposed to work in
I'm trying to find the equivalent of IsFilled=False that is used in a PathGeometry,
I would like to customize the background (and maybe the border too) of all
Background: At my company we are developing a bunch applications that are using the
Background I am trying to create a copy of a business object I have
I am in the process of trying to make an app that has most
I have been using several Microbreaks software solutions. And all of them are overkill

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.