Android Studio中没有此类列错误

时间:2016-11-04 00:00:14

标签: java android sqlite android-studio

我是一名新手/学生,正在开发一个显示待办事项列表的应用项目。我一直在使用警报对话框来获取用户的输入并将后者存储在表中。当我扩展到2个对话框以获取有关任务和主题的信息时,发生以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.zilunlin.bacpack, PID: 8118
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zilunlin.bacpack/com.example.zilunlin.bacpack.MainActivity}: android.database.sqlite.SQLiteException: no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks
              #################################################################
              Error Code : 1 (SQLITE_ERROR)
              Caused By : SQL(query) error or missing database.
                (no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks)
              #################################################################
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7229)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: android.database.sqlite.SQLiteException: no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks
              #################################################################
              Error Code : 1 (SQLITE_ERROR)
              Caused By : SQL(query) error or missing database.
                (no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks)
              #################################################################
                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
                  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                  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:1454)
                  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1301)
                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1172)
                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1340)
                  at com.example.zilunlin.bacpack.MainActivity.updateUI(MainActivity.java:128)
                  at com.example.zilunlin.bacpack.MainActivity.onCreate(MainActivity.java:36)
                  at android.app.Activity.performCreate(Activity.java:6876)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                    ... 9 more

请注意,此错误不会直接检测到,只有在我运行调试时才会显现。

以下是我的MainActivity代码,特别是对话框

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add_task:
            final EditText taskEditText = new EditText(this);
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("Add a new task")
                    .setMessage("What is it that you gotta do?")
                    .setView(taskEditText)
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String task = String.valueOf(taskEditText.getText());
                            SQLiteDatabase db = mHelper.getWritableDatabase();
                            ContentValues values = new ContentValues();
                            values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
                            db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
                                    null,
                                    values,
                                    SQLiteDatabase.CONFLICT_REPLACE);
                            db.close();
                            updateUI();
                        }
                    })
                    .setNegativeButton("Cancel", null)
                    .create();
            dialog.show();
            final EditText SubjectText = new EditText(this);
            AlertDialog setSubject = new AlertDialog.Builder(this)
                    .setTitle("Add its subject")
                    .setMessage("What is its subject?")
                    .setView(SubjectText)
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface setSubject, int which) {
                            String subject = String.valueOf(SubjectText.getText());
                            SQLiteDatabase db = mHelper.getWritableDatabase();
                            ContentValues subjectvalues = new ContentValues();
                            subjectvalues.put(TaskContract.TaskEntry.SUBJECT_NAME, subject);
                            db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
                                    null,
                                    subjectvalues,
                                    SQLiteDatabase.CONFLICT_REPLACE);
                            db.close();
                            updateUI();
                        }

                    })
                    .setNegativeButton("Cancel", null)
                    .create();
            setSubject.show();




            return true;

        default:
            return super.onOptionsItemSelected(item);
    }}

这是db合约

import android.provider.BaseColumns;

public class TaskContract {
    public static final String DB_NAME = "com.zilunlin.todolist.db";
    public static final int DB_VERSION = 1;

    public class TaskEntry implements BaseColumns {
        public static final String TABLE = "tasks";
        public static final String COL_TASK_TITLE = "title";
        public static final String SUBJECT_NAME = "subject";
    }
}

最后,这是db helper

  public class TaskDbHelper extends SQLiteOpenHelper {

    public TaskDbHelper(Context context) {
        super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
                TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL);" +
                TaskContract.TaskEntry.SUBJECT_NAME + " TEXT NOT NULL);";

        db.execSQL(createTable);
    }

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

我理解这个问题可能会重复,但鉴于我目前对编程的理解,我无法充分转移答案。任何回复都将不胜感激。

PS:我关注了Aldo Ziflaj关于待办事项列表的有用教程。

1 个答案:

答案 0 :(得分:0)

你添加了额外的分号......

更改了此

String createTable =“CREATE TABLE IF NOT EXISTS”+ TaskContract.TaskEntry.TABLE +“(”+                 TaskContract.TaskEntry._ID +“INTEGER PRIMARY KEY AUTOINCREMENT”,+                 TaskContract.TaskEntry.COL_TASK_TITLE +“TEXT NOT NULL,”+                 TaskContract.TaskEntry.SUBJECT_NAME +“TEXT NOT NULL”“;