akhil
akhil

Reputation: 347

Primary key is inserting as null in android sqlite

I am learning mobile app development. While I am working sqlite database every thing is fine but primary key column is always null.

Code for create table:

 public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE "+ DATABASE_TABLE+"(" +
        KEY_ROWID+" INTERGER PRIMARY KEY," +
        KEY_NAME+" TEXT NOT NULL,"+
        KEY_HOTNESS+" TEXT NOT NULL);"
                );

    }

code for insert:

public String getData() {
    // TODO Auto-generated method stub
    String[] columns= {KEY_ROWID,KEY_NAME,KEY_HOTNESS};
    Cursor c=ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    String result="";

    int iRow=c.getColumnIndex(KEY_ROWID);
    int iName=c.getColumnIndex(KEY_NAME);
    int iHotness=c.getColumnIndex(KEY_HOTNESS);

    for(c.moveToFirst() ; !c.isAfterLast() ; c.moveToNext()){
        result= result + c.getString(iRow)+" "+c.getString(iName)+" "+c.getString(iHotness)+ "\n";
    }



    return result;
}

Upvotes: 0

Views: 322

Answers (1)

Jeremy Dowdall
Jeremy Dowdall

Reputation: 1274

add AUTOINCREMENT to the primary key definition:

KEY_ROWID+" INTEGER PRIMARY KEY AUTOINCREMENT,"

(also make sure it's INTEGER and not INTERGER :))

Upvotes: 2

Related Questions