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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:34:54+00:00 2026-06-14T21:34:54+00:00

My table has a column named status with data type CHARACTER . I wish

  • 0

My table has a column named status with data type CHARACTER. I wish to do the following.

If a row does not exists for a primary key, insert ‘active’ in
status. If a row already has an entry for a primary key, toggle
the status. Use ative, not_active values for toggling.

Currently I am doing it by writing multiple queries. Is there any single query to do this like Mysql?

  • 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-06-14T21:34:55+00:00Added an answer on June 14, 2026 at 9:34 pm

    SQLite does not have control logic commands like IF because that would not make sense in an embedded database.

    Implement the control logic in whatever language you’re using to access the database:

    db.begin_transaction()
    result = db.execute("SELECT status FROM MyTable WHERE id = ?", [id]);
    if result.length > 0:
        new_status = if result[0][0] == 'active': 'not_active' else: 'active'
        db.execute("UPDATE MyTable SET status = ? WHERE id = ?", [new_status, id])
    else:
        db.execute("INSERT INTO MyTable(id, status) VALUES(?, ?)", [id, 'active'])
    db.commit()
    

    However, the toggle logic itself can be implemented with a CASE expression, so if you don’t care too much about clarity, you can execute the following two commands instead, in this order (the UPDATE will do nothing if the record does not exist, and the INSERT OR IGNORE will do nothing if the new record would violate any unique constraint):

    UPDATE MyTable
    SET status = CASE status
                 WHEN 'active' THEN 'not_active'
                 ELSE               'active'
                 END
    WHERE id = ?;
    
    INSERT OR IGNORE INTO MyTable (id, status) VALUES (?, 'active');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have 2 tables, users and follows. table follows has a column named status.
My database table is named 'status' and it has a column named 'order', which
I have many tables, say T1, T2, T3. Every table has a column named
My table named pictures has a column named pic and I'm wanting to get
I have a self referencing table named categories that has a parentcategoryid column that
I have a table called Bowzer which has a column called Type . When
i have 3 tables A,B and C. table A has column employee_name,id table B
I have a table ProductNumberDuplicates_backups, which has two columns named ProductID and ProductNumber. There
PROFESSIONAL Table has many columns FIRST_NAME,MIDDLE_NAME, LAST_NAME, TITLE_CODE, COMMUNICATION_TYPE_CODE, COMMUNICATION_VALUE PROFESSIONAL Table COMMUNICATION_TYPE_CODE column
I'm using http://keith-wood.name/countdown.html . Each 'Event' table in my database has a 'date' column.

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.