How to check whether a table exist in android sqlite

This question already has an answer here:

  • How does one check if a table exists in an Android SQLite database? 10 answers

  • 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

  • expand data
  • again expand data inside it
  • then search for folder with your app package name
  • Then open database folder inside it
  • expand and pull that database file into local, while pulling save it with .sqlite extension
  • Open DBBrowser and open the database which you have in local.
  • you can now check wether the table has created or it has rows in it.
  • 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

    上一篇: Extreme Sharding:每用户一个SQLite数据库

    下一篇: 如何检查一个表是否存在于android sqlite中