【发布时间】:2013-04-25 13:38:05
【问题描述】:
当我想在我的数据库中添加一些东西时,我遇到了问题。我收到错误 19 idimage can not be null,我不明白为什么会收到此错误,我定义 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 插入数据并完成堆栈跟踪的代码。