babyhir
babyhir

Reputation: 47

SQLiteDatabase Error Inserting

Main questions,

  1. Is it possible to rely on onCreate to supplement updates to a DB Table without the use of onUpdate?

  2. Does the AUTOINCREMENT apply for a STRING Column. Clueless here.

So I'm getting an Error stating that my insert is faulty. It is proven from the generated result where Cursor.getCount() constantly/only outputs the old record of 1. I can't seem to insert to create the 2nd row.

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "AccountDB";

// Account table name
private final String TABLE_ACCOUNTS = "AccountTable";

    public void onCreate(SQLiteDatabase db) {

    //boolean dbExist = checkDataBase();

    String CREATE_CONTACTS_TABLE = " CREATE TABLE " + TABLE_ACCOUNTS  + "("
            + id + " TEXT PRIMARY KEY AUTOINCREMENT ," + fullname + " TEXT,"
            + username + " TEXT," + password + " TEXT,"+ email + " TEXT," + contact_no + " TEXT,"+ activated + " TEXT," + banned + " TEXT," + ban_reason + " TEXT,"+ new_password_key + " TEXT," + new_password_requested + " TEXT,"
            + new_email + " TEXT," + new_email_key + " TEXT," + last_ip + " TEXT," + last_login + " TEXT," + created + " TEXT,"+ modified +" TEXT, " + encrypt_key + " TEXT,"+ login_key + " TEXT" + ");";
    db.execSQL(CREATE_CONTACTS_TABLE);

}

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(id, acc.getID());
    values.put(fullname, acc.getFullname()); 
    values.put(username, acc.getUsername()); 
    values.put(password, acc.getPassword()); 
    values.put(email, acc.getEmail()); 
    values.put(contact_no, acc.getContactNo());
    values.put(activated, acc.getActivated()); 
    values.put(banned, acc.getBanned()); 
    values.put(ban_reason, acc.getBanReason()); 
    values.put(new_password_key, acc.getNewPasswordKey()); 
    values.put(new_password_requested, acc.getNewPasswordRequested()); 
    values.put(new_email, acc.getNewEmail());
    values.put(new_email_key, acc.getNewEmailKey()); 
    values.put(last_ip, acc.getLastIP()); 
    values.put(last_login, acc.getLastLogin()); 
    values.put(created, acc.getCreated()); 
    values.put(modified, acc.getModified()); 
    values.put(encrypt_key, acc.getEncryptKey()); 
    values.put(login_key, acc.getLoginKey()); 
    System.out.println("hohoho "+acc.getLoginKey());
    Log.v("response",acc.getLoginKey());
    Log.v("DATABASEHandler","Table populated");
    // Inserting Row
    //System.out.println("hereee"+account.getLoginKey());
    db.insert(TABLE_ACCOUNTS, null, values);


    Cursor mCursor =db.query(TABLE_ACCOUNTS, new String[] {id,
                      fullname, username,password,email,contact_no,activated,
                        banned,ban_reason,new_password_key,new_password_requested,
                        new_email,new_email_key,last_ip,last_login,created,modified,
                        encrypt_key,login_key}, null,null, null, null, null);


    int count = mCursor.getCount();
    Log.d("Database stuff", "Count is "+count);
    if(mCursor != null)
        mCursor.moveToNext();

Output:

12-26 17:16:42.855: E/SQLiteDatabase(30078): Error inserting last_ip= new_password_key=null contact_no=0166262596 login_key=TWpZNU4yUTBaREE1TldOa1lUTXlOemxpWVdZM09ESXdZV1JpTTJVeU1UQT0= ban_reason=null password=$2a$08$aQvLAf5fGafdm.XkEkgw1uhg1O.KByN5LIWgIVXtKWWl8tpGlBG/q new_email_key=null banned=0 modified=2012-12-04 06:51:08 last_login=0000-00-00 00:00:00 id=13196 username=babyhir encrypt_key=WlROcll6UTJNWFozYm5rNGRYTjJjUT09 new_password_requested=null created=2012-12-04 06:52:34 [email protected] activated=1 new_email=null fullname=demo

12-26 17:16:42.855: E/SQLiteDatabase(30078): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed


12-26 17:16:42.855: V/DATABASEHandler(30078): query start
12-26 17:16:42.865: D/Database stuff(30078): Count is 1 (always 1!)
12-26 17:16:42.865: I/System.out(30078): HereID: 13196 (record of 1)

12-26 17:16:42.865: I/System.out(30078): Last IP: 

Upvotes: 1

Views: 8815

Answers (1)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You are trying to insert null value in a field which has constraint of NOT NULL. Check for that. I think last_ip is null.

Edit:

It is violating some other constraint. I think its problem due to auto increment on TEXT. Try this:

String CREATE_CONTACTS_TABLE = " CREATE TABLE " + TABLE_ACCOUNTS  + "("
            + id + " NUMBER PRIMARY KEY AUTOINCREMENT ," + fullname + " TEXT,"
            + username + " TEXT," + password + " TEXT,"+ email + " TEXT," + contact_no + " TEXT,"+ activated + " TEXT," + banned + " TEXT," + ban_reason + " TEXT,"+ new_password_key + " TEXT," + new_password_requested + " TEXT,"
            + new_email + " TEXT," + new_email_key + " TEXT," + last_ip + " TEXT," + last_login + " TEXT," + created + " TEXT,"+ modified +" TEXT, " + encrypt_key + " TEXT,"+ login_key + " TEXT" + ");";

Upvotes: 2

Related Questions