[sqlite] How to check whether a table is empty or not in sqlite.

182 views
Skip to first unread message

bhaskarReddy

unread,
Feb 13, 2012, 7:57:52 AM2/13/12
to sqlite...@sqlite.org

HI Friends,

How to check whether a table is empty or not. If a table is empty, i
want to do some logic. If not another logic.


Can any one tell me how to check if table in a data base is empty
or not, using sqlite3.


Regards,
Bhaskar.
--
View this message in context: http://old.nabble.com/How-to-check-whether-a-table-is-empty-or-not-in-sqlite.-tp33314679p33314679.html
Sent from the SQLite mailing list archive at Nabble.com.

_______________________________________________
sqlite-users mailing list
sqlite...@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Igor Tandetnik

unread,
Feb 13, 2012, 8:02:27 AM2/13/12
to sqlite...@sqlite.org
bhaskarReddy <uni...@gmail.com> wrote:
> How to check whether a table is empty or not. If a table is empty, i
> want to do some logic. If not another logic.

select exists (select 1 from MyTable);

--
Igor Tandetnik

Simon Slavin

unread,
Feb 13, 2012, 10:48:24 AM2/13/12
to General Discussion of SQLite Database

On 13 Feb 2012, at 1:02pm, Igor Tandetnik wrote:

> bhaskarReddy <uni...@gmail.com> wrote:
>> How to check whether a table is empty or not. If a table is empty, i
>> want to do some logic. If not another logic.
>
> select exists (select 1 from MyTable);

Or

SELECT COUNT(*) from MyTable

which will give you 0 if the table is empty, and some other integer otherwise.

Simon.

Jay A. Kreibich

unread,
Feb 13, 2012, 11:07:13 AM2/13/12
to General Discussion of SQLite Database
On Mon, Feb 13, 2012 at 03:48:24PM +0000, Simon Slavin scratched on the wall:

>
> On 13 Feb 2012, at 1:02pm, Igor Tandetnik wrote:
>
> > bhaskarReddy <uni...@gmail.com> wrote:
> >> How to check whether a table is empty or not. If a table is empty, i
> >> want to do some logic. If not another logic.
> >
> > select exists (select 1 from MyTable);

> SELECT COUNT(*) from MyTable

For a large table, that might take some time.

-j

--
Jay A. Kreibich < J A Y @ K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
but showing it to the wrong people has the tendency to make them
feel uncomfortable." -- Angela Johnson

Kit

unread,
Feb 13, 2012, 11:20:11 AM2/13/12
to General Discussion of SQLite Database
2012/2/13 Igor Tandetnik <itand...@mvps.org>:

> bhaskarReddy <uni...@gmail.com> wrote:
>>        How to check whether a table is empty or not. If a table is empty, i
>> want to do some logic. If not another logic.
>
> select exists (select 1 from MyTable);

SELECT exists(SELECT 1 FROM MyTable LIMIT 1);
--
Kit

Igor Tandetnik

unread,
Feb 13, 2012, 11:37:45 AM2/13/12
to sqlite...@sqlite.org
On 2/13/2012 11:20 AM, Kit wrote:
> 2012/2/13 Igor Tandetnik<itand...@mvps.org>:
>> bhaskarReddy<uni...@gmail.com> wrote:
>>> How to check whether a table is empty or not. If a table is empty, i
>>> want to do some logic. If not another logic.
>>
>> select exists (select 1 from MyTable);
>
> SELECT exists(SELECT 1 FROM MyTable LIMIT 1);

exists() predicate is smart enough to stop as soon as the first row is
retrieved. LIMIT 1 clause is redundant, though harmless.
--
Igor Tandetnik

Reply all
Reply to author
Forward
0 new messages