[英]Create entries using SQLite (Android)
I have problem with my insert method while testing it by JUnit Android Test and no enrty is saved in the database : 我在通过JUnit Android Test测试它的插入方法时遇到问题,并且数据库中未保存任何enrty:
public long save(TAV t) {
ContentValues v = new ContentValues();
v.put(TAV_TITRE, t.getTitle());
v.put(TAV_DATE, t.getDate());
v.put(TAV_DESCRIPTION, t.getDescription());
return db.insert(TAV_TABLE, null, v);
}
//this is the JUnit Test Method
public void testAddition() {
setUp();
TAV t = new TAV("titre", "date", "decsritpion");
m.open();
m.save(t);
Log.i(tag, "insertion");
testShowAll();
}
HELP PLEASE !!! 请帮助 !!!
In your save()
method, make sure you make an instance of your databse,something like SQLiteDatabase db = this.getWritableDatabase();
在
save()
方法中,确保创建数据库实例,例如SQLiteDatabase db = this.getWritableDatabase();
.And remove return
from return db.insert(TAV_TABLE, null, v);
然后从
return db.insert(TAV_TABLE, null, v);
删除return
return db.insert(TAV_TABLE, null, v);
ie use 即使用
public void save(TAV t) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues v = new ContentValues();
v.put(TAV_TITRE, t.getTitle());
v.put(TAV_DATE, t.getDate());
v.put(TAV_DESCRIPTION, t.getDescription());
db.insert(TAV_TABLE, null, v);
}
From the SQLiteDatabase Documents 从SQLiteDatabase文档
public long insert (String table, String nullColumnHack, ContentValues values) Returns the row ID of the newly inserted row, or -1 if an error occurred
公共长插入(字符串表,字符串nullColumnHack,ContentValues值)返回新插入的行的行ID;如果发生错误,则返回-1。
So if your purpose is just to add a row into your database then your method need not return anything.So you can just make return type of your method void
and use db.insert
inside. 因此,如果您的目的只是在数据库中添加一行,则您的方法无需返回任何内容,因此您可以使方法的返回类型为
void
并在其中使用db.insert
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.