I have a problems when i want to add something in my DB. I get error 19 idimage can not be null, i do not understand why i get this error, i defined that idimage is auto increment.
Thanks for your help
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);
Hello, thanks for your answer i still have the problems.
There is more information.
I changed my sql request:
"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 )";
There is the logcat error:
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)
There is the code for the insertion:
// 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);
}