1

my code is correct is assume but even though it gives error coloumn not found for _hour & _min:

package com.example.ifest;


public class ProfileView extends ListActivity{

    int hour,min;
    TimePicker tp ;
    String e,e1;
    static int p = 0;
    Spinner spn ;
    Button b1,b2,b3;
    EditText et;
    String str1;
    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        list.add("Create");
        openDB();
        p++;

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        setListAdapter(adapter);      
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if(position == 0){
            final Dialog build = new Dialog(ProfileView.this);
            build.setTitle("String Name and Details");
            build.setContentView(R.layout.activity_dialog);
            build.show();
            spn = (Spinner)build.findViewById(R.id.spinner1_Dialog);
            et = (EditText) build.findViewById(R.id.editText1_Dialog);
            b2 = (Button) build.findViewById(R.id.button1_Dialog);
            tp = (TimePicker)build.findViewById(R.id.timePicker1_Dialog);
            b3 = (Button)build.findViewById(R.id.button2_Dialog);
            tp.setIs24HourView(true);
            tp.setCurrentMinute(00);

            b2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    build.dismiss();
                    hour = tp.getCurrentHour();
                    min = tp.getCurrentMinute();
                    list.add(et.getText().toString());
                    addDB(et.getText().toString(),spn.getLastVisiblePosition(),hour,min);
                    adapter.notifyDataSetChanged();
                    setListAdapter(adapter);
                }
            });

            b3.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    build.dismiss();
                }
            });
        }
    }

    protected void addDB(String name,int id,int h,int m) {
        DBHandler handle = new DBHandler(this);
        SQLiteDatabase db = handle.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("_name",name);
        if(id == 0)
            cv.put("_type","WIFI");
        else if(id == 1)
            cv.put("_type","BLUETOOTH");
        else if(id == 2)
            cv.put("_type","MEDIA");
        cv.put("_hour", h);
        cv.put("_min", m);
        db.insert("_table", null , cv);
        db.close();
    }

    private void openDB() {
        if(p != 0){ 
            DBHandler handle = new DBHandler(this);
            SQLiteDatabase db = handle.getReadableDatabase();

            Cursor c = db.rawQuery("SELECT * FROM "+ "_table",null);
            c.moveToFirst();
            while(c.moveToNext()){
                list.add(c.getString(1));
                Log.d("cursor", c.getString(1));
            }
            c.close();
            db.close();
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflate = getMenuInflater();
        inflate.inflate(R.menu.string_main,menu);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()){
        case R.id.item1:
            final Dialog build = new Dialog(ProfileView.this);
            build.setTitle("String Name and Details");
            build.setContentView(R.layout.activity_dialog);
            build.show();
            spn = (Spinner)build.findViewById(R.id.spinner1_Dialog);
            et = (EditText) build.findViewById(R.id.editText1_Dialog);
            b2 = (Button) build.findViewById(R.id.button1_Dialog);
            tp = (TimePicker)build.findViewById(R.id.timePicker1_Dialog);
            b3 = (Button)build.findViewById(R.id.button2_Dialog);
            tp.setIs24HourView(true);
            tp.setCurrentMinute(00);

            b2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    build.dismiss();
                    hour = tp.getCurrentHour();
                    min = tp.getCurrentMinute();
                    list.add(et.getText().toString());
                    addDB(et.getText().toString(),spn.getLastVisiblePosition(),hour,min);
                    adapter.notifyDataSetChanged();
                    setListAdapter(adapter);
                }
            });

            b3.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    build.dismiss();
                }
            });
            break;

        case R.id.item2:
            break;

        case R.id.item3:
            Intent i = new Intent("com.example.ifest.ABOUTUS");
            startActivity(i);
            break;

        case R.id.item4:
            finish();
            break;
        }
        return true;
    }
}

and the database class is:

public class DBHandler extends SQLiteOpenHelper{

    private static final String DB_NAME = "event_db";
    private static final int DB_VERSION = 1;
    private static final String TABLE_NAME = "_table";
    private static final String EVENT_NAME = "_name";
    private static final String EVENT_ID = "_no";
    private static final String EVENT_TYPE = "_type";
    private static final String EVENT_HOUR = "_hour";
    private static final String EVENT_MINUTE = "_min";


    public DBHandler(Context context) {
        super(context, DB_NAME, null, DB_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + EVENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
                    + EVENT_NAME + " TEXT NOT NULL, " + EVENT_TYPE + " TEXT NOT NULL, " + EVENT_HOUR +"  INTEGER, " + EVENT_MINUTE + " INTEGER " + ");");
    }

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

is there anything wrong with the SQL exec statement means any space or something?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • E/Database(423): android.database.sqlite.SQLiteException: table _table has no column named _min: , while compiling: INSERT INTO _table(_type, _name, _min) VALUES(?, ?, ?); is the error in the log – shalinshah1993 Oct 01 '12 at 18:22
  • Did you add the _hour and _min columns to your SQLiteOpenHelper class later? – Vinay S Shenoy Oct 01 '12 at 18:23
  • I think you're neglecting some code. The only insertion above includes `_hour` which was not part of your output log. – Cat Oct 01 '12 at 18:25
  • no i think i am not missing and what do u exactly mean by only insertion include _hour ? – shalinshah1993 Oct 01 '12 at 18:36
  • vinay i already have those coloumns in my db helper class – shalinshah1993 Oct 01 '12 at 18:37
  • E/Database(483): android.database.sqlite.SQLiteException: table _table has no column named _hour: , while compiling: INSERT INTO _table(_type, _name, _hour, _min) VALUES(?, ?, ?, ?); lastest error log after adding every thing in the table – shalinshah1993 Oct 01 '12 at 18:38
  • Have you changed the table schema inside `DBHandler#onCreate()` after running the app at least once? If so you need to increment `DB_VERSION = 2` to inform your database that changes were made. – Sam Oct 01 '12 at 18:38
  • ohh awsome it worked why do i need to change version every time i mean when do i need to change it? – shalinshah1993 Oct 01 '12 at 18:43

1 Answers1

3

It looks like you have changed the table schema inside DBHandler#onCreate() after running the app at least once. If so you need to increment DB_VERSION = 2.

The database won't check the code inside onCreate() for a new schema on it's own, you need to tell the database to look for changes after you have made them. The easiest way to do this is by increasing the DB_VERSION.

Sam
  • 86,580
  • 20
  • 181
  • 179