找不到SQLite android列

时间:2012-10-01 18:21:07

标签: android database sqlite

我的代码是正确的假设,但即使它为_hour&找不到错误coloumn _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;
    }
}

,数据库类是:

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);        
    }
}

SQL exec语句是否有任何错误意味着任何空间或什么?

1 个答案:

答案 0 :(得分:2)

看起来您在运行应用程序至少一次后更改了DBHandler#onCreate()内的表架构。如果是这样,您需要增加DB_VERSION = 2

数据库不会检查onCreate()内的代码是否有自己的新架构,您需要告诉数据库在完成更改后查找更改。最简单的方法是增加DB_VERSION