Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence Using SQLite for Structured Data Adding the Annotations Table

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

Getting Error: E/SQLiteLog﹕ (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

Hi,

I'm getting a SQLiteException when I try to run memeMaker for the first time to try to create the tables. See below log;

06-03 12:47:49.550 5819-5819/com.teamtreehouse.mememaker E/SQLiteLog﹕ (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY 06-03 12:47:49.550 5819-5819/com.teamtreehouse.mememaker D/AndroidRuntime﹕ Shutting down VM 06-03 12:47:49.551 5819-5819/com.teamtreehouse.mememaker E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.teamtreehouse.mememaker, PID: 5819 android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE MEMES(_id INEGER PRIMARY KEY AUTOINCREMENT,ASSET TEXT,NAME TEXT) 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.SQLiteStatement.<init>(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605) at com.teamtreehouse.mememaker.database.MemeSQLiteHelper.onCreate(MemeSQLiteHelper.java:51) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251) at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187) at com.teamtreehouse.mememaker.database.MemeDatasource.<init>(MemeDatasource.java:26) at com.teamtreehouse.mememaker.ui.fragments.MemeItemFragment.onResume(MemeItemFragment.java:89) at android.app.Fragment.performResume(Fragment.java:2096) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:928) at android.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:789) at android.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1091) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1469) at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:483) at android.support.v13.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145) at android.support.v4.view.ViewPager.populate(ViewPager.java:1068) at android.support.v4.view.ViewPager.populate(ViewPager.java:914) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560) at android.view.View.measure(View.java:17430) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 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)

2 Answers

Ari Sprung
Ari Sprung
2,256 Points

You need to add a space between KEY_ID AND INTEGER

So change

  • KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " to

  • KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

Thanks - however I already had the space.

I had, "INEGER PRIMARY KEY " - So i've now changed that to be correct, However it's still crashing on take off.

See my MemeSQLiteHelper.java;

public class MemeSQLiteHelper extends SQLiteOpenHelper{

private static final String DB_NAME = "memes.db";
private static final int DB_VERSION = 1;
//Meme Table functionality
public static final String MEMES_TABLE = "MEMES";
public static final String COLUMN_MEME_ASSET = "ASSET";
public static final String COLUMN_MEME_NAME = "NAME";
private static String CREATE_MEMES =
        "CREATE TABLE " + MEMES_TABLE + "("
                + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_MEME_ASSET + " TEXT," +
                COLUMN_MEME_NAME + " TEXT)";

//Meme Table Annotations functionality
public static final String ANNOTATIONS_TABLE = "ANNOTATIONS";
public static final String COLUMN_ANNOTATION_COLOR = "COLOR";
public static final String COLUMN_ANNOTATION_X = "X";
public static final String COLUMN_ANNOTATION_Y = "Y";
public static final String COLUMN_ANNOTATION_TITLE = "TITLE";
public static final String COLUMN_FOREIGN_KEY_MEME = "MEME_ID";

private static final String CREATE_ANNOTATIONS = "CREATE TABLE " + ANNOTATIONS_TABLE + " (" +
        BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
        COLUMN_ANNOTATION_X + " INTEGER, " +
        COLUMN_ANNOTATION_Y + " INTEGER, " +
        COLUMN_ANNOTATION_TITLE + " TEXT, " +
        COLUMN_ANNOTATION_COLOR + " TEXT, " +
        COLUMN_FOREIGN_KEY_MEME + " INTEGER, " +
        "FOREIGN KEY(" + COLUMN_FOREIGN_KEY_MEME + ") + REFERENCES MEMES(_ID))";


public MemeSQLiteHelper(Context context)
{
    super(context, DB_NAME, null, DB_VERSION);
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(CREATE_MEMES);
    sqLiteDatabase.execSQL(CREATE_ANNOTATIONS);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

}

}