初学者Java android studio错误

时间:2018-02-04 16:19:05

标签: java android sql database sqlite

我刚刚开始学习java / android studio,我正在按照youtube教程来构建一个SQLite数据库。我收到了一些我无法解决的错误。

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final string DATABASE_NAME = "skinApp.db";
    public static final string TABLE_NAME = "products_table";
    public static final String COLUMN_TASKID = "_id";
    public static final String COLUMN_PHOTO = "Photo";
    public static final String COLUMN_NAME = "Product name";
    public static final String COLUMN_DETAILS = "Details";
    public static final String COLUMN_PRICE = "Price";
    public static final String COLUMN_BRAND = "Brand";
    public static final String COLUMN_TAGS = "Tags";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, PHOTO BLOB, PRODUCTNAME TEXT, DETAILS TEXT, PRICE DECIMAL, BRAND TEXT, TAGS TEXT");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

在最后两个@override上,我的错误是陈述

  

'方法不能从其超类'

中获取ovverride方法

并在super之后的括号中的文本(我的错误说:

  

Object()中的对象不能应用于... [所有参数陈述]

这些错误意味着什么,我该如何解决?

3 个答案:

答案 0 :(得分:1)

像这样创建表,你的DataBaseHelper类还有另一个错误,你没有初始化那个数据是TEXT还是VARCHAR:

 String createTable = " CREATE TABLE "+ Constants.TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, "+Constants.NAME+" TEXT, "+Constants.SKU+" TEXT ,"+Constants.UPC+" TEXT,"+Constants.ASSOC_UPC+" TEXT,"+Constants.PRICE+" TEXT, "+Constants.DISPLAY_SIZE+" TEXT, "+Constants.DISPLAY_SIZE_YES+" INTEGER , "+Constants.STATUS+" TEXT)";
        db.execSQL(createTable);

这是我的DataBaseHelper类尝试理解它,我在创建两个表:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "DatabaseHelper";
    private static final String DATABASE_NAME = "GiftsDatabase.db";
    SQLiteDatabase db;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME,null,Constants.DATABASE_VERSION);
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        try{
            String createTable = " CREATE TABLE "+ Constants.TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, "+Constants.NAME+" TEXT, "+Constants.SKU+" TEXT ,"+Constants.UPC+" TEXT,"+Constants.ASSOC_UPC+" TEXT,"+Constants.PRICE+" TEXT, "+Constants.DISPLAY_SIZE+" TEXT, "+Constants.DISPLAY_SIZE_YES+" INTEGER , "+Constants.STATUS+" TEXT)";
            db.execSQL(createTable);
            String createTableCount = " CREATE TABLE "+ Constants.TABLE_NAME_COUNT +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, "+Constants.SKU_COUNT+" TEXT ,"+Constants.QUANTITY_COUNT+" TEXT)";
            db.execSQL(createTableCount);

            Log.e(TAG,"Created Database");
        }catch (Exception e){
            Log.e("EXCEPTION",""+e);
        }

    }

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

    public boolean addData(String name , String sku, String upc ,String assocUpc, String price, String displaySize, int displaySizeYes, String status){
        db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Constants.NAME, name );
        contentValues.put(Constants.SKU, sku );
        contentValues.put(Constants.UPC, upc );
        contentValues.put(Constants.ASSOC_UPC, assocUpc );
        contentValues.put(Constants.PRICE, price );
        contentValues.put(Constants.DISPLAY_SIZE, displaySize );
        contentValues.put(Constants.DISPLAY_SIZE_YES, displaySizeYes );
        contentValues.put(Constants.STATUS, status );

        long result = db.insert(Constants.TABLE_NAME,null,contentValues);
        Log.e(TAG,""+upc+"  Inserted");
        Log.e(TAG,""+assocUpc+"  Inserted");

        if(result == -1) {
            return false;
        }else{
          //  Log.e(TAG,"value inserted");
            return true;
        }

    }

    public boolean addDataCount(String skuCount,String quantityCount ){
        db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
       // contentValues.put(Constants.UPC_COUNT,upcCount);
        contentValues.put(Constants.SKU_COUNT,skuCount);
        contentValues.put(Constants.QUANTITY_COUNT,quantityCount);

        long result = db.insert(Constants.TABLE_NAME_COUNT,null,contentValues);
        Log.e(TAG,""+skuCount+"  Inserted");

        if(result == -1) {
            return false;
        }else{
            Log.e(TAG,"value inserted");
            return true;
        }


    }

    public  boolean deleteTable(){
        db = this.getWritableDatabase();
        db.execSQL("delete from "+ Constants.TABLE_NAME);
        Log.e("DELETE_TABLE","DELETE_TABLE");
        return true;

    }
    public  boolean deleteTableCount(){
        db = this.getWritableDatabase();
        db.execSQL("delete from "+ Constants.TABLE_NAME_COUNT);
        Log.e("DELETE_TABLE_COUNT","DELETE_TABLE_COUNT");
        return true;

    }


}

答案 1 :(得分:1)

尝试没有" SQLiteDatabase db = this.getWritableDatabase();"像这样:

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}

答案 2 :(得分:0)

  

'方法不能从其超类'

中获取ovverride方法

您错过了在,SQLiteDatabase db

之间添加int oldVersion

这应解决您的问题

public void onUpgrade(SQLiteDatabase db , int oldVersion, int newVersion)