0
我有一個問題,當我想添加一些東西在我的數據庫。我得到錯誤19 idimage不能爲空,我不明白爲什麼我得到這個錯誤,我定義了idimage是自動增量。Android sqlite idimage不能爲空
感謝您的幫助
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class BaseSqlLite extends SQLiteOpenHelper{
private static final String CREATE_BDD =
"CREATE TABLE IF NOT EXISTS `t_image` (`idimage` INTEGER PRIMARY KEY AUTOINCREMET,`name` VARCHAR(20) NOT NULL ,`description` VARCHAR(45) NOT NULL ,`rank` TINYINT NOT NULL ,`date` VARCHAR(45) NOT NULL ,PRIMARY KEY (`idimage`))";
public BaseSqlLite(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BDD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE t_image ;");
onCreate(db);
}
}
ImagesBDD imagesBdd = new ImagesBDD(context);
SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
String PhotoName = "photo_" + timeStampFormat.format(new Date()) +".jpg" ;
String descri = myInputText.getText().toString();
int rank = 1;
String date = timeStampFormat.format(new Date());
Image image = new Image(PhotoName,descri,rank,date);
// Open the BD
imagesBdd.open();
// insert of the image
imagesBdd.insertImage(image);
您好,感謝您的回答,我仍然有問題。
有更多的信息。
我改變了我的SQL請求:
"CREATE TABLE IF NOT EXISTS t_image (idimage INTEGER PRIMARY KEY AUTOINCREMET,name VARCHAR(20) NOT NULL ,description VARCHAR(45) NOT NULL ,rank TINYINT NOT NULL ,date VARCHAR(45) NOT NULL)";
還有就是logcat的錯誤:
E/SQLiteDatabase(19708): Error inserting rank=1 date=2013-04-25-15.53.20 description=gun name=photo_2013-04-25-15.53.20.jpg
E/SQLiteDatabase(19708): android.database.sqlite.SQLiteConstraintException: t_image.idimage may not be NULL (code 19)
沒有爲插入的代碼:
// Insetion d'imahe
public long insertImage(Image image){
//Création d'un ContentValues
ContentValues values = new ContentValues();
//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(COL_NAME, image.getName());
values.put(COL_DESCRIPTION, image.getDescription());
values.put(COL_RANK, image.getRank());
values.put(COL_DATE, image.getDate());
//on insère l'objet dans la BDD via le ContentValues
return bdd.insert(TABLE_ImageS, null, values);
}
發佈插入數據並從logcat完成堆棧跟蹤的代碼。 – 2013-04-25 13:43:58