@Test public void doesTableExistTest() { final String tableName = "testTable"; final String columnName = CustomSQLiteQueryBuilder.quoteIdentifier("col"); SQLiteDatabase db = Collect.getInstance().openOrCreateDatabase("testDatabase", MODE_PRIVATE, null); assertFalse(SQLiteUtils.doesTableExist(db, tableName)); List<String> columnDefinitions = new ArrayList<>(); columnDefinitions.add(CustomSQLiteQueryBuilder.formatColumnDefinition(columnName, "TEXT")); CustomSQLiteQueryExecutor.begin(db).createTable(tableName).columnsForCreate(columnDefinitions).end(); assertTrue(SQLiteUtils.doesTableExist(db, tableName)); }
public static boolean doesTableExist(SQLiteDatabase db, String tableName) { final String sqliteSystemTable = CustomSQLiteQueryBuilder.quoteIdentifier("sqlite_master"); final String nameColumn = CustomSQLiteQueryBuilder.quoteIdentifier("name"); final String typeColumn = CustomSQLiteQueryBuilder.quoteIdentifier("type"); final String tableLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral("table"); final String tableNameLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral(tableName); final String[] columnsToSelect = {nameColumn}; final String[] selectCriteria = { CustomSQLiteQueryBuilder.formatCompareEquals(typeColumn, tableLiteral), CustomSQLiteQueryBuilder.formatCompareEquals(nameColumn, tableNameLiteral) }; Cursor cursor = db.query(sqliteSystemTable, columnsToSelect, CustomSQLiteQueryBuilder.formatLogicalAnd(selectCriteria), null, null, null, null); boolean foundTable = cursor.getCount() == 1; cursor.close(); return foundTable; }
SQLiteUtils { public static boolean doesTableExist(SQLiteDatabase db, String tableName) { final String sqliteSystemTable = CustomSQLiteQueryBuilder.quoteIdentifier("sqlite_master"); final String nameColumn = CustomSQLiteQueryBuilder.quoteIdentifier("name"); final String typeColumn = CustomSQLiteQueryBuilder.quoteIdentifier("type"); final String tableLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral("table"); final String tableNameLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral(tableName); final String[] columnsToSelect = {nameColumn}; final String[] selectCriteria = { CustomSQLiteQueryBuilder.formatCompareEquals(typeColumn, tableLiteral), CustomSQLiteQueryBuilder.formatCompareEquals(nameColumn, tableNameLiteral) }; Cursor cursor = db.query(sqliteSystemTable, columnsToSelect, CustomSQLiteQueryBuilder.formatLogicalAnd(selectCriteria), null, null, null, null); boolean foundTable = cursor.getCount() == 1; cursor.close(); return foundTable; } }
SQLiteUtils { public static boolean doesTableExist(SQLiteDatabase db, String tableName) { final String sqliteSystemTable = CustomSQLiteQueryBuilder.quoteIdentifier("sqlite_master"); final String nameColumn = CustomSQLiteQueryBuilder.quoteIdentifier("name"); final String typeColumn = CustomSQLiteQueryBuilder.quoteIdentifier("type"); final String tableLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral("table"); final String tableNameLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral(tableName); final String[] columnsToSelect = {nameColumn}; final String[] selectCriteria = { CustomSQLiteQueryBuilder.formatCompareEquals(typeColumn, tableLiteral), CustomSQLiteQueryBuilder.formatCompareEquals(nameColumn, tableNameLiteral) }; Cursor cursor = db.query(sqliteSystemTable, columnsToSelect, CustomSQLiteQueryBuilder.formatLogicalAnd(selectCriteria), null, null, null, null); boolean foundTable = cursor.getCount() == 1; cursor.close(); return foundTable; } private  SQLiteUtils(); }
SQLiteUtils { public static boolean doesTableExist(SQLiteDatabase db, String tableName) { final String sqliteSystemTable = CustomSQLiteQueryBuilder.quoteIdentifier("sqlite_master"); final String nameColumn = CustomSQLiteQueryBuilder.quoteIdentifier("name"); final String typeColumn = CustomSQLiteQueryBuilder.quoteIdentifier("type"); final String tableLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral("table"); final String tableNameLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral(tableName); final String[] columnsToSelect = {nameColumn}; final String[] selectCriteria = { CustomSQLiteQueryBuilder.formatCompareEquals(typeColumn, tableLiteral), CustomSQLiteQueryBuilder.formatCompareEquals(nameColumn, tableNameLiteral) }; Cursor cursor = db.query(sqliteSystemTable, columnsToSelect, CustomSQLiteQueryBuilder.formatLogicalAnd(selectCriteria), null, null, null, null); boolean foundTable = cursor.getCount() == 1; cursor.close(); return foundTable; } private  SQLiteUtils(); static boolean doesTableExist(SQLiteDatabase db, String tableName); static boolean doesColumnExist(SQLiteDatabase db, String tableName, String columnName); static List<String> getColumnNames(SQLiteDatabase db, String tableName); static void addColumn(SQLiteDatabase db, String table, String column, String type); static void renameTable(SQLiteDatabase db, String table, String newTable); static void copyRows(SQLiteDatabase db, String srcTable, String[] columns, String dstTable); static void dropTable(SQLiteDatabase db, String table); }
SQLiteUtils { public static boolean doesTableExist(SQLiteDatabase db, String tableName) { final String sqliteSystemTable = CustomSQLiteQueryBuilder.quoteIdentifier("sqlite_master"); final String nameColumn = CustomSQLiteQueryBuilder.quoteIdentifier("name"); final String typeColumn = CustomSQLiteQueryBuilder.quoteIdentifier("type"); final String tableLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral("table"); final String tableNameLiteral = CustomSQLiteQueryBuilder.quoteStringLiteral(tableName); final String[] columnsToSelect = {nameColumn}; final String[] selectCriteria = { CustomSQLiteQueryBuilder.formatCompareEquals(typeColumn, tableLiteral), CustomSQLiteQueryBuilder.formatCompareEquals(nameColumn, tableNameLiteral) }; Cursor cursor = db.query(sqliteSystemTable, columnsToSelect, CustomSQLiteQueryBuilder.formatLogicalAnd(selectCriteria), null, null, null, null); boolean foundTable = cursor.getCount() == 1; cursor.close(); return foundTable; } private  SQLiteUtils(); static boolean doesTableExist(SQLiteDatabase db, String tableName); static boolean doesColumnExist(SQLiteDatabase db, String tableName, String columnName); static List<String> getColumnNames(SQLiteDatabase db, String tableName); static void addColumn(SQLiteDatabase db, String table, String column, String type); static void renameTable(SQLiteDatabase db, String table, String newTable); static void copyRows(SQLiteDatabase db, String srcTable, String[] columns, String dstTable); static void dropTable(SQLiteDatabase db, String table);  }