sqlite数据库没有列

时间:2015-02-15 19:14:54

标签: database sqlite android-sqlite

我试图建立一个sqlite数据库,那里有我的代码

public class SQliteHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "moneyDB";
    public static final String TABLE_MONEY = "money", KEY_DATE = "date", KEY_VALUE = "value", KEY_WHY = "why";
    public static final String[] COLUMNS = {KEY_DATE,KEY_VALUE, KEY_WHY};

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

    @Override
    public void onCreate(SQLiteDatabase db) {
      String CREATE_MONEY_TABLE =  "CREATE TABLE" + TABLE_MONEY + "("
        + KEY_DATE +"TEXT PRIMARY KEY,"  +
       KEY_VALUE + "TEXT," +
        KEY_WHY +"TEXT, )";
        db.execSQL(CREATE_MONEY_TABLE);
    }

这是我的添加方法

public void addDate(money money) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_DATE, money.getDate());
        contentValues.put(KEY_VALUE, money.getMoney());
        contentValues.put(KEY_WHY, money.getWhy());
        db.insert(TABLE_MONEY,null,contentValues);
        db.close();
    }

每次我运行应用程序时都会收到错误

02-15 21:02:22.242  27324-27324/com.main.project.money E/SQLiteLog﹕ (1) table money has no column named date
02-15 21:02:22.252  27324-27324/com.main.project.money E/SQLiteDatabase﹕ Error inserting date=15/02/2015 why=gg value=55
    android.database.sqlite.SQLiteException: table money has no column named date (code 1): , while compiling: INSERT INTO money(date,why,value) VALUES (?,?,?)

1 个答案:

答案 0 :(得分:0)

您错过了字段名称和字段类型之间的一些空格:
以及CREATE TABLE语句和表名之间 你还有一个额外的结尾逗号(??)。

@Override
public void onCreate(SQLiteDatabase db){
    String CREATE_MONEY_TABLE =  "CREATE TABLE" + TABLE_MONEY + "("
    + KEY_DATE +"TEXT PRIMARY KEY,"  +
     KEY_VALUE + "TEXT," +
     KEY_WHY +"TEXT, )";
     db.execSQL(CREATE_MONEY_TABLE);
}

应该是:

@Override
public void onCreate(SQLiteDatabase db)
{
    String CREATE_MONEY_TABLE =  "CREATE TABLE " + TABLE_MONEY + " (" +
    KEY_DATE + " TEXT PRIMARY KEY, "  +
    KEY_VALUE + " TEXT, " +
    KEY_WHY + " TEXT)";
    db.execSQL(CREATE_MONEY_TABLE);
}