无法在SQLiteDataBase android中插入列

时间:2014-09-24 09:58:08

标签: android sqlite

我正在尝试在现有数据库中插入名为KEY_CATEGORY的新列,但是出现错误,我尝试按照一些答案但没有找到解决方案。请帮忙。提前完成。

这是SQLController类

SQLController:

public class SQLController extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "Test";
// Table Name
public static final String TABLE_TEST = "TestTable";
// Column Name
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_AGE = "age";
public static final String KEY_CATEGORY = "category";

public SQLController(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Create Table
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS "
            + TABLE_TEST + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT," + KEY_AGE + " TEXT" + KEY_CATEGORY
            + "TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("ALTER TABLE " + TABLE_TEST + " ADD COLUMN " + KEY_CATEGORY);
    if (newVersion > oldVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEST);

    }
    onCreate(db);
}

// Insert Value
public void adddata(Context context, String name, String age, int cat) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name);
    values.put(KEY_AGE, age);
    values.put(KEY_CATEGORY, cat);
    db.insert(TABLE_TEST, null, values);
    db.close();
}

// check complete database
public String Exist(String user) {
    String username = "";
    SQLiteDatabase db = this.getReadableDatabase();

    try {
        Cursor c = db.query(TABLE_TEST, null, KEY_NAME + "=?",
                new String[] { String.valueOf(user) }, null, null, null);

        if (c == null) {
            return username;
        } else {
            c.moveToFirst();
            username = c.getString(c.getColumnIndex(KEY_NAME));
        }
    }

    catch (Exception e) {
        e.printStackTrace();
    }
    db.close();
    return username;
}

// Get Row Count
public int getCount() {
    String countQuery = "SELECT  * FROM " + TABLE_TEST;
    int count = 0;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    if (cursor != null && !cursor.isClosed()) {
        count = cursor.getCount();
        cursor.close();
    }
    return count;
}

// Delete Query
public void removeFav(int id) {

    String countQuery = "DELETE FROM " + TABLE_TEST + " where " + KEY_ID
            + "= " + id;
    SQLiteDatabase db = this.getReadableDatabase();
    db.execSQL(countQuery);
}

// Get FavList
public List<DataSource> getFavList() {
    String selectQuery = "SELECT  * FROM " + TABLE_TEST;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    List<DataSource> FavList = new ArrayList<DataSource>();
    if (cursor.moveToFirst()) {
        do {
            DataSource list = new DataSource();
            list.setId(Integer.parseInt(cursor.getString(0)));
            list.setName(cursor.getString(1));
            list.setAge(cursor.getString(2));
            // list.setCateg(cursor.getString(3));
            FavList.add(list);

        } while (cursor.moveToNext());
    }
    db.close();
    return FavList;
}

logcat的某些部分是

logcat的:

 09-24 14:40:52.785: E/SQLiteDatabase(25039): android.database.sqlite.SQLiteException: table TestTable has no column named category: , while compiling: INSERT INTO TestTable(category,age,name) VALUES (?,?,?)
09-24 14:40:52.785: E/SQLiteDatabase(25039):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-24 14:40:52.785: E/SQLiteDatabase(25039):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
09-24 14:40:52.785: E/SQLiteDatabase(25039):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:150)

3 个答案:

答案 0 :(得分:2)

您在上一个列规范之后错过了,,并在此处列出了列名称和类型之间的空格:

" TEXT" + KEY_CATEGORY
+ "TEXT" + ")";

更改为

" TEXT," + KEY_CATEGORY
+ " TEXT" + ")";

修复SQL后,卸载您的应用程序,以便再次运行sqlite帮助程序onCreate()

答案 1 :(得分:0)

   //write to this on create method
    String CREATE_CONTACTS_TABLE="create table "
                      + TABLE_TEST + "(" + KEY_ID
                      + " integer primary key , " + KEY_NAME
                      + " text ,"+ KEY_AGE+" text,"+KEY_CATEGORY+" text);";

db.execSQL(CREATE_CONTACTS_TABLE);

答案 2 :(得分:0)

试试这个:

     String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS "
        + TABLE_TEST + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
        + KEY_NAME + " TEXT," + KEY_AGE + " TEXT, " + KEY_CATEGORY
        + " TEXT " + ")";

      db.execSQL(CREATE_CONTACTS_TABLE);