jeudi 31 mars 2016

my database cannot saved and not create a table?

Give me a solution? and can you tell me if this class have an errror?

public class UserDbHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "HISSABBOOK.db";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
        "CREATE TABLE " + UserTransaction.TransactionInfo.TABLE_NAME + "(" + UserTransaction.TransactionInfo.DATE + " TEXT," + "" + UserTransaction.TransactionInfo.AMOUNT + " TEXT," + "" + UserTransaction.TransactionInfo.CATEGORY + " TEXT," + UserTransaction.TransactionInfo.PAYMENTMETHOD + " TEXT," + "" + UserTransaction.TransactionInfo.TAG + " TEXT," + "" + UserTransaction.TransactionInfo.QUANTITY + " TEXT," + "" + UserTransaction.TransactionInfo.DESCRIPTION + " TEXT);";

public UserDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.e("Database operation", "Database created/open....");
}




@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_QUERY);
    Log.e("DATABASE OPERATION","Table created...");

}

public void addIncomeData(String date, String amount, String category, String paymentmethod, String tag, String quantity, String description, SQLiteDatabase db)
{


    ContentValues contentValues = new ContentValues();
    contentValues.put(UserTransaction.TransactionInfo.DATE, date);
    contentValues.put(UserTransaction.TransactionInfo.AMOUNT, amount);
    contentValues.put(UserTransaction.TransactionInfo.CATEGORY, category);
    contentValues.put(UserTransaction.TransactionInfo.PAYMENTMETHOD, paymentmethod);
    contentValues.put(UserTransaction.TransactionInfo.TAG, tag);
    contentValues.put(UserTransaction.TransactionInfo.QUANTITY, quantity);
    contentValues.put(UserTransaction.TransactionInfo.DESCRIPTION, description);
    db.insert(UserTransaction.TransactionInfo.TABLE_NAME, null, contentValues);
    Log.e("DATABASE OPERATION", "one row inserted...");
}

public class AddIncomeActivity extends AppCompatActivity  {

Button donebtn;
EditText dateEdt,amountEdt,tagEdt,quantityEdt,descriptionEdt;
Spinner paymentmethodSpin,categorySpin;
Context context =this;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addincome_activity);
    donebtn=(Button)findViewById(R.id.btn_done);
    dateEdt=(EditText)findViewById(R.id.date_id);
    amountEdt=(EditText)findViewById(R.id.amount_id);
    tagEdt=(EditText)findViewById(R.id.tag_id);
    quantityEdt=(EditText)findViewById(R.id.quantity);
    descriptionEdt=(EditText)findViewById(R.id.description_id);
    paymentmethodSpin=(Spinner)findViewById(R.id.paymentmethod_spin);
    categorySpin=(Spinner)findViewById(R.id.category_spin);
    getSupportActionBar();


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.savemenu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id=item.getItemId();

    if(id==R.id.save)
    {

        Toast.makeText(this,"Save",Toast.LENGTH_LONG).show();
        return true;
    }
    else if (id==R.id.mainhome)
    {
        startActivity(new Intent(this,MainActivity.class));
        return true;
    }
    else if (id==android.R.id.home)
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}





public void addIncome(View view){

    String date=dateEdt.getText().toString();
    String amount=amountEdt.getText().toString();
    String category=categorySpin.getSelectedItem().toString();
    String paymentmethod=paymentmethodSpin.getSelectedItem().toString();
    String tag=tagEdt.getText().toString();
    String quantity=quantityEdt.getText().toString();
    String description=descriptionEdt.getText().toString();
    userDbHelper=new UserDbHelper(context);
    sqLiteDatabase=userDbHelper.getWritableDatabase();
    userDbHelper.addIncomeData(date,amount,category,paymentmethod,tag,quantity,description,sqLiteDatabase);
    Toast.makeText(getBaseContext(),"Data Saved",Toast.LENGTH_LONG).show();
    userDbHelper.close();

}

this class contain information a fields?

public class UserTransaction {

public static abstract class TransactionInfo {

    public static final String TABLE_NAME = "transaction";
    public static final String DATE = "date";
    public static final String AMOUNT = "amount";
    public static final String CATEGORY = "category";
    public static final String PAYMENTMETHOD = "paymentmethod";
    public static final String TAG = "tag";
    public static final String QUANTITY = "quantity";
    public static final String DESCRIPTION = "quantity";
}
}

Aucun commentaire:

Enregistrer un commentaire