我刚刚开始研究一个项目,但我的程序反复崩溃。我认为这是由于getWritableDatabase。我正在按照教程,我做了他们告诉我的所有事情,我甚至多次检查了我的代码。如果您对教程的链接感兴趣,那就是https://www.youtube.com/watch?v=38DOncHIazs
public class AddExpense extends Activity {
EditText inputDate , inputDescription, inputCategory, inputAmount;
Context context;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_expense);
inputDate = (EditText) findViewById(R.id.inputDate);
inputDescription = (EditText) findViewById(R.id.inputDescription);
inputCategory = (EditText) findViewById(R.id.inputCategory);
inputAmount = (EditText) findViewById(R.id.inputAmount);
}
public void AddExpense(View view){
String date = inputDate.getText().toString();
String description = inputDescription.getText().toString();
String category = inputCategory.getText().toString();
String amount = inputAmount.getText().toString();
userDbHelper = new UserDbHelper(context);
sqLiteDatabase = userDbHelper.getWritableDatabase();
userDbHelper.addInformations(date, description, category, amount, sqLiteDatabase);
Toast.makeText(getBaseContext(),"Data Saved", Toast.LENGTH_LONG).show();
userDbHelper.close();
}
}
这是我的数据库助手类
public class UserDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "EXPENSE.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
"CREATE TABLE " + ExpenseDatabase.NewExpenseItem.TABLE_NAME + " ("+ ExpenseDatabase.NewExpenseItem.DATE+" TEXT,"+ ExpenseDatabase.NewExpenseItem.DESCRIPTION+ " TEXT,"+
ExpenseDatabase.NewExpenseItem.CATEGORY+" TEXT," + ExpenseDatabase.NewExpenseItem.AMOUNT+ " TEXT);";
public UserDbHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS", "Database created / opened");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS", "Table created");
}
public void addInformations(String date, String description, String category, String amount,SQLiteDatabase db ){
ContentValues contentValues = new ContentValues();
contentValues.put(ExpenseDatabase.NewExpenseItem.DATE,date);
contentValues.put(ExpenseDatabase.NewExpenseItem.DESCRIPTION, description);
contentValues.put(ExpenseDatabase.NewExpenseItem.CATEGORY, category);
contentValues.put(ExpenseDatabase.NewExpenseItem.AMOUNT, amount);
db.insert(ExpenseDatabase.NewExpenseItem.TABLE_NAME, null, contentValues);
Log.e("DATABASE OPERATIONS", "One row inserted in DB");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
答案 0 :(得分:0)
根据我的经验,最好只在UserDbHelper类中创建一个方法并将相关值传递给该方法。 尝试在该类本身中执行与数据库相关的所有操作。我认为它会奏效。
编辑:
public void addInformation(your values){
ContentValues values = new ContentValues();
values.put();
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE,null,values);
db.close();
}
答案 1 :(得分:0)
尝试使用此userDbHelper = new UserDbHelper(this);某些您的上下文未初始化。这可能是问题