SQLite创建第二个表,找不到表

时间:2015-11-09 00:50:08

标签: android database sqlite

我正在尝试使用SQLite在Android Studio中的同一数据库中创建第二个表。但是,我不断得到一个表未找到错误,并认为它可能与错误的SQL代码有关。

我的数据库助手如下:

package com.example.katkin.stressguard;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.sql.SQLException;


public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "stressGuard.db";

private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "user_id";
private static final String COLUMN_USER = "username";
private static final String COLUMN_PASS = "password";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table users (user_id integer primary key not null , " +
        "username text not null , password text not null);";

private static final String TABLE_NAME_MOOD_RATINGS = "mood_ratings";
private static final String TABLE_CREATE_MOOD_RATINGS = "create table mood_ratings (mood_rating_id integer primary key not null , " +
        "mood_rating real not null , minutes_exercise integer not null , hours_slept integer not null , month_key integer not null , " +
        "FOREIGN KEY(user_id) REFERENCES users(user_id));";
private static final String COLUMN_MOOD_RATING = "mood_rating";
private static final String COLUMN_EXERCISE = "minutes_exercise";
private static final String MOOD_RATING_COLUMN_ID = "mood_rating_id";
private static final String COLUMN_HOURS_SLEPT = "hours_slept";
private static final String COLUMN_MONTH_KEY = "month_key";

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

public void insertUser(Users u) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from users";
    Cursor cursor = db.rawQuery(query , null);
    int count = cursor.getCount();

    values.put(COLUMN_ID , count);
    values.put(COLUMN_USER , u.getUsername());
    values.put(COLUMN_PASS , u.getPassword());

    db.insert(TABLE_NAME, null, values);
    db.close();
}

public String searchPass(String username) {

    db = this.getReadableDatabase();
    String query = "select username, password from " + TABLE_NAME;
    Cursor cursor = db.rawQuery(query , null);
    String uname, pword;
    pword ="not found";

    if(cursor.moveToFirst()) {
        do {
            uname = cursor.getString(0);


            if (uname.equals(username)) {
                pword = cursor.getString(1);
                break;
            }

        } while(cursor.moveToNext());
    }

    return pword;
}

public void insertMoodRating(MoodRatings moodRating) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from mood_ratings";
    Cursor cursor = db.rawQuery(query , null);
    int count = cursor.getCount();

    values.put(MOOD_RATING_COLUMN_ID , count);
    values.put(COLUMN_MOOD_RATING , moodRating.getMoodRating());
    values.put(COLUMN_HOURS_SLEPT , moodRating.getHoursSlept());
    values.put(COLUMN_MONTH_KEY , moodRating.getMonthKey());
    values.put(COLUMN_EXERCISE , moodRating.getMinutesExercise());


    db.insert(TABLE_NAME, null, values);
    db.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
    //create user table
    //create mood ratings table
    db.execSQL(TABLE_CREATE);
    try {
        db.execSQL(TABLE_CREATE_MOOD_RATINGS);
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.db = db;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(query);
    String q2 = "DROP TABLE IF EXISTS " + TABLE_NAME_MOOD_RATINGS;
    db.execSQL(q2);

    this.onCreate(db);
}
}

这是调用它的方法:

public void saveButtonClick(View v) {

NumberPicker monthSelector = (NumberPicker) findViewById(R.id.monthSelector);
int monthKey = monthSelector.getValue();

//get ratings from mood rating bar
float moodRating = moodRatingBar.getRating();
//get ratings from wellbeing rating bar

NumberPicker exerciseNumberPicker = (NumberPicker) findViewById(R.id.minutesExercisedNumberPicker);
int minutesExercised = exerciseNumberPicker.getValue();

NumberPicker numberPicker = (NumberPicker) findViewById(R.id.timeSleptNumberPicker);
int timeSlept = numberPicker.getValue();

//insert details in database
MoodRatings moodRecord = new MoodRatings();

moodRecord.setMoodRating(moodRating);
moodRecord.setHoursSlept(timeSlept);
moodRecord.setMinutesExercise(minutesExercised);
moodRecord.setMonthKey(monthKey);
//moodRecord.setPassword(passwordString);

helper.insertMoodRating(moodRecord);

}

这是db类:

public class MoodRatings {
private int entry_id;
private float mood_rating;
//private float wellbeingRating;
private int minutes_exercise;
private int hours_slept;
private int month_key;

public MoodRatings() {

}

public MoodRatings(int entry_id, float mood_rating, int minutes_exercise, int month_key, int hours_slept) {
    this.entry_id = entry_id;
    this.mood_rating = mood_rating;
    this.minutes_exercise = minutes_exercise;
    this.month_key = month_key;
    this.hours_slept = hours_slept;
}

public MoodRatings(float mood_rating, int minutes_exercise, int month_key, int hours_slept) {
    this.mood_rating = mood_rating;
    this.minutes_exercise = minutes_exercise;
    this.month_key = month_key;
    this.hours_slept = hours_slept;
}

public void setMoodRating(float mood_rating) {
    this.mood_rating = mood_rating;
}

public float getMoodRating() {
    return this.mood_rating;
}

public void setMonthKey(int month_key) {
    this.month_key = month_key;
}

public int getMonthKey() {
    return this.month_key;
}

public void setMinutesExercise(int minutes_exercise) {
    this.minutes_exercise = minutes_exercise;
}

public int getMinutesExercise() {
    return this.minutes_exercise;
}

public void setHoursSlept(int hours_slept) {
    this.hours_slept = hours_slept;
}

public int getHoursSlept() {
    return this.hours_slept;
}
}

这就是logcat,即使在包含try-catch块之后也似乎没有任何洞察力:

11-09 00:35:40.580    1878-1897/com.example.katkin.stressguard 

W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-09 00:35:40.580    1878-1897/com.example.katkin.stressguard W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f1874eff680, error=EGL_SUCCESS
11-09 00:35:48.870    1878-1897/com.example.katkin.stressguard W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-09 00:35:48.870    1878-1897/com.example.katkin.stressguard W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f18762f2b00, error=EGL_SUCCESS
11-09 00:35:53.720    1878-1878/com.example.katkin.stressguard I/Choreographer﹕ Skipped 294 frames!  The application may be doing too much work on its main thread.
11-09 00:36:04.960    1878-1897/com.example.katkin.stressguard W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-09 00:36:04.960    1878-1897/com.example.katkin.stressguard W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f1874eef440, error=EGL_SUCCESS
11-09 00:36:09.030    1878-1878/com.example.katkin.stressguard E/SQLiteLog﹕ (1) no such table: mood_ratings
11-09 00:36:09.030    1878-1878/com.example.katkin.stressguard D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
11-09 00:36:09.030    1878-1878/com.example.katkin.stressguard E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.katkin.stressguard, PID: 1878
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.database.sqlite.SQLiteException: no such table: mood_ratings (code 1): , while compiling: select * from mood_ratings
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
            at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
            at com.example.katkin.stressguard.DatabaseHelper.insertMoodRating(DatabaseHelper.java:87)
            at com.example.katkin.stressguard.MoodDiary.saveButtonClick(MoodDiary.java:106)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我是Android的新手,所以怀疑我错过了一些非常明显的东西。任何帮助,将不胜感激!第一个用户表工作并插入数据,第二个心情评级表没有。

1 个答案:

答案 0 :(得分:0)

如果您已将数据库添加到数据库中,则需要告诉android重建它。增加数据库版本,或手动删除它(可能通过清除应用程序数据)