我正在创建一个应用程序,但是当我去插入一些数据时,它不起作用。这是我的应用程序的screenshot,包括错误消息。这是我的database
课程:
public class database extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "STUDENT";
public static final String TABLE_NAME = "TRIP";
public static final String COL1 = "ID";
public static final String COL2 = "NAME";
public static final String COL3 = "CURRENT";
public static final String COL4 = "DESTINATION";
public static final String COL5 = "BUDGET";
public database(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CURRENT TEXT,DESTINATION TEXT,BUDGET INTEGER )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
public void insertdata(String name,String current,String destination,String budget)
{
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL("insert into TRIP(NAME,CURRENT,DESTINATION,BUDGET)values('" + name + "','" + current + "','" + destination + "',"+Integer.parseInt(budget)+")");
}
}
有关如何解决此问题的任何建议?
答案 0 :(得分:1)
尝试使insertdata
函数返回long
。将insertdata
函数替换为以下函数:
public long insertdata(String name, String current, String destination, String budget) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, name);
contentValues.put(COL3, current);
contentValues.put(COL4, destination);
contentValues.put(COL5, budget);
return db.insert(TABLE_NAME, null, contentValues);
}
在您插入数据的活动中,请确保按以下方式调用此函数:
database db = new database(this);
if (db.insertdata("name", "current", "destination", "budget") > 0)
// do something else here
如果您仍遇到同样的问题,请卸载您的应用并重试。如果它仍然无法正常工作,请随时在下面发表评论。