App Crashes after listing 14.6


#1

After I added the code from listing 14.6 it is showing a message on db.execSQL(“create table”) line but it is not showing any error. The message is “INDEX , TABLE , TEMP , TEMPORARY , TRIGGER , UNIQUE or VIRTUAL Expected , got tablecrimes”. When I run the app it crashes. Pls help me if you know the reason of app crashing.
My Logcat is showing this
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nishant.criminalintent/com.example.nishant.criminalintent.CrimeListActivity}: android.database.sqlite.SQLiteException: near “tablecrimes”: syntax error (code 1): , while compiling: create tablecrimes(_id integer primary key autoincrement , uuid, title, date, solved)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
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:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: android.database.sqlite.SQLiteException: near “tablecrimes”: syntax error (code 1): , while compiling: create tablecrimes(_id integer primary key autoincrement , uuid, title, date, solved)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1704)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1635)
at database.CrimeBaseHelper.onCreate(CrimeBaseHelper.java:20)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.nishant.criminalintent.CrimeLab.(CrimeLab.java:32)
at com.example.nishant.criminalintent.CrimeLab.get(CrimeLab.java:24)
at com.example.nishant.criminalintent.CrimeListFragment.updateUI(CrimeListFragment.java:134)
at com.example.nishant.criminalintent.CrimeListFragment.onCreateView(CrimeListFragment.java:56)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1244)
at android.app.Activity.performStart(Activity.java:6108)


#2
create tablecrimes(_id integer primary key autoincrement , uuid, title, date, solved)

What about adding a space between the “table” and the “crimes”?
By the way, I always use all caps for the SQL Key words. e.g.

        public static final String TABLE_NAME = "crimes";

        public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + Cols.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + Cols.UUID + ", "
                + Cols.TITLE + ", "
                + Cols.DATE + ", "
                + Cols.SOLVED + ", "
                + Cols.REQUIRES_POLICE
                + ")";

#4

Thanks Man for helping me. This solution worked out for me…