How to check whether a table exist in android sqlite
This question already has an answer here:
You can use the following statement to check if the table exists:
Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"
+ TABLE_NAME + "'", null);
SQLite maintains a table called sqlite_master containing the information of all the tables in the database. So if the resulting cursor returns a count of 1, you know the table exists. After you check that, query the database again with your desired query:
cursor = your-query-here;
And use cursor.getCount();
again to determine if you have data. If cursor.getCount();
returns more than 0 then you have the data you want.
SQLiteStatement:
SELECT * FROM sqlite_master WHERE name ='tablename' and type='table';
Call
db.rawQuery()
and read the Cursor or
SQLiteStatement s = db.compileStatement(SQLiteStatement);
long count = s.simpleQueryForLong();
if(count > 0) -> Table Exists
After you checked if it exists you can query a count on rows or so on. maybe you have to check if specific columns do exist for the table
Manual way of doing it
Visit following link and download DBrowser : Sqlite DBBrowser
Now, go to Android Device Monitor
.sqlite
extension EDIT
change the DATABASE_VERSION = 1
to DATABASE_VERSION = 2
So whenever you change anything within the onCreate()
and onUpgrade()
increase the Database version.
To check whether your table has rows,
if(list.size() == 0){
// empty
}
链接地址: http://www.djcxy.com/p/19810.html