有人可以告诉我这段代码有什么问题吗?它应该在数据库中添加一个新行,但是当我运行它时,应用程序会停止。我检查了联系人的详细信息,但是当在数据库中添加新信息时,它就会停止工作。
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "ContactsDetails.db";
public static final String TABLE_NAME = "ConactsDetails";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRST_NAME = "first_name";
public static final String COLUMN_SURNAME = "surname";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_PHONE = "phone";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_USERNAME = "username";
public static final String COLUMN_PASSWORD = "password";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, DATABASE_NAME, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT" +
COLUMN_FIRST_NAME + " TEXT " +
COLUMN_SURNAME + " TEXT " +
COLUMN_DESCRIPTION + " TEXT " +
COLUMN_PHONE + " TEXT " +
COLUMN_EMAIL + " TEXT " +
COLUMN_USERNAME + " TEXT " +
COLUMN_PASSWORD + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
//add new contact's details
public void addContact(NewUserContacts contact){
ContentValues values = new ContentValues();
values.put(COLUMN_FIRST_NAME, contact.getFirstName());
values.put(COLUMN_SURNAME, contact.getSurname());
values.put(COLUMN_DESCRIPTION, contact.getDescription());
values.put(COLUMN_PHONE, contact.getPhone());
values.put(COLUMN_EMAIL, contact.getEmail());
values.put(COLUMN_USERNAME, contact.getUsername());
values.put(COLUMN_PASSWORD, contact.getPassword());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
}