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 3873476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:02:28+00:00 2026-05-19T22:02:28+00:00

Is it possible to iterate over a result set and if a condition is

  • 0

Is it possible to iterate over a result set and if a condition is met, delete the current row?

i.e. something like

int rc;
sqlite3_stmt* statement;
sqlite3_exec(db, "BEGIN", 0, 0, 0);

sqlite3_prepare_v2(_db, "SELECT id,status,filename,del FROM mytable", -1, &statement, NULL);

rc = sqlite3_step(statement);
while (rc == SQLITE_ROW){
  int id = sqlite3_column_int(statement, 1);
  int status = sqlite3_column_int(statement, 2);
  const unsigned char* filename = sqlite3_column_int(statement, 3);
  int del = sqlite3_column_int(statement, 4);

  if (status == 0 || del > 0){
    int rc = unlink(filename);
    if (rc == 0)
      // Now delete the current row
    else 
      // unlink failed, find out why, try again or ... ?
  } 

  rc = sqlite3_step(statement);
}
sqlite3_finalize(statement);
sqlite3_exec(db, "COMMIT", 0, 0, 0);

I could just call a single sql statement to delete all rows that match the criteria, but I don’t want to do that if for some reason the unlink fails.

Can I call an operation to delete the current row?

EDIT:
So there is a special column called rowid. Do I just add that that as a column in the
previous statement and create another statement like “delete from table where rowid=?” and pass in the current rowid?

That should work right? Is this the best way of going about it?

  • 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-19T22:02:28+00:00Added an answer on May 19, 2026 at 10:02 pm

    In terms of efficiency, it’s probably not the most efficient. If you’re doing this for something on the level of thousands or greater number of rows, you should consider doing one (or a combination) of the following:

    • Change your query to only consider rows whose del is > 0 (SELECT id,status,filename,del FROM mytable WHERE del > 0). You’re performing a table scan with your current method, which you should always try to avoid. Also make sure you have an index on the del column.

    • Build up an intermediary array of row ids, and then perform a query of the following form: DELETE FROM table WHERE id IN (?), and the parameterized value is your collected row ids joined into a comma separated string. Based on the number of rows you’re dealing with, you could set this delete to be performed in batches (delete in batch sizes of 1000, 5000, etc.); since it’s SQLite, tune to the device you’re running with.

    • Register a custom SQLite function at connection creation time using the form:

      void deleteFileFunc(sqlite3_context * context, int argc, sqlite3_value ** argv) {
        assert(argc == 1);
        const char * fileName = sqlite3_value_text(argv[0]);
        int rc = unlink(fileName);
        sqlite3_result_int(context, rc);
      }
      sqlite3_create_function(db, "delete_file", 1, SQLITE3_UTF8, NULL, &deleteFileFunc, NULL, NULL);
      

    and then change your database query to the form DELETE FROM mytable WHERE del > 0 AND delete_file(filename) == 0. The row will only be deleted if the delete succeeds, and you don’t need to iterate over the result set. SQLite 3 create function page: http://www.sqlite.org/c3ref/create_function.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to iterate over a Set and remove the elements from the
Is it possible to iterate over a pymongo Cursor as a key-value pair like
Is it possible to iterate over a list of functions in MATLAB? I'm trying
Possible Duplicate: Django - Iterate over model instance field names and values in template
I am trying to (1) iterate over all possible combinations of letters and (2)
Is it possible to do something like this in Python using regular expressions? Increment
Is it possible to iterate collection and list only filtered object information while debugging
I'm trying to iterate over a 2D array that is structured in this specific
In my user control's paint handler I iterate over a collection of predefined Bitmap
Is it possible to merge two assemblies at runtime such that, when you iterate

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.