当我从应用程序退出并再次运行时,我的应用程序不会插入值,我的数据库出现此问题。我确信数据只存储在第一次运行中,我不能再添加数据了。另外,由于这个原因,我无法更新。
有人可以帮忙吗?
public static final String TABLE_NAME = "User_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "Password";
public static final String TABLE_NAME2 = "Courses";
public static final String COL_1C = "ID";
public static final String COL_2C = "Course_Name";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//create first table
String CREATE_USER_TABLE= "CREATE TABLE " + TABLE_NAME + "(" + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_2+ " TEXT," + COL_3 + " TEXT" +")";
db.execSQL(CREATE_USER_TABLE);
// create second table
String CREATE_COURSE_TABLE= "CREATE TABLE " + TABLE_NAME2 + "(" + COL_1C + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_2C+ " TEXT" +")";
db.execSQL(CREATE_COURSE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
//first table
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
// Create tables again
onCreate(db);
}
public boolean Inser_to_user(String name,String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues Values = new ContentValues();
Values.put(COL_2, name);
Values.put(COL_3, password);
long Result = db.insert(TABLE_NAME, null, Values);
if (Result == -1) {
return false;
} else {
return true;
}
}
public boolean Inser_to_courses(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues Values = new ContentValues();
Values.put(COL_2C, name);
long Result = db.insert(TABLE_NAME2, null, Values);
if (Result == -1) {
return false;
} else {
return true;
}
}
public Cursor showdata(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor Result=db.rawQuery("select * from" +TABLE_NAME,null);
return Result;
}
public boolean updateuser(String id,String name,String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues Values=new ContentValues();
Values.put(COL_1,id);
Values.put(COL_2,name);
Values.put(COL_3,password);
db.update(TABLE_NAME,Values,COL_1 +"=?",new String[]{Integer.toString(Integer.parseInt(id))});
return true;
}