将数据插入数据库"表产品没有名为accFor(代码1)"

时间:2016-05-11 11:17:13

标签: java android sqlite

好的,所以我正在尝试创建一个应用程序,它从我的界面获取两个值,一个来自文本框,另一个来自微调器,然后将它们插入数据库并在插入字段下方显示它们。

插入accFor = Personal productname = Ayy时出错                                                                 android.database.sqlite.SQLiteException:表产品没有名为accFor(code 1)的列:,编译时:INSERT INTO产品(accFor,productname)VALUES(?,?)

^^这是我的确切错误代码

package com.kai.todolist;


    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.database.Cursor;
    import android.content.Context;
    import android.content.ContentValues;

public class MyDBManager extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TODO = "productname";
public static final String COLUMN_FOR = "accFor";

//We need to pass database information along to superclass
public MyDBManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_TODO + " TEXT, " +
            COLUMN_FOR + "TEXT " +
            ");";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
    onCreate(db);
}

//Add a new row to the database
public void addProduct(Product product){
    ContentValues values = new ContentValues();
    values.put(COLUMN_TODO, product.getToDo());
    values.put(COLUMN_FOR, product.getAccFor());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_PRODUCTS, null, values);
    db.close();
}

//Delete a product from the database
public void deleteProduct(String productName){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_TODO + "=\"" + productName + "\";");
}

public String databaseToString(){
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

    //Cursor points to a location in your results
    Cursor c = db.rawQuery(query, null);
    //Move to the first row in your results
    c.moveToFirst();

    //Position after the last row means the end of the results
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("productname")) != null) {
            dbString += c.getString(c.getColumnIndex("productname"));
            dbString += "\n";
        }
        c.moveToNext();
    }

    db.close();
    return dbString;
}

}

这是我的数据库处理程序代码,我假设问题是我不知道从哪里开始这个问题所以任何帮助都将不胜感激。我的代码中的其他类文件是Products,它构建构造函数和setter以及getter和main活动。

1 个答案:

答案 0 :(得分:0)

COLUMN_FOR + "TEXT " +

列名称及其类型之间需要一个空格。

添加后,您可以卸载应用程序以重新创建数据库。