-1

So what do I get? I cannot find it and I already forgot. Will it be an error or just variable which is supposed to get the item will stay empty?

(I am asking it because I want to check if in db is already the element to avoid duplicates, if you have any other way feel free to post it)

EDIT: What is unclear about this question? I have asked what do I get if I execute SELECT query in SQLite (android) and the element is not present in the DB what value will have the cursor or what is the result. Is it an error or null value or what? As I already found out it is null value in cursor. That was all.

EDIT2: If it is so unclear how is it possible that Viger understood and answered, unfortunately in the comment:

You get an empty Cursor. Just use the cursor.getCount(); method to determine it it contains 0 rows

  • Just try it and see what happens? – donfuxx Mar 22 '14 at 11:09
  • 2
    [This answer](http://stackoverflow.com/a/5935434/1798304) shows you how to use `count`. You may utilize that and check if it returns `0`. – MalaKa Mar 22 '14 at 11:09
  • You get an empty Cursor. Just use the `cursor.getCount();` method to determine it it contains 0 rows. – Phantômaxx Mar 22 '14 at 11:15
  • As I already find out, you are right I got empty Cursor so yes, get count works, thank you, also thanks for minus point ... this question was not yet asked on the internet, or it was but I could not find it quickly enough so I don't think it is a bad question. Maybe a bit noobish but still many people may find it useful. – Ondrej 'zatokar' Tokár Mar 22 '14 at 11:24
  • @Ondrej 'zatokar' Tokár You're welcome. The minus is probably due to your lack of effort. As you just wrote, you already found that out, it wasn't a hard task, you simply needed to try. Also, the behaviour is more or less implied by the `Cursor` since `Cursor.moveToFirst()` returns a boolean, indicating whether or not there was an entry matching your query. And you will in most questions about the `ContentResolver` and `Cursor` find the usage of that method. – MalaKa Mar 22 '14 at 11:29
  • 1
    Thank you very much I got it :), and It was not my laziness I just thought it may help other people if the answer will be here since I could not find it... – Ondrej 'zatokar' Tokár Mar 22 '14 at 11:38

1 Answers1

1

Or if you need just an id for example, do something like this:

DECLARE roomId INTEGER;

SELECT id INTO roomId
FROM Room
WHERE roomNumber = '401';

IF roomId IS NULL THEN
  raise_application_error('Room does not exist', -20001);
END IF;

--do something with your ID

Edit: this is Oracle PL\SQL specific code, with some minor modifications you can change it to other database vendor's code.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71