Java Code Examples for org.greenrobot.greendao.database.Database#rawQuery()

The following examples show how to use org.greenrobot.greendao.database.Database#rawQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MigrationHelper.java    From NovelReader with MIT License 6 votes vote down vote up
private List<String> getColumns(Database db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}
 
Example 2
Source File: DbMigrationHelper.java    From ml-authentication with Apache License 2.0 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    Log.i(DbMigrationHelper.class.getName(), "getColumns");

    List<String> columns = null;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
        if (null != cursor && cursor.getColumnCount() > 0) {
            columns = Arrays.asList(cursor.getColumnNames());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        if (null == columns)
            columns = new ArrayList<>();
    }
    return columns;
}
 
Example 3
Source File: DaoMigrationHelper.java    From android-mvp-starter with MIT License 6 votes vote down vote up
private List<String> getColumns(Database db, String tableName) {
    List<String> columns = null;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
        if (null != cursor && cursor.getColumnCount() > 0) {
            columns = Arrays.asList(cursor.getColumnNames());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        if (null == columns)
            columns = new ArrayList<>();
    }
    return columns;
}
 
Example 4
Source File: MigrationHelper2.java    From HHComicViewer with Apache License 2.0 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = null;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
        if (null != cursor && cursor.getColumnCount() > 0) {
            columns = Arrays.asList(cursor.getColumnNames());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
        if (null == columns) {
            columns = new ArrayList<>();
        }
    }
    return columns;
}
 
Example 5
Source File: MigrationHelper2.java    From HHComicViewer with Apache License 2.0 6 votes vote down vote up
private static boolean isTableExists(Database db, boolean isTemp, String tableName) {
    if (db == null || TextUtils.isEmpty(tableName)) {
        return false;
    }
    String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER;
    String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?";
    Cursor cursor = null;
    int count = 0;
    try {
        cursor = db.rawQuery(sql, new String[]{"table", tableName});
        if (cursor == null || !cursor.moveToFirst()) {
            return false;
        }
        count = cursor.getInt(0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return count > 0;
}
 
Example 6
Source File: MigrationHelper.java    From GreenDaoSample with Apache License 2.0 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}
 
Example 7
Source File: DbMigrationHelper.java    From ml-authentication with Apache License 2.0 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    Log.i(DbMigrationHelper.class.getName(), "getColumns");

    List<String> columns = null;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
        if (null != cursor && cursor.getColumnCount() > 0) {
            columns = Arrays.asList(cursor.getColumnNames());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        if (null == columns)
            columns = new ArrayList<>();
    }
    return columns;
}
 
Example 8
Source File: MigrationHelper.java    From Android-Architecture with Apache License 2.0 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (cursor != null) cursor.close();
    }
    return columns;
}
 
Example 9
Source File: MyMigrationHelper.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = null;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
        if (null != cursor && cursor.getColumnCount() > 0) {
            columns = Arrays.asList(cursor.getColumnNames());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        if (null == columns)
            columns = new ArrayList<>();
    }
    return columns;
}
 
