无法在android中使用sqliteopenhelper类创建数据库

时间:2017-09-06 20:28:23

标签: android android-sqlite sqliteopenhelper

这是我的DataBase类的代码。我不明白为什么我无法创建表格文章。 错误消息说“没有这样的表:文章(code1):,同时编译:select * from articles”。 我完全不知道为什么会这样。请帮我解决这个问题。

 class DataBase extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "MyDB.db";

public static final String ARTICLES_TABLE_NAME = "articles";
public static final String ARTICLES_COLUMN_ID = "id";
private static final String ARTICLES_COLUMN_NAME = "name";
public static final String ARTICLES_COLUMN_PRICE = "price";

public static final String COSTUMER_TABLE_NAME = "costumers";
public static final String COSTUMER_COLUMN_ID = "id";
public static final String COSTUMER_COLUMN_NAME = "name";
public static final String COSTUMER_COLUMN_PHONE = "phone";

//private HashMap hp;

DataBase(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(
            "create table articles " +
                    "(id integer primary key, name text, price text)");

    db.execSQL(
            "create table costumers " +
                    "(id integer primary key, name text,phone text)");

}

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

boolean insertArticle(String name, String price) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("price", price);
    db.insert("articles", null, contentValues);
    return true;
}

ArrayList<String> getAllArticles() {
    ArrayList<String> array_list = new ArrayList<String>();

    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from articles", null );
    res.moveToFirst();

    while(!res.isAfterLast()){
        array_list.add(res.getString(res.getColumnIndex(ARTICLES_COLUMN_NAME)));
        res.moveToNext();
    }

    return array_list;
}

}

0 个答案:

没有答案