proG
proG

Reputation: 111

Android , SQLite.... the primary key is inserted as null

I'm a beginner with android, and trying to create a database. with one table and three functions add , delete, update and retrieve.

When I insert an item to the data base the primary key will be given a number that is incremented each time .. but it is inserted to the database as null !

Therefore I couldn't update and delete records because I need the id. and for the retrieve the output works fine except for the id it is null.

Here is the schema

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME= "alarmapp"; 
private static final String CREATE_alarm= "CREATE TABLE alarm" +
        " (Aid integer  PRIMARY KEY NOT NULL AUTOINCREMENT," +
        "message varchar(100)," +
        "date varchar(100));";

and here is the insert code:

// columns 
private static final String AI = "Aid";
private static final String Message= "message";
private static final String Date = "date";

// tables
private static final String TABLE_NAME= "alarm";

private Context context;
private DatabaseHelper dbhelper;
private SQLiteDatabase database;

public long add( String message, String date) {
    ContentValues values= new ContentValues();

    values.put(Message,message);
    values.put(Date, date);

    return database.insert(TABLE_NAME, null,values);
}

Upvotes: 0

Views: 4168

Answers (1)

user1122549
user1122549

Reputation: 738

Check out the following link: http://www.androidph.com/2012/01/android-sqlite-primary-key-auto.html

Upvotes: 4

Related Questions