Note that there are some explanatory texts on larger screens.

plurals
  1. POsqlite how to add value on conflict
    primarykey
    data
    text
    <p>I have a database with product name,product price and product counter. Product name is unique,product price gets replaced everytime a new value is entered and the problem is the product counter. Its default value is <code>1</code>,when a new product is entered his value is set to 1.I need it to increment whenever there is a conflict for the product name.So if <code>Cheese</code> is entered twice,the counter will say <code>2</code> and so on. What i need it to do is when there is a conflict,add 1 to its value. I want to do it this way because i'll need this later.I'll need to add the inputed value to the table value on some other thing i plan to implement in my app.</p> <p>How can i achieve this ? I'd like to keep doing it the way i'm doing it now,with the <code>contentvalues</code> method of inserting and not with the sqlite syntax(<code>INSERT,SELECT,etc</code>).Is that even possible ? Cuz i'm an absolute 0 at sqlite syntax.And also,i need it to have a method that i can call in other activities to insert into the database (like <code>insertCountry(Japan,10)</code>)</p> <pre><code>public class SQLiteCountryAssistant extends SQLiteOpenHelper { private static final String DB_NAME = "usingsqlite.db"; private static final int DB_VERSION_NUMBER = 1; private static final String DB_TABLE_NAME = "countries"; private static final String DB_COLUMN_1_NAME = "country_name"; private static final String DB_COLUMN_2_NAME = "country_price"; private static final String DB_COLUMN_3_NAME = "country_counter"; private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME + " (_id integer primary key autoincrement, country_name text UNIQUE ON CONFLICT REPLACE,country_price text,country_counter integer default '1' );)"; </code></pre> <p>And this is how i insert :</p> <pre><code>public void insertCountry(String countryName, String countryPrice) { sqliteDBInstance.execSQL("INSERT INTO " + DB_TABLE_NAME + "(country_name, country_price) VALUES('" + countryName + "', '" + countryPrice + "')"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload