1

主要问题,

  1. 是否可以在不使用 onUpdate 的情况下依靠 onCreate 来补充数据库表的更新?

  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=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: 
4

1 回答 1

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" + ");";
于 2012-12-26T09:54:40.697 回答