将临时值插入表格中的所有列时,它似乎正常工作。请参阅以下方法并在下面记录。但是,当我测试列是否存在下面的测试方法时,它会返回false,表示该列存在!!
另外你可以在日志中看到getType为除了name列之外的列返回0 ?? !!出于这个原因,我在底部添加了表创建方法(是的,我更新了版本号)
请注意,此问题已根据以下评论进行了编辑(因此前几条评论可能没有意义)。
第一段代码来自下面两种方法的名称
ContentValues values = setContVal_All_Columns(myTable);
//create row
long insertId = database.insert(myTable, null, values);
System.out.println("Column date exists = " + String.valueOf(doesColumnExist(myTable,"date")));
setContVal_All_Columns方法:
//sets generic content values to initialize row = excluding ID column
private ContentValues setContVal_All_Columns(String myTable) {
ContentValues contentValues = new ContentValues();
Cursor cursor = database.query(myTable,null,null,null,null,null,null);
String[] columnNames = cursor.getColumnNames();
cursor.moveToFirst();
for(String name : columnNames) {
if(!name.equals(IdColumn)) { //excludes Id column
int index = cursor.getColumnIndex(name);
System.out.println("Column name = " + name + " index = " + String.valueOf(index));
System.out.println("Type = " + String.valueOf(cursor.getType(index))); //appears to crash on the getType
if (cursor.getType(index) == 3) { //String
System.out.println("Column is string");
contentValues.put(name, " ");
} else if (cursor.getType(index) == 1) { //integer
contentValues.put(name, 0);
}
}
}
return contentValues;
}
日志信息:
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column name = date index = 1
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Type = 0
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column name = name index = 2
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Type = 3
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column is string
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column name = altitude index = 3
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Type = 0
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column name = is_used index = 4
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Type = 0
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column name = warning index = 5
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Type = 0
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column name = action index = 6
02-16 19:58:42.296 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Type = 0
02-16 19:58:42.316 9711-9711/com.mycompany.dudesmyreminders I/System.out﹕ Column date exists = false
编辑:测试栏目列表的方法:总是在日期栏上返回-1 ...
private boolean doesColumnExist(String myTable, String myColumn) {
boolean doesExist = true;
Cursor cursor = database.rawQuery("PRAGMA table_info(" + myTable + ")",null);
cursor.moveToFirst();
int value = cursor.getColumnIndex(myColumn);
if(value == -1)
{
doesExist = false;
}
return doesExist;
}
编辑:表格创建方法:
//Database creation sql statement
private static final String SQL_CREATE_SPECIAL_DAYS =
"CREATE TABLE " + dbFields.TABLE_NAME_SPECIAL_DAYS + " (" +
dbFields.COLUMN_SPECIAL_DAYS_ID + INTEGER_PRIMARY_KEY + COMMA_SEP +
dbFields.COLUMN_SPECIAL_DAYS_DATE + TEXT_TYPE + COMMA_SEP +
dbFields.COLUMN_SPECIAL_DAYS_NAME + TEXT_TYPE + COMMA_SEP +
dbFields.COLUMN_SPECIAL_DAYS_ALTITUDE + INTEGER_TYPE + COMMA_SEP +
dbFields.COLUMN_SPECIAL_DAYS_USED + INTEGER_TYPE + COMMA_SEP +
dbFields.COLUMN_SPECIAL_DAYS_WARNING + INTEGER_TYPE + COMMA_SEP +
dbFields.COLUMN_SPECIAL_DAYS_ACTION + INTEGER_TYPE +
// Any other options for the CREATE command
" )";
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(SQL_CREATE_SPECIAL_DAYS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + dbFields.TABLE_NAME_SPECIAL_DAYS);
onCreate(db);
}
答案 0 :(得分:2)
查看原始代码块中的代码...
Cursor cursor = database.query(myTable,null,null,null,null,null,null);
String[] columnNames = cursor.getColumnNames();
...执行任何游标查询时,游标'index'(即它指向的数据集行)始终为-1。这是之前第一行的索引为0(有原因)。但是,此时调用getColumNames()
是合法的,因为列名在查询过程中会被单独编目,并且在游标的生命周期内保持不变。
但是,调用getType()
确实需要光标“指向”(引用)数据集中的有效行。文档并不完全清楚,但推断这是一个要求......
public abstract int getType(int columnIndex)在API级别11中添加
返回给定列值的数据类型。 返回列的首选类型,但数据可能会转换为get类型方法中记录的其他类型,如getInt(int),getFloat(int)等。
换句话说,除非光标引用数据集的有效行,否则无法获得首选类型。只需将光标移动到第一行(假设它在数据集中有有效的数据行),就可以获得每列中实际数据的首选类型。
所以基本上只需在使用moveToFirst()
...
getType()
Cursor cursor = database.query(myTable,null,null,null,null,null,null);
String[] columnNames = cursor.getColumnNames();
if (cursor.getCount() > 0)
cursor.moveToFirst(); // Calls to cursor.getType() will work from now on