Example 10
Source File: MyMigrationHelper.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isTableExists(Database db, boolean isTemp, String tableName) {
    if (db == null || TextUtils.isEmpty(tableName)) {
        return false;
    }
    String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER;
    String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?";
    Cursor cursor=null;
    int count = 0;
    try {
        cursor = db.rawQuery(sql, new String[]{"table", tableName});
        if (cursor == null || !cursor.moveToFirst()) {
            return false;
        }
        count = cursor.getInt(0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return count > 0;
}
 
Example 11
Source File: MigrationHelper.java    From FriendBook with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isTableExists(Database db, boolean isTemp, String tableName) {
    if (db == null || TextUtils.isEmpty(tableName)) {
        return false;
    }
    String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER;
    String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?";
    Cursor cursor=null;
    int count = 0;
    try {
        cursor = db.rawQuery(sql, new String[]{"table", tableName});
        if (cursor == null || !cursor.moveToFirst()) {
            return false;
        }
        count = cursor.getInt(0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return count > 0;
}
 
Example 12
Source File: Update2Helper.java    From NovelReader with MIT License 6 votes vote down vote up
private List<String> getColumns(Database db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}
 
Example 13
Source File: MigrationHelper.java    From Android with MIT License 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = null;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
        if (null != cursor && cursor.getColumnCount() > 0) {
            columns = Arrays.asList(cursor.getColumnNames());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        if (null == columns)
            columns = new ArrayList<>();
    }
    return columns;
}
 
Example 14
Source File: MigrationHelper.java    From Android with MIT License 6 votes vote down vote up
/**
 *
 * @param db
 * @param isTemp
 * @param tableName
 * @return
 */
public static boolean isTableExists(Database db, boolean isTemp, String tableName) {
    if (db == null || TextUtils.isEmpty(tableName)) {
        return false;
    }
    String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER;
    String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?";
    Cursor cursor=null;
    int count = 0;
    try {
        cursor = db.rawQuery(sql, new String[]{"table", tableName});
        if (cursor == null || !cursor.moveToFirst()) {
            return false;
        }
        count = cursor.getInt(0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return count > 0;
}
 
Example 15
Source File: MigrationHelper.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
/**
 * 获取表列名集合
 **/
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage());
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}
 
Example 16
Source File: MigrationHelper.java    From FriendBook with GNU General Public License v3.0 6 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = null;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
        if (null != cursor && cursor.getColumnCount() > 0) {
            columns = Arrays.asList(cursor.getColumnNames());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        if (null == columns)
            columns = new ArrayList<>();
    }
    return columns;
}
 
Example 17
Source File: Update2Helper.java    From NovelReader with MIT License 5 votes vote down vote up
private void updateCollBook(Database db) {
    Class<? extends AbstractDao<?, ?>> collBookClass = CollBookBeanDao.class;

    // 遍历查找本地文件,然后修改本地文件的数据
    DaoConfig daoConfig = new DaoConfig(db, collBookClass);
    String tableName = daoConfig.tablename;

    Cursor cursor = db.rawQuery("select _ID,IS_LOCAL from " + tableName, null);
    String id = null;
    String cover = null;
    String isLocal = null;

    StringBuilder updateSb = new StringBuilder();
    while (cursor.moveToNext()) {
        cover = cursor.getString(0);
        id = MD5Utils.strToMd5By16(cover);
        isLocal = cursor.getString(1);

        //如果是本地文件
        if (isLocal.equals("1")) {
            // 数据更新
            updateSb.append("UPDATE " + tableName + " SET ");
            updateSb.append("_ID=").append(String.format(QUOTE, id)).append(DIVIDER);
            updateSb.append("COVER=").append(String.format(QUOTE, cover)).append(" ");
            updateSb.append("WHERE _ID=").append(String.format(QUOTE,cover)).append(";");

            db.execSQL(updateSb.toString());
            updateSb.delete(0, updateSb.length());
        }
    }
}
 
Example 18
Source File: GreenDaoUpgrade.java    From MissZzzReader with Apache License 2.0 5 votes vote down vote up
private static List<String> getColumns(Database db, String tableName) {
    List<String> columns = new ArrayList<>();
    try (Cursor cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null)) {
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    }
    return columns;
}
 
Example 19
Source File: Update2Helper.java    From NovelReader with MIT License 4 votes vote down vote up
/**
 * 生成临时列表
 *
 * @param db
 */
private void generateTempTables(Database db, Class<? extends AbstractDao<?, ?>> bookChapterClass) {
    // 解析 GreenDao,获取 table 名
    DaoConfig daoConfig = new DaoConfig(db, bookChapterClass);
    String tableName = daoConfig.tablename;

    // 创建临时 table 名。
    String tempTableName = daoConfig.tablename.concat("_TEMP");
    ArrayList<String> properties = new ArrayList<>();

    StringBuilder createTableStringBuilder = new StringBuilder();
    createTableStringBuilder.append("CREATE TABLE ").append(tempTableName).append(" (");

    // 新增的三个字段
    String ID = "ID";
    String START = "START";
    String END = "end";


    // 新建的 id 主键字段
    createTableStringBuilder.append(ID + " ").append("TEXT ").append("PRIMARY KEY");
    properties.add(ID);

    // 获取符合新表的旧字段。
    for (int j = 0; j < daoConfig.properties.length; j++) {
        String columnName = daoConfig.properties[j].columnName;
        if (getColumns(db, tableName).contains(columnName)) {
            properties.add(columnName);

            String type = null;

            try {
                type = getTypeByClass(daoConfig.properties[j].type);
            } catch (Exception exception) {
                exception.printStackTrace();
            }

            createTableStringBuilder.append(DIVIDER).append(columnName).append(" ").append(type);
        }
    }

    // 新建的 START,和 END 字段。
    createTableStringBuilder.append(DIVIDER).append(START).append(" ").append("INTEGER");
    createTableStringBuilder.append(DIVIDER).append(END).append(" ").append("INTEGER");

    properties.add(START);
    properties.add(END);

    createTableStringBuilder.append(");");
    // 创建临时数据表
    db.execSQL(createTableStringBuilder.toString());

    StringBuilder insertTableStringBuilder = new StringBuilder();

    // 将 link 字段的文件的内容转换成 Id
    Cursor cursor = db.rawQuery("select * from " + daoConfig.tablename, null);

    String id = null;
    String link = null;
    String title = null;
    String taskName = null;
    String unreadble = null;
    String bookId = null;

    while (cursor.moveToNext()) {
        link = cursor.getString(0);
        id = MD5Utils.strToMd5By16(link);
        title = cursor.getString(1);
        taskName = cursor.getString(2);
        unreadble = cursor.getString(4);
        bookId = cursor.getString(3);

        insertTableStringBuilder.append("INSERT INTO ").append(tempTableName).append(" (");
        insertTableStringBuilder.append(TextUtils.join(",", properties));
        insertTableStringBuilder.append(") VALUES (");
        insertTableStringBuilder.append(String.format(QUOTE, id)).append(DIVIDER);
        insertTableStringBuilder.append(String.format(QUOTE, link)).append(DIVIDER);
        insertTableStringBuilder.append(String.format(QUOTE, title)).append(DIVIDER);
        insertTableStringBuilder.append(String.format(QUOTE, taskName)).append(DIVIDER);
        insertTableStringBuilder.append(unreadble).append(DIVIDER);
        insertTableStringBuilder.append(String.format(QUOTE, bookId)).append(DIVIDER);
        insertTableStringBuilder.append("0").append(DIVIDER);
        insertTableStringBuilder.append("0").append(");");

        db.execSQL(insertTableStringBuilder.toString());

        insertTableStringBuilder.delete(0, insertTableStringBuilder.length());
    }
}