當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。