我无法找到代码中缺少的内容。我正在尝试插入数据,但数据库助手类中的“long result == db.insert”始终返回-1”。我无法指出我做错了什么。请帮忙。任何想法将不胜感激。
我的数据库助手类:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "drivefinal.db";
public static final String TABLE_NAME = "drive_table";
public static final String ID = "ID";
public static final String NUMBER = "Number";
public static final String FNAME = "First Name";
public static final String LNAME = "Last Name";
public static final String COORDINATE = "Coordinate";
public static final String ADDRESS = "Address";
public static final String NOTES = "Notes";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists " + TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT ,NUMBER TEXT,FNAME TEXT,LNAME TEXT,COORDINATE TEXT,ADDRESS TEXT,NOTES TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(db);
}
public boolean insertData(String number, String fName, String lName, String coordinate, String address, String notes){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NUMBER, number);
contentValues.put(FNAME, fName);
contentValues.put(LNAME, lName);
contentValues.put(COORDINATE, coordinate);
contentValues.put(ADDRESS, address);
contentValues.put(NOTES, notes);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1 )
return false;
else
return true;
}
慕森王
相关分类