4

I have two edittext when i click on it a timepickerdialog will popup and the time will set in edittext for that my code is,

  int TIME_PICKER_ID = 1,hour,minute,flag;

  edittext_starttime.setOnClickListener(this);
  edittext_endtime.setOnClickListener(this);

    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

  @Override
   public void onClick(View v) {
     if(v.getId() == R.id.editText_starttime_stsa){
        flag = 1;
        showDialog(TIME_PICKER_ID);
    }//editText_starttime_stsa

    if(v.getId() == R.id.editText_endtime_stsa){
        flag = 2;
        showDialog(TIME_PICKER_ID);
    }//editText_endtime_stsa

    if(v.getId() == R.id.button_create_stsa){
        onCreateAlert();
    }//button_create_susa       
}

   @Override
protected Dialog onCreateDialog(int id) {
    switch(id){
    case 1: 
return new TimePickerDialog(this, timePickerListener, hour, minute, false);
    }
    return null;
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new     TimePickerDialog.OnTimeSetListener(){
    public void onTimeSet(TimePicker view,int selectedHour,int selectedMinute){
        hour = selectedHour;
        minute = selectedMinute;

        updateDisplay(hour,minute);
    }
};

private void updateDisplay(int hour, int minute) {
    if(flag==1){
        edittext_starttime.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
    }else if(flag==2){
        edittext_endtime.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
    }
}

private static String pad(int c) {
    if (c >= 10)
       return String.valueOf(c);
    else
       return "0" + String.valueOf(c);
}

I want to insert start_time and end_time in the sqlite database for that i have written query,

String query = "insert into Status_Master(status_id, status_name, status_description,  timewindow_start, timewindow_end) values("+status_id+",'"+status_name+"','"+status_desc+"',time('"+start_time+"'),time('"+end_time+"'))";

But i am getting error. Is this the correct way to insert time in database?

3
  • what error u are getting? plz also add log with question if app is crashing Commented Mar 1, 2013 at 11:13
  • before inserting the time values directly to the database u should convert it to the appropriate format that is supported by sqlite Commented Mar 1, 2013 at 11:30
  • But i think time() will change it in sqlite time format know. Else how to convert it? please give the example. Commented Mar 1, 2013 at 11:36

3 Answers 3

2
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

  Date date1 = new Date();
  Date date2 = new Date();

    ContentValues initialValues = new ContentValues(); 
         initialValues.put("status_id",status_id);
         initialValues.put("status_name", status_name);
         initialValues.put("status_description", status_desc);



         initialValues.put("timewindow_start", dateFormat.format(date1));
          initialValues.put("timewindow_end", dateFormat.format(date2));

    long result= mDb.insert(DB_TABLE_NAME, null, initialValues);
Sign up to request clarification or add additional context in comments.

1 Comment

I want to insert time only not date.
0

Please check the SQLite site and go through the time functions. SQLite only supports the 12 formats listed in the site. Make sure the strings you are providing are in the correct format.

1 Comment

But i have used time() only.
0

Do it this way. First create database, my code is here.

public class AppDatabase extends SQLiteOpenHelper
{
    private final static String DATABASE_NAME="AppDB";
    public final static String TABLE_PASSWORD="password";
    public static int version=1;
    public Context cont;
    public AppDatabase(Context context)
    {
        super(context,DATABASE_NAME, null, version);
        this.cont=context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqlDb)
    {
        sqlDb.execSQL("Create table password(_id INTEGER autoincreament PRIMARY KEY, monthOfBirth INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqlDb, int old, int latest)
    {
        sqlDb.execSQL("DROP TABLE password");
        this.onCreate(sqlDb);
    }
 }

Now create a transaction class to insert values into database by a method.

public long savePassword(int numberMonth)
    {
        long x=-1;
        sqliteDb=appDb.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("monthOfBirth", numberMonth);
        try
        {
            if(sqliteDb.isOpen())
            {
                x=sqliteDb.insert("password", null, values);
                if(x>=0)
                {
                    //Toast.makeText(context, "Password Save", Toast.LENGTH_SHORT).show();
                }
            }
        }
        catch(Exception exc)
        {
            System.err.print(exc);
        }
    return x;
    }

Now in your activity just pick the value from edittext and and call this method via Transaction's object.

trans = new Transactions(MainActivity.this);
                            long x = trans.savePassword(pMonth);
                            if (x >= 0)
                                {
                                    Toast.makeText(getBaseContext(), "Password Save in Database.", Toast.LENGTH_SHORT).show();
                                } 
                            else
                                {
                                    Toast.makeText(getBaseContext(), "Password didn't save.",   Toast.LENGTH_SHORT).show();
                                }

Hope this will help.

2 Comments

i just want that how to insert time in database. Give the insert query for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.