-2

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

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

}

4

2 回答 2

2

您正在查询作为数据库event_db名称的表,而不是您的

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

你应该查询_table

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

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

于 2012-09-29T21:58:53.670 回答
2

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

于 2012-09-29T22:00:44.060 回答