---
title: "Problem in inserting value where not exist in database:"  
description: "Problem in inserting value where not exist in database:"  
author: "Anonymous User"  
published: 2015-10-01  
updated: 2023-05-27  
canonical: https://www.mindstick.com/forum/33487/problem-in-inserting-value-where-not-exist-in-database  
category: "sqlite"  
tags: ["database", "sqlite", "sqlite3"]  
reading_time: 3 minutes  

---

# Problem in inserting value where not exist in database:

I want to [insert](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net) [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) where value is not [present](https://answers.mindstick.com/qa/96635/explain-about-the-various-features-present-in-ms-access) in [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table),

[problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) in this [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):

```
INSERT INTO table (...)VALUES (...) WHERE NOT EXISTS (...)
```

[exists](https://www.mindstick.com/forum/158947/how-do-you-use-the-exists-operator-to-check-for-the-existence-of-records-in-a-subquery) using Sqlite3?\
EDIT:\
INSERT OR IGNORE

Is not working in my case

## Replies

### Reply by Aryan Kumar

If you are facing difficulties inserting values into an SQLite database when the value does not already exist, you can use the following approach:

1. Define a unique constraint:\
- Ensure that the column or columns where you want to insert unique values have a unique constraint defined. This constraint prevents duplicate values from being inserted into the column(s). You can specify the unique constraint when creating the [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) or alter the table later to add the constraint. For example:

```plaintext
    CREATE TABLE my_table (
        column_name INTEGER UNIQUE,
        other_columns...
    );
```

2. Use the INSERT OR IGNORE statement:\
- SQLite provides the `INSERT OR IGNORE` statement, which allows you to insert a row into a table only if it does not violate any unique constraints. If a conflict occurs, the statement will silently ignore the insertion without raising an error. Here's an example:

```plaintext
    INSERT OR IGNORE INTO my_table (column_name, other_columns...)
    VALUES (value1, value2, ...);
```

- Replace `my_table` with the name of your table and provide the appropriate column names and values.

By combining a unique constraint with the `INSERT OR IGNORE` statement, you can ensure that only unique values are inserted into the specified column(s). If a value already exists, it will be ignored, and no new row will be inserted.

Note: If you want to handle conflicts differently, you can also use other conflict resolution methods like `INSERT OR REPLACE` or `INSERT OR ABORT`, depending on your requirements.

Additionally, ensure that you are properly handling errors and exceptions in your code. If an error occurs during the insertion, it's important to capture and handle any exceptions raised by the SQLite library to avoid unexpected behavior or data inconsistencies.

### Reply by Tarun Kumar

you can use this method to check the value is exist in [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) or not.\
I hope it will be helpful for you:

```
public boolean isRecordExist(String TableName, String columnName,                                        String value) {      DbHelper  dbHelper = new DbHelper(this);      dbHelper.createDataBase();      SQLiteDatabase sqldb = dbHelper.openDataBase();      String Query = "Select * from " + TableName + " where " + columnName + "='" + value + "'";      Cursor cursor = sqldb.rawQuery(Query, null);      if (cursor != null && cursor.getCount() <= 0) {          cursor.close();          return false;      } else if (cursor != null) {          cursor.close();      }      return true;}
```


---

Original Source: https://www.mindstick.com/forum/33487/problem-in-inserting-value-where-not-exist-in-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
