SQLite数据库不会创建列

时间:2014-10-21 19:31:15

标签: android sqlite

我想创建几个列,但SQLite确实只创建了两列。 我正在检查这样的列:

    String[] str= cursor.getColumnNames();
    for(int i=0; i<str.length; i++)
        Toast.makeText(this, str[i], Toast.LENGTH_SHORT).show();

它始终只有COUNTNAME和UID。 (是的,我正在更新DATABASE_VERSION)

这是我的代码:

public class DatabaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME ="CountsDatabase";
public static final String TABLE_NAME="COUNTSTABLE";
private static final int DATABASE_VERSION = 21;
public static final String UID = "_id";
public static final String COUNTNAME = "CountName1";
public static final String COUNTNAME2 = "CountName2";
public static final String AKTUELLERWERT = "aktuellerWert";
private static final String CREATE_TABLE ="CREATE TABLE " + TABLE_NAME + " ("+ UID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COUNTNAME +" TEXT,"+ COUNTNAME2 +" TEXT," + AKTUELLERWERT + " INTEGER" + ");";
private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
Context context;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context=context;
    Toast.makeText(context, "Constructor Database", Toast.LENGTH_SHORT).show();
}

@Override
public void onCreate(SQLiteDatabase db) {
    Toast.makeText(context, "onCreate Database", Toast.LENGTH_SHORT).show();
    Toast.makeText(context, CREATE_TABLE, Toast.LENGTH_LONG).show();
    try{

        db.execSQL(CREATE_TABLE);
    } catch(SQLException e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Toast.makeText(context, "Upgrade Database", Toast.LENGTH_SHORT).show();
    try{
        db.execSQL(DROP_TABLE);
        onCreate(db);
    } catch(SQLException e){
        e.printStackTrace();
    }

}
}

1 个答案:

答案 0 :(得分:0)

阿尔托,你是对的。我从没有所有值的ListView创建了光标。

    Cursor cursor = (Cursor)parent.getItemAtPosition(position);

    String[] str= cursor.getColumnNames();
    for(int i=0; i<str.length; i++)
                Toast.makeText(this, str[i], Toast.LENGTH_SHORT).show();

解决方案是:

    String[] columns= {DatabaseHelper.UID, DatabaseHelper.COUNTNAME, DatabaseHelper.AKTUELLERWERT};
    Cursor cursor = sqlDatabase.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
    String[] str= cursor.getColumnNames();
    for(int i=0; i<str.length; i++)
        Toast.makeText(this, str[i], Toast.LENGTH_SHORT).show();