根据android studio编译器,数据库表不存在 [英] Database table doesn't exist according to android studio compiler

查看:102
本文介绍了根据android studio编译器,数据库表不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:


android.database.sqlite.SQLiteException:没有这样的表:设置(代码1): ,同时编译:SELECT * FROM setting

android.database.sqlite.SQLiteException: no such table: setting (code 1): , while compiling: SELECT * FROM setting

但我在DatabaseHandler文件中创建了表:

Yet I have created table in my DatabaseHandler file:

public class DatabaseHandler extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "database.db";

    //table name
    private static final String TABLE_DETAILS = "details";
    private static final String TABLE_FOOD = "food";
    private static final String TABLE_OLDDETAILS = "oldDetails";
    private static final String TABLE_SETTING = "setting";

    //Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_HEIGHT = "height";
    private static final String KEY_WEIGHT = "weight";
    private static final String KEY_CALORIES = "calories";
    private static final String KEY_DATE = "date";
    private static final String KEY_LEVEL = "level";
    private static final String KEY_DURATION = "duration";
    private static final String KEY_DAYS = "days";


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

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_DETAILS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_DETAILS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," +  KEY_HEIGHT + " REAL," + KEY_WEIGHT + " REAL " + ")";
        String CREATE_FOOD_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_FOOD + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_CALORIES + " INTEGER " + ")";
        String CREATE_OLDDETAILS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_OLDDETAILS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT," +  KEY_HEIGHT + " REAL," + KEY_WEIGHT + " REAL " + ")";
        String CREATE_SETTING_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_SETTING + "("
                + KEY_ID + " INTEGER PRIMARY KEY," +  KEY_LEVEL + " INTEGER," + KEY_DURATION + " INTEGER," + KEY_DAYS + " INTEGER " + ")";

        db.execSQL(CREATE_OLDDETAILS_TABLE);
        db.execSQL(CREATE_DETAILS_TABLE);
        db.execSQL(CREATE_FOOD_TABLE);
        db.execSQL(CREATE_SETTING_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_OLDDETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOOD);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTING);

        // Create tables again
        onCreate(db);
    }
 public boolean addSetting(int level, int duration, int days) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, 1);
        values.put(KEY_LEVEL, level);
        values.put(KEY_DURATION, duration);
        values.put(KEY_DAYS, days);

        // Inserting Row
        long result = db.insert(TABLE_SETTING, null, values);
        if(result == -1){
            return false;
        }
        else{
            return true;
        }
    }
public boolean checkSetting(){
        SQLiteDatabase db = this.getWritableDatabase();
        String selectQuery = "SELECT * FROM " + TABLE_SETTING;
        Cursor cursor = db.rawQuery(selectQuery, null);
        Boolean rowExists;

        if (cursor.moveToFirst())
        {
            // DO SOMETHING WITH CURSOR
            rowExists = true;

        } else
        {
            // I AM EMPTY
            rowExists = false;
        }
        return rowExists;
    }
public setting getSetting() {
        String selectQuery = "SELECT * FROM " + TABLE_SETTING;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor != null)
            cursor.moveToFirst();
        setting set = new setting(cursor.getInt(1), cursor.getInt(2), cursor.getInt(3));

        return set;
    }
public int updateSetting(setting set) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_LEVEL, set.getLevel());
        values.put(KEY_DURATION, set.getDuration());
        values.put(KEY_DAYS, set.getDays());
        Log.d("UPDATE: ", "updated all");

        // updating row
        return db.update(TABLE_SETTING, values, KEY_ID + " = ?", new String[] { String.valueOf(1) });
    }

如你所见,有CREATE IF NOT EXISTS的方法和我我试图创建它,虽然错误正在重复。

As you can see, there is "CREATE IF NOT EXISTS" method and I'm trying to create it, although the error is repeating itselfs.

我正在尝试在片段中执行此数据库代码,您可以看到这里到目前为止似乎没有问题,我认为它主要是DatabaseHandler。

I'm trying to execute this database code in a fragment, which you can see here and so far it seems not to be the problem, I think it mainly the DatabaseHandler.

如何正确创建表 SETTING 以便我可以从中进行选择?

How can I create the table SETTING properly so I will be able to select from it?

推荐答案

您已经安装了应用程序但是已经更改了数据库结构,但 onCreate 只会执行一次,因此您有两个选项可以您的更改会反映到数据库中

You have changed the database structure while app is already installed but onCreate will only be executed once so you have two option to make your changes reflect into database

1。)将数据库版本增加1以执行 onUpgrade 以将更改应用于现有数据库数据库

1.) Increment the database version by one to execute onUpgrade to apply changes into existing database

2。)手动从手机中卸载应用程序以执行 onCrea te

2.) Manually uninstall the app from your phone to execute onCreate

这篇关于根据android studio编译器,数据库表不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