-1
tx.executeSql("SELECT * FROM Routes WHERE id_object = 0", [], 
function (tx, result) 
{ 
alert('You already added'); 
},
function (tx, error) { 
tx.executeSql('INSERT INTO Routes (id_object) VALUES (0)');             
alert('add successfull')} ) 
}  

If the result set of SELECT * FROM Routes WHERE id_object = 0 is empty, it should execute tx.executeSql('INSERT INTO Routes (id_object) VALUES (0)');

TimWolla
  • 31,849
  • 8
  • 63
  • 96

1 Answers1

1

Is the id_object unique (i.e., a primary key, or declared UNIQUE)? If so, you can simply

INSERT OR IGNORE INTO Routes (id_object) VALUES (0);

If your id_object column cannot be made unique, then you need what's sometimes called an UPSERT (update or insert) statement. SQLite doesn't support this directly, but perhaps you can achieve it with a combination of triggers.

By far the easiest method is to do the test in your program's logic rather than in SQL, e.g., based on

SELECT COUNT(*) FROM Routes WHERE id_object = 0"

and if the result is 0 do your insert.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
  • tx.executeSql("SELECT Count(*) AS 'colvo' FROM Routes WHERE id_object = 0", [], function (tx, result) { if (result.rows['colvo'] == 0) alert('Вы уже добавляли данный объект ранее'); else {tx.executeSql('INSERT INTO Routes (id_object) VALUES (0)'); } },function (tx, error) { } ) Where is a bug? – user2382168 May 16 '13 at 04:30
  • Don't you need to use `result.rows.item(0).colvo == 0`? I am guessing at what programming language you are using. You need to look into the first row (which will be the only row) of the result set from the `select count(*)...` – Doug Currie May 16 '13 at 16:11