SQLite告诉我一个列不存在

时间:2016-06-22 16:18:17

标签: android sqlite

我正在编写一个可以存储保险卡的应用程序,用户可以在其中输入信息并将其存储在数据库中。应用程序启动时会显示以前的事件列表,这就是问题发生的地方。它告诉我那列" driverone"不存在。

我对编程很新,而且对SQLite来说很新,所以我们将非常感谢所有建议。我知道这个话题已经出现在这里,我搜索了答案,仍然无法弄清楚发生了什么。

以下是我定义数据库的代码:

public class DbSchema {
    public static final class CrashTable {
        public static final String NAME = "crash";

        public static final class Cols {
            public static final String UUID = "uuid";
            //TODO: "column 'driverone' does not exist...fix it
            public static final String DRIVER_ONE = "driverone";
            public static final String INSCO = "insco";
            public static final String POLICY = "policy";
            public static final String PHONE = "phone";
            public static final String EMAIL = "email";

            //public static final String DRIVER_TWO = "driver_two";
            //public static final String INSURANCECOMPANY_TWO = "insurancecompany_two";
            //public static final String POLICYNUMBER_TWO = "policynumber_two";
            //public static final String TELEPHONENUMBER_TWO = "telephonenumber_two";
            //public static final String EMAILADDRESS_TWO = "emailaddress_two";

            //public static final String DATE = "date";

        }
    }
}

我在这里创建数据库:

private static final String CRASH_TABLE_CREATE_STATEMENT = "create table " + CrashTable.NAME + "(" +
        " _id integer primary key autoincrement, " +
        CrashTable.Cols.UUID + ", " +
        //CrashTable.Cols.ID + " integer primary key autoincrement, " +
        CrashTable.Cols.DRIVER_ONE + ", " +
        CrashTable.Cols.INSCO + ", " +
        CrashTable.Cols.POLICY + ", " +
        CrashTable.Cols.PHONE + ", " +
        CrashTable.Cols.EMAIL + ", " +
        ");";

这就是错误发生的地方:

public class CrashCursorWrapper extends CursorWrapper{
    public CrashCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    public Crash getCrash() {
        String uuidString = getString(getColumnIndexOrThrow(CrashTable.Cols.UUID));
        String driverone = getString(getColumnIndexOrThrow(CrashTable.Cols.DRIVER_ONE));  <------ Error here
        String insco = getString(getColumnIndexOrThrow(CrashTable.Cols.INSCO));
        String policy = getString(getColumnIndexOrThrow(CrashTable.Cols.POLICY));
        String phone = getString(getColumnIndexOrThrow(CrashTable.Cols.PHONE));
        String email = getString(getColumnIndexOrThrow(CrashTable.Cols.EMAIL));

        //String theirName = getString(getColumnIndex(CrashTable.Cols.DRIVER_TWO));
        //String theirInsCo = getString(getColumnIndex(CrashTable.Cols.INSURANCECOMPANY_TWO));
        //String theirPolicy = getString(getColumnIndex(CrashTable.Cols.POLICYNUMBER_TWO));
        //String theirTelephone = getString(getColumnIndex(CrashTable.Cols.TELEPHONENUMBER_TWO));
        //String theirEmail = getString(getColumnIndex(CrashTable.Cols.EMAILADDRESS_TWO));
        //long date = getLong(getColumnIndex(CrashTable.Cols.DATE));

        Crash crash = new Crash(UUID.fromString(uuidString));
        crash.setYourName(driverone);
        crash.setInsCo(insco);
        crash.setPolicy(policy);
        crash.setTelephone(phone);
        crash.setEmail(email);

        //crash.setTheirName(theirName);
        //crash.setInsCo_Two(theirInsCo);
        //crash.setPolicy_Two(theirPolicy);
        //crash.setTelephone_Two(theirTelephone);
        //crash.setEmail_Two(theirEmail);

       // crash.setDate(new Date(date));

        return crash;
    }
}

这是我的日志,供参考:

06-22 10:59:49.645 31983-31983/com.bignerdranch.android.crashcourse E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.bignerdranch.android.crashcourse, PID: 31983
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.crashcourse/com.bignerdranch.android.crashcourse.CrashListActivity}: java.lang.IllegalArgumentException: column 'driverone' does not exist
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
      at android.app.ActivityThread.-wrap11(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: java.lang.IllegalArgumentException: column 'driverone' does not exist
      at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
      at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:87)
      at com.bignerdranch.android.crashcourse.database.CrashCursorWrapper.getCrash(CrashCursorWrapper.java:21)
      at com.bignerdranch.android.crashcourse.CrashShop.getCrashes(CrashShop.java:53)
      at com.bignerdranch.android.crashcourse.CrashListFragment.updateUI(CrashListFragment.java:110)
      at com.bignerdranch.android.crashcourse.CrashListFragment.onCreateView(CrashListFragment.java:46)
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1988)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:754)
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1653)
      at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:364)
      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:602)
      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
      at android.app.Activity.performStart(Activity.java:6253)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

创建表格时,不应在最后一个属性后面加上逗号,在你的情况下是EMAIL。

此外,AUTOINCREMENT在SQLite中没用,因为它已包含在PRIMARY KEY中。