SQL表未找到异常

时间:2012-09-29 21:43:14

标签: android sqlite runtime-error

我已经创建了一个表及其字段但是我得到错误,没有这样的行存在,如果我将它们注释掉,那么它不会检测到该表也显示没有这样的表存在。这是代码:

package com.example.ifest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

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" ;


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

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

我的其他活动的代码是:

public class ProfileView extends ListActivity{

String e,e1;
static int p = 0;
Spinner spn ;
Button b1,b2;
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);
        b2.setOnClickListener(new View.OnClickListener() {

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

protected void addDB(String name,int id) {
    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");
    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 "+ "event_db",null);
        c.moveToFirst();
        while(c.moveToNext()){
            list.add(c.getString(1));
            Log.d("cursor", c.getString(1));
        }
    }
}

@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);
        b2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                build.dismiss();
                list.add(et.getText().toString());
                addDB(et.getText().toString(),spn.getLastVisiblePosition());
                adapter.notifyDataSetChanged();
                setListAdapter(adapter);
            }
        });
        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;
}

String e,e1; static int p = 0; Spinner spn ; Button b1,b2; 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); b2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { build.dismiss(); list.add(et.getText().toString()); addDB(et.getText().toString(),spn.getLastVisiblePosition()); adapter.notifyDataSetChanged(); setListAdapter(adapter); } }); } } protected void addDB(String name,int id) { 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"); 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 "+ "event_db",null); c.moveToFirst(); while(c.moveToNext()){ list.add(c.getString(1)); Log.d("cursor", c.getString(1)); } } } @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); b2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { build.dismiss(); list.add(et.getText().toString()); addDB(et.getText().toString(),spn.getLastVisiblePosition()); adapter.notifyDataSetChanged(); setListAdapter(adapter); } }); 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; }

2 个答案:

答案 0 :(得分:1)

您正在查询{strong}数据库的名称event_db表,而不是表格

Cursor c = db.rawQuery("SELECT * FROM "+ "event_db",null);

您应该查询_table

Cursor c = db.rawQuery("SELECT * FROM "+ "_table",null);

顺便说一下,如果你命名数据库db(我怀疑你还需要不止一个数据库)和你的表_event(你可以有很多表,你应该命名)会更清楚他们的角色而不仅仅是_table

答案 1 :(得分:1)

您在contentvalue上使用“type”,但您的列名为:“_ type”。