-1

I want to take date, time & title field value for creating reminder. I don't know how to store date & time data. I'm not able to insert data into database. Advance thanks for help.

AddReminder.java

import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class AddReminder extends AppCompatActivity implements View.OnClickListener{

    private EditText et_name, et_date, et_time, et_reminder_title;
    private Button btn_add, btn_view, btn_date, btn_time, btn_set_reminder, btn_view_reminders;
    private int mYear, mMonth, mDay, mHour, mMinute;

    private String s_date, s_time, s_title;

    private DBOperation operation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_reminder);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Add RemindMe");

        operation = new DBOperation(this);


        /*********************************************************************/

        btn_date = (Button)findViewById(R.id.btn_date);
        btn_time = (Button)findViewById(R.id.btn_time);
        et_date = (EditText)findViewById(R.id.in_date);
        et_time = (EditText)findViewById(R.id.in_time);
        et_reminder_title = (EditText)findViewById(R.id.et_reminder_title);

        btn_date.setOnClickListener(this);
        btn_time.setOnClickListener(this);

        btn_set_reminder = (Button)findViewById(R.id.btn_set_reminder);
        btn_set_reminder.setOnClickListener(this);

        btn_view_reminders = (Button)findViewById(R.id.bt_view_reminders);
        btn_view_reminders.setOnClickListener(this);

    }


    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public void onClick(View v) {
        if (v == btn_date) {

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {

                            et_date.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
        if (v == btn_time) {

            // Get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {

                            et_time.setText(hourOfDay + ":" + minute);
                        }
                    }, mHour, mMinute, false);
            timePickerDialog.show();
        }

        if(v == btn_set_reminder){

            boolean isInserted = operation.insertData(et_date.getText().toString(),
                    et_time.getText().toString(),
                    et_reminder_title.getText().toString());

            if (isInserted == true)
                Toast.makeText(AddReminder.this, "Data Inserted", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(AddReminder.this, "Data Not Inserted", Toast.LENGTH_SHORT).show();
        }

        if (v == btn_view_reminders)
        {

            Cursor res = operation.getAllData();
            if (res.getCount() == 00)
            {
                showMsg("ERROR", "No Data Found");
                return;
            }
            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext())
            {
                buffer.append("ID: "+res.getString(0) + "\n");
                buffer.append("Date: "+res.getString(1) + "\n");
                buffer.append("Time: "+res.getString(2) + "\n");
                buffer.append("Title: "+res.getString(3) + "\n\n");
            }

            showMsg("Data", buffer.toString());
        }
    }

    public void showMsg(String msg_title, String msg)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(msg_title);
        builder.setMessage(msg);
        builder.show();
    }
}

DBOperation.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.EditText;

public class DBOperation extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "reminder_db.db";
    public static final String TABLE_NAME = "reminder_table";
    public static final String ID = "C_ID";
    public static final String DATE = "C_DATE";
    public static final String TIME = "C_TIME";
    public static final String TITLE = "C_TITLE";

    public DBOperation(Context context)
    {
        super(context,DATABASE_NAME,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+ TABLE_NAME + "(C_ID INTEGER PRIMARY KEY AUTOINCREMENT, C_DATE TEXT, C_NAME TEXT, C_TITLE TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String date, String time, String title){

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(DATE,date);
        contentValues.put(TIME,time);
        contentValues.put(TITLE,title);

        long result = db.insert(TABLE_NAME,null,contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

    public Cursor getAllData()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("SELECT * FROM " +TABLE_NAME, null);
        return res;
    }    
}

Error

Error inserting C_DATE=8/3/2017 C_TIME=23:14 C_TITLE=Rem
 android.database.sqlite.SQLiteException: table reminder_table has no column named C_TIME (code 1): , while compiling: INSERT INTO reminder_table(C_DATE,C_TIME,C_TITLE) VALUES (?,?,?)
     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
     at mukesh.com.task11.DBOperation.insertData(DBOperation.java:44)
     at mukesh.com.task11.AddReminder.onClick(AddReminder.java:146)
     at android.view.View.performClick(View.java:4444)
     at android.view.View$PerformClick.run(View.java:18457)
     at android.os.Handler.handleCallback(Handler.java:733)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5113)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
     at dalvik.system.NativeStart.main(Native Method)
2
  • Can't read explicit error messages, can't search on Google, ... Oh boy, are you sure you should be working in this field ??? Commented Apr 1, 2017 at 9:27
  • Possible duplicate of Android: Database handler SQLiteException Commented Apr 1, 2017 at 9:47

1 Answer 1

1

That's because you don't have the column C_TIME in your onCreate script:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE "+ TABLE_NAME + "(C_ID INTEGER PRIMARY KEY AUTOINCREMENT, C_DATE TEXT, C_NAME TEXT, C_TITLE TEXT)");
}

You forgot to add the field:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE "+ TABLE_NAME + "(C_ID INTEGER PRIMARY KEY AUTOINCREMENT, C_DATE TEXT, C_NAME TEXT, C_TITLE TEXT, C_TIME TEXT)");
}
Sign up to request clarification or add additional context in comments.

Comments

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.