Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

database - SQLite android column not found

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...