2012-12-26 49 views
1

的主要问题,SQLiteDatabase错误插入

  1. 是否可以依靠的onCreate补充更新到数据库表,而不使用的onUpdate的?

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

所以我收到一个错误,指出我的插入有问题。从生成的结果证明,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 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: 

回答

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

last_ip的意思是空或''atm。我可以将约束更改为NULL还是可选? – babyhir

+0

btw我编辑了第一行last_ip是可以接受的,它会产生一个输出。 – babyhir

+0

@babyhir,我刚刚检查过你没有在任何列中给出任何NOT NULL约束。那么它一定是违反了其他约束。我在编辑我的答案 –