1
的主要問題,SQLiteDatabase錯誤插入
是否可以依靠的onCreate補充更新到數據庫表,而不使用的onUpdate的?
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:
last_ip的意思是空或''atm。我可以將約束更改爲NULL還是可選? – babyhir
btw我編輯了第一行last_ip是可以接受的,它會產生一個輸出。 – babyhir
@babyhir,我剛剛檢查過你沒有在任何列中給出任何NOT NULL約束。那麼它一定是違反了其他約束。我在編輯我的答案 –