简体   繁体   中英

how to insert first row value static when the onCreate() method will call and other will be dynamic on click button in a database?

I check if customer index=0 then inserting the data on OnCREATE method and else part inserting the data on click button.

if (C_Id==0){
    helper.CustomerAdd(new CustomerModel("developer","test","test@gmail.com","9874643212","test",null,"abc road","gurgaon","india",null));
}

public int CustomerAdd(CustomerModel fm){
    int cust_id=0;
    SQLiteDatabase db = this.getWritableDatabase();
    db.beginTransaction();
    ContentValues cv = new ContentValues();
    cv.put( C_NAME, fm.getC_name() );
    cv.put( C_NO, fm.getC_phone() );
    cv.put( C_EMAIL, fm.getC_email() );
    cv.put( BILL_NAME, fm.getC_bill_name() );
    cv.put( GST_NO, fm.getGst_no() );
    cv.put( C_ADDRESS, fm.getC_address() );
    cv.put( CITY, fm.getC_city() );
    cv.put( Country, fm.getC_country() );
    cv.put( DESCRIPTION, fm.getDescription() );
    cv.put( JOB_Tittle, fm.getJob_title());
    SimpleDateFormat sdfdndj = new SimpleDateFormat( "dd/MM/yy" );
    String Timedatanew = sdfdndj.format( new java.util.Date() );
    cv.put( Created_date, Timedatanew );
    try{
        db.insert( CUSTOMER_TABLE, null, cv );
        Cursor cursor = db.rawQuery( "Select C_ID from CUSTOMER Order by C_ID DESC limit 1",null);
        if (cursor.moveToLast()){
            cust_id= cursor.getInt( 0 );
        }else {
            cust_id=0;
        }
    }

I tried like this but don't know how to add data on the first row. any help would be appreciated!

Your issue could be that you begin a transaction but don't commit ( setTransactionSuccessful ) and end the transaction.

However, for a single action such as inserting a row then there is no need to begin/commit/end as being a single action it will be it's own transaction.

As such, you could just remove the line db.beginTransaction();


If your issue is how to add a row to the CUSTOMER table, if there are no rows, in the onCreate method of an activity, then you could code and call the following method (this assumes that the CustomerAdd method is in a class named MyDatabaseHelper , therefore you may need to change accordingly)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    helper = new MyDatabaseHelper(this);
    addFirstCustomerIfNone(); //<<<<<<<<<<
}

private void addFirstCustomerIfNone() {
    SQLiteDatabase db = helper.getWritableDatabase();
    if(DatabaseUtils.queryNumEntries(db,"CUSTOMER") < 1) {
        if (helper.CustomerAdd(new CustomerModel("developer","test","test@gmail.com","9874643212","test",null,"abc road","gurgaon","india",null)) ==0) {
            Toast.makeText(this,"Customer Added Successfully.",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"Customer was not added.",Toast.LENGTH_SHORT).show();
        }
    }
}

Thus when the activity is started and the onCreate method is invoked helper (an instance of the database helper class) is instantiated and then the addFirstCustomerIfNone method is called.

The addFirstCustomerIfNone method checks to see if the CUSTOMER table has any rows, if not then the first row is added. If rows already exists then nothing is added.

  • Note the above is based upon the available information and makes some assumptions and may therefore not cater for all circumstance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM