当前位置: 首页>>代码示例>>Java>>正文


Java Cursor.getColumnNames方法代码示例

本文整理汇总了Java中android.database.Cursor.getColumnNames方法的典型用法代码示例。如果您正苦于以下问题:Java Cursor.getColumnNames方法的具体用法?Java Cursor.getColumnNames怎么用?Java Cursor.getColumnNames使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.database.Cursor的用法示例。


在下文中一共展示了Cursor.getColumnNames方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: RootCursorWrapper

import android.database.Cursor; //导入方法依赖的package包/类
public RootCursorWrapper(String authority, String rootId, Cursor cursor, int maxCount) {
    mAuthority = authority;
    mRootId = rootId;
    mCursor = cursor;

    final int count = cursor.getCount();
    if (maxCount > 0 && count > maxCount) {
        mCount = maxCount;
    } else {
        mCount = count;
    }

    if (cursor.getColumnIndex(COLUMN_AUTHORITY) != -1
            || cursor.getColumnIndex(COLUMN_ROOT_ID) != -1) {
        throw new IllegalArgumentException("Cursor contains internal columns!");
    }
    final String[] before = cursor.getColumnNames();
    mColumnNames = new String[before.length + 2];
    System.arraycopy(before, 0, mColumnNames, 0, before.length);
    mAuthorityIndex = before.length;
    mRootIdIndex = before.length + 1;
    mColumnNames[mAuthorityIndex] = COLUMN_AUTHORITY;
    mColumnNames[mRootIdIndex] = COLUMN_ROOT_ID;
}
 
开发者ID:gigabytedevelopers,项目名称:FireFiles,代码行数:25,代码来源:RootCursorWrapper.java

示例2: testUserTableCols

import android.database.Cursor; //导入方法依赖的package包/类
@Test
public void testUserTableCols() {
    Cursor c = db.query(DatabaseHelper.TABLE_USER, null, null, null, null, null, null);
    assertNotNull( c );

    String[] cols = c.getColumnNames();
    assertThat("Column not implemented: " + DatabaseHelper.COLUMN_ID,
            cols, hasItemInArray(DatabaseHelper.COLUMN_ID));

    assertThat("Column not implemented: " + DatabaseHelper.COLUMN_LOGIN,
            cols, hasItemInArray(DatabaseHelper.COLUMN_LOGIN));

    assertThat("Column not implemented: " + DatabaseHelper.COLUMN_AVATAR_URL,
            cols, hasItemInArray(DatabaseHelper.COLUMN_AVATAR_URL));

    assertThat("Column not implemented: " + DatabaseHelper.COLUMN_URL,
            cols, hasItemInArray(DatabaseHelper.COLUMN_URL));

    c.close();
}
 
开发者ID:lethalskillzz,项目名称:Andela-ALC-Challenge,代码行数:21,代码来源:DatabaseHelperTest.java

示例3: queryDatabaseData

import android.database.Cursor; //导入方法依赖的package包/类
/**
 * 查询指定数据库中指定表的数据.
 * 当前只能全部查询,后期可以进行优化
 *
 * @param context {@link Context}
 * @param name    database
 * @param table   table
 * @return list
 */
public static List<TablePackage> queryDatabaseData(Context context, String name, String table) {
    List<TablePackage> all = null;
    //判断数据库是否存在
    // TODO 重构查询方式.
    List<File> databaseList = queryDatabaseList(context);
    if (!isDatabaseExits(databaseList, name)) {
        return null;
    }
    // TODO 重构查询方式.
    //判断表是否存在
    List<String> tableList = queryTableByName(context, name);
    if (!isTableExits(tableList, table)) {
        return null;
    }
    SQLiteDatabase database = context.openOrCreateDatabase(name, Context.MODE_PRIVATE, null);
    if (database != null) {
        Cursor cursor = database.query(table, null, null, null, null, null, null);
        if (cursor != null) {
            String[] columnName = cursor.getColumnNames();
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                all = new ArrayList<>();
                do {
                    TablePackage tablePackage = new TablePackage();
                    for (int index = 0; index < columnName.length; index++) {
                        //TODO 后面可以修改为根据column的类型来获取相应的值,暂时测试么有问题
                        int columnIndex = cursor.getColumnIndex(columnName[index]);
                        tablePackage.put(columnName[index], columnIndex > -1 ? cursor.getString(columnIndex) : "");
                    }
                    all.add(tablePackage);
                } while (cursor.moveToNext());
            }
            cursor.close();
        }
        database.close();
    }
    return all;
}
 
