Check If table exist or not
To check if table exists or not , below is the code snippet-
import java.sql.ResultSet;
public class Main {
public static void main(String[] argv) throws Exception {
Connection c = null;
DatabaseMetaData dbm = c.getMetaData();
ResultSet rs = dbm.getTables(null, null, "employee", null);
if (rs.next()) {
System.out.println("Table exists");
} else {
System.out.println("Table does not exist");
}
}
}
int tableCount = 0;
Cursor cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE name='" + tableName+ "' AND type='table';", null );
while(cursor.moveToNext()) {
tableCount++;
break;
}
cursor.close();
return (tableCount>0) ? true : false;
}
import java.sql.Connection;
import java.sql.DatabaseMetaData;import java.sql.ResultSet;
public class Main {
public static void main(String[] argv) throws Exception {
Connection c = null;
DatabaseMetaData dbm = c.getMetaData();
ResultSet rs = dbm.getTables(null, null, "employee", null);
if (rs.next()) {
System.out.println("Table exists");
} else {
System.out.println("Table does not exist");
}
}
}
For Android internal memory, SQLLite -
public boolean doesTableExists(String tableName) {int tableCount = 0;
Cursor cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE name='" + tableName+ "' AND type='table';", null );
while(cursor.moveToNext()) {
tableCount++;
break;
}
cursor.close();
return (tableCount>0) ? true : false;
}
Comments
Post a Comment