我试图在Android中的SQLite数据库中创建一个表,但由于某种原因它不起作用。我的代码似乎很好,有人可以帮我看看我做错了什么吗?
TableData.java
public class TableData {
public TableData(){
}
public static abstract class TableInfo implements BaseColumns{
public static final String EXPENSE_NAME = "name";
public static final String EXPENSE_COST = "cost";
public static final String EXPENSE_CATEGORY = "category";
public static final String DATABASE_NAME = "AquariumApp";
public static final String TABLE_NAME = "Expenses";
}
}
DatabadeOperations.java
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version = 2;
public String CREATE_QUERY = "CREATE TABLE " + TableInfo.TABLE_NAME + "(" + TableInfo.EXPENSE_NAME + " TEXT," + TableInfo.EXPENSE_COST + " TEXT," + TableInfo.EXPENSE_CATEGORY + " TEXT );";
public DatabaseOperations (Context context){
super(context, TableInfo.DATABASE_NAME, null, database_version);
Log.d("Database operations", "Database successfully created");
}
@Override
public void onCreate(SQLiteDatabase sdb){
sdb.execSQL(CREATE_QUERY);
Log.d("Database operations", "Table created");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2){
}
public void putInformation(DatabaseOperations dop, String name, String cost, String category){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableInfo.EXPENSE_NAME, name);
cv.put(TableInfo.EXPENSE_COST, cost);
cv.put(TableInfo.EXPENSE_CATEGORY, category);
long k = SQ.insert(TableInfo.TABLE_NAME, null, cv);
Log.d("Database operations", "One row inserted.");
}
}
AddExpense.java
public class AddExpense extends AppCompatActivity {
//EditText NameExpense = (EditText) findViewById(R.id.editText);
//EditText CostExpense = (EditText) findViewById(R.id.editText3);
EditText expenseName, expenseCost;
RadioGroup expenseCategory;
RadioButton radioButton;
String expenseNameString, expenseCostString, radiobutt, expenseCategoryString;
Button REG;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_expense);
setTitle("Add Expense");
expenseName = (EditText) findViewById(R.id.expenseName);
expenseCost = (EditText) findViewById(R.id.expenseCost);
expenseCategory = (RadioGroup) findViewById(R.id.RadioGroup);
REG = (Button) findViewById(R.id.button);
REG.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
expenseNameString = expenseName.getText().toString();
expenseCostString = expenseCost.getText().toString();
// get selected radio button from radioGroup
int selectedId = expenseCategory.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
expenseCategoryString = radioButton.getText().toString();
DatabaseOperations DB = new DatabaseOperations(ctx);
DB.putInformation(DB, expenseNameString, expenseCostString, expenseCategoryString);
Toast.makeText(getBaseContext(), "Expense Added!", Toast.LENGTH_LONG).show();
}
});
}