sqlite - How to check table is empty with count(*) -
i trying check if table empty. code method:
public boolean checkifnull() throws sqlexception, classnotfoundexception { boolean flag=false; system.out.println("checking if table empty..."); string sq = "select count(*) table1"; try { class.forname(typedb); c = drivermanager.getconnection(path); stm = c.preparestatement(sq); preparedstatement stm = c.preparestatement(sq); int rowsaffected = stm.executeupdate(); if(rowsaffected == 0) flag=true; } catch (sqlexception e) { system.out.println(e.getmessage()); } { if (stm != null) { stm.close(); } if (c != null) { c.close(); } } return flag; }
but sth wrong hapenning , error message query returns results
exceptionn: java.sql.sqlexception: [sqlite_error] sql error or missing database (connection closed)
how check returning value of check?
update 1: instead of query above, tried select exists(select 1 table1)
same happening..
update 2: used this:
resultset rs = stm.executequery(); if(!rs.next()) flag=true; else system.err.println("error - table has records...");
and prints error - "the table has records..."
. how possible? see table through sqlite manager , empty!
you executing select
, need use executequery
, not executeupdate
. executeupdate
statements update
, delete
, insert
, executequery
executing statements return result set (like select
).
you need execute select statement, , do:
try (resultset rs = stm.executequery()) { rs.next(); // have row, count int count = rs.getint(1); flag = count == 0; }
the code in update won't work, because if select count(*) table
, always have 1 row (with count).
Comments
Post a Comment