简体   繁体   中英

Can't edit SQLite database name

I can't edit my database name and I can't find the list of information I put into the database.

If I try to change the database name from "diet" to something else, I'm still able to run but there's the green underline typo. I'm following this tutorial.

public class DietDbHelper extends SQLiteOpenHelper {
    public static final String TABLE_NAME = "entry";
    public static final String COLUMN_NAME_ID = "_id";
    public static final String COLUMN_NAME_TIMESTAMP = "timestamp";
    public static final String COLUMN_NAME_WEIGHT = "weight";

    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_NAME_ID + " INTEGER PRIMARY KEY," +
                    COLUMN_NAME_TIMESTAMP + " INTEGER," +
                    COLUMN_NAME_WEIGHT + " INTEGER )";

    private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;

    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 2;
    public static final String DATABASE_NAME = "diet";

    public DietDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}

I would change your database name code from public static final String DATABASE_NAME = "diet";

to public static final String DATABASE_NAME = "diet.db"; All of my sqlite column names, table and database names are highlighted in green.

you have to change database version for each change that you make in your database structure. for example change :

public static final int DATABASE_VERSION = 2;

to

public static final int DATABASE_VERSION = 3;

and so on for every change. I hope it will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM