SQLiteDatabase错误插入

时间:2012-12-26 09:50:06

标签: android sqlite

主要问题,

  1. 是否可以依赖onCreate来补充数据库表的更新,而无需使用onUpdate?

  2. AUTOINCREMENT是否适用于STRING列。这里很无能为力。

  3. 所以我收到一条错误,指出我的插件有问题。从生成的结果证明,Cursor.getCount()不断地/只输出1的旧记录。 我似乎无法插入来创建第二行。

    // 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();
    

    输出:

    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=demo89@hotmail.com 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: 
    

1 个答案:

答案 0 :(得分:2)

您正尝试在约束为NOT NULL的字段中插入空值。检查一下。我认为last_ip为空。

编辑:

违反了其他一些限制。我认为它的问题是由于TEXT上的自动增量。试试这个:

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" + ");";