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
230 views
in Technique[技术] by (71.8m points)

android - SQL table not found exception

i have made a table and its fields but i get error that no such row exist and if i comment them out then it is not detecting the table also showing no such table exist.Here's the code:

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

and the code for my other acitvity is:

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

}

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You are using "type" on your contentvalue but your column is named: "_type".


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

...