开发者ID:facetome,项目名称:smart_plan,代码行数:48,代码来源:DatabaseQueryHelper.java

示例4: cursor2ObjectList

import android.database.Cursor; //导入方法依赖的package包/类
/**
 * 将游标转为对象集合 支持【int,long,double,String,date】
 * 
 * @param <T>
 * @param cursor
 * @param clazz
 * @return
 */
public static final <T> ArrayList<T> cursor2ObjectList(Cursor cursor,
		Class<T> clazz) {
	ArrayList<T> list = new ArrayList<T>();
	try {
		if (clazz != null && cursor != null && !cursor.isClosed()) {
			String[] columnNames = cursor.getColumnNames();
			int length = columnNames.length;
			Field[] fields = clazz.getDeclaredFields();
			while (cursor.moveToNext()) {
				T instance = clazz.newInstance();
				for (int index = 0; index < length; index++) {
					String columnName = columnNames[index];
					for (Field field : fields) {
						field.setAccessible(true);
						// 排除Exclude注解的字段
						if (field.getAnnotation(Exclude.class) != null) {
							continue;
						}
						if (field.getName().equals(columnName)) {
							setField(cursor, index, instance, field);
						}
					}

				}
				list.add(instance);
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return list;
}
 
开发者ID:PlutoArchitecture,项目名称:Pluto-Android,代码行数:41,代码来源:DaoUtils.java

示例5: showForm

import android.database.Cursor; //导入方法依赖的package包/类
@NonNull
private Page showForm(@NonNull String database, @NonNull String tableName, @Nullable String id) {
    try {
        boolean isEdit = CommonUtils.isNumber(id);
        SQLiteDatabase sqLiteDatabase = getContext().openOrCreateDatabase(database, Context.MODE_PRIVATE, null);
        Cursor cursor;
        if (isEdit) {
            cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + tableName + " WHERE " + COLUMN_ROW_ID + "=?", new String[]{id});
        } else {
            cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + tableName + " LIMIT 1", null);
        }
        Page page = new Page();
        page.addNavigationLink("?" + PARAMETER_DATABASE + "=" + database + "&" + PARAMETER_TABLE + "=" + tableName, "Back to items");
        if (cursor.moveToFirst()) {
            Form form = new Form();
            form.setAction("?" + PARAMETER_DATABASE + "=" + database + "&" + PARAMETER_TABLE + "=" + tableName);
            form.addHidden(PARAMETER_SAVE, id);
            String[] columnNames = cursor.getColumnNames();
            for (String columnName : columnNames) {
                String value = "";
                if (isEdit) {
                    value = cursor.getString(cursor.getColumnIndex(columnName));
                }
                form.addInputText(columnName, PARAMETER_FIELD + "[" + columnName + "]", value);
            }
            form.addSubmit("Save");
            page.setSingleContentPart(form);
        }
        cursor.close();
        return page;
    } catch (Exception e) {
        e.printStackTrace();
        return new ErrorPage("Can't show form: " + e.getMessage(), true);
    }
}
 
开发者ID:bartwell,项目名称:ultra-debugger,代码行数:36,代码来源:Module.java

示例6: testNullColumnConstraints

import android.database.Cursor; //导入方法依赖的package包/类
/**
>>>>>>> a6840f1... S07.03-Exercise-ConflictResolutionPolicy
     * Tests the columns with null values cannot be inserted into the database.
     */
    @Test
    public void testNullColumnConstraints() {
        /* Use a WeatherDbHelper to get access to a writable database */

        /* We need a cursor from a weather table query to access the column names */
        Cursor weatherTableCursor = database.query(
                REFLECTED_TABLE_NAME,
                /* We don't care about specifications, we just want the column names */
                null, null, null, null, null, null);

        /* Store the column names and close the cursor */
        String[] weatherTableColumnNames = weatherTableCursor.getColumnNames();
        weatherTableCursor.close();

        /* Obtain weather values from TestUtilities and make a copy to avoid altering singleton */
        ContentValues testValues = TestUtilities.createTestWeatherContentValues();
        /* Create a copy of the testValues to save as a reference point to restore values */
        ContentValues testValuesReferenceCopy = new ContentValues(testValues);

        for (String columnName : weatherTableColumnNames) {

            /* We don't need to verify the _ID column value is not null, the system does */
            if (columnName.equals(WeatherContract.WeatherEntry._ID)) continue;

            /* Set the value to null */
            testValues.putNull(columnName);

            /* Insert ContentValues into database and get a row ID back */
            long shouldFailRowId = database.insert(
                    REFLECTED_TABLE_NAME,
                    null,
                    testValues);

            String variableName = getConstantNameByStringValue(
                    WeatherContract.WeatherEntry.class,
                    columnName);

            /* If the insert fails, which it should in this case, database.insert returns -1 */
            String nullRowInsertShouldFail =
                    "Insert should have failed due to a null value for column: '" + columnName + "'"
                            + ", but didn't."
                            + "\n Check that you've added NOT NULL to " + variableName
                            + " in your create table statement in the WeatherEntry class."
                            + "\n Row ID: ";
            assertEquals(nullRowInsertShouldFail,
                    -1,
                    shouldFailRowId);

            /* "Restore" the original value in testValues */
            testValues.put(columnName, testValuesReferenceCopy.getAsDouble(columnName));
        }

        /* Close database */
        dbHelper.close();
    }
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:60,代码来源:TestSunshineDatabase.java

示例7: showItemsList

import android.database.Cursor; //导入方法依赖的package包/类
@NonNull
private Page showItemsList(String database, String tableName) {
    try {
        SQLiteDatabase sqLiteDatabase = getContext().openOrCreateDatabase(database, Context.MODE_PRIVATE, null);
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT " + COLUMN_ROW_ID + ",* FROM " + tableName, null);
        Table table = new Table();
        if (cursor.moveToFirst()) {
            int y = 0;
            do {
                if (y == 0) {
                    String[] columnNames = cursor.getColumnNames();
                    for (int x = 1; x < columnNames.length; x++) {
                        table.add(x - 1, 0, new RawContentPart(columnNames[x]));
                    }
                    y++;
                }
                int columnCount = cursor.getColumnCount();
                for (int x = 1; x < columnCount; x++) {
                    table.add(x - 1, y, new RawContentPart(cursor.getString(x)));
                }
                table.add(columnCount - 1, y,
                        new Link("?" + PARAMETER_DATABASE + "=" + database + "&" + PARAMETER_TABLE + "=" + tableName + "&" + PARAMETER_EDIT + "="
                                + cursor.getString(0), "Edit"));
                table.add(columnCount, y,
                        new Link("?" + PARAMETER_DATABASE + "=" + database + "&" + PARAMETER_TABLE + "=" + tableName + "&" + PARAMETER_DELETE + "="
                                + cursor.getString(0), "Delete"));
                y++;
            } while (cursor.moveToNext());
        }
        cursor.close();
        Page page = new Page();
        page.addNavigationLink("?", "Databases list");
        page.addNavigationLink("?" + PARAMETER_DATABASE + "=" + database, "Tables list");
        page.addNavigationLink("?" + PARAMETER_DATABASE + "=" + database + "&" + PARAMETER_TABLE + "=" + tableName + "&" + PARAMETER_EDIT + "=new", "Insert row");
        page.setSingleContentPart(table);
        return page;
    } catch (Exception e) {
        e.printStackTrace();
        return new ErrorPage("Can't read items: " + e.getMessage(), true);
    }
}
 
开发者ID:bartwell,项目名称:ultra-debugger,代码行数:42,代码来源:Module.java


注:本文中的android.database.Cursor.getColumnNames方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。