尝试显示SQLite数据库时“没有这样的表错误”

时间:2012-12-06 03:15:15

标签: sqlite

我试图在一个活动中显示我的SQlite数据库,它告诉我“没有这样的表:sfdnull”。我知道该表存在是因为我可以在我的DDMS上看到它。我的代码在下面是我的dbhelper类和我的viewswimmerprofile类,它用于显示表“sfd”。我希望有人有一些建议!

public class dbhelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "swimmers";
    public static final String TABLE_SWIMMERS = "sfd";

    public static final String C_ID = "id";
    public static final String NAME = "name";
    public static final String TEAM = "team";
    public static final String NOTES = "notes";

    public static final int VERSION = 1;

    public dbhelper(Context context)
    {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate (SQLiteDatabase db) 
    {
        String createdb = "create table " + TABLE_SWIMMERS + "("
                + C_ID + " integer primary key autoincrement, "
                + NAME + " text, "
                + TEAM + " text, "
                + NOTES + " text); ";

        db.execSQL(createdb);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        db.execSQL("drop table " + TABLE_SWIMMERS);
        onCreate(db);
    }

    //add new entry
    void addSwimmer(Swimmer swimmer){

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(NAME, swimmer.getname());
        values.put(TEAM, swimmer.getteam());
        values.put(NOTES, swimmer.getnotes());

        db.insert(TABLE_SWIMMERS, null, values);
        db.close();
    }

    //Getting single swimmer
    Swimmer getSwimmer(int id){
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_SWIMMERS, new String[] { C_ID, NAME, TEAM, NOTES}, C_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor !=null)
            cursor.moveToFirst();

        Swimmer swimmer = new Swimmer (Integer.parseInt(cursor.getString(0)), 
                cursor.getString(1), cursor.getString(2), null);
        return swimmer;
    }

    public int updateSwimmer(Swimmer swimmer){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(NAME, swimmer.getname());
        values.put(TEAM, swimmer.getteam());
        values.put(NOTES, swimmer.getnotes());

        return db.update(TABLE_SWIMMERS, values, C_ID + " = ?", new String[] { String.valueOf(swimmer.getID()) });        
    }

    //delete single contact
    public void deleteSwimmer(Swimmer swimmer){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_SWIMMERS, C_ID + " = ?", new String[] { String.valueOf(swimmer.getID()) });        
        db.close();
    }

    //get contacts count
    public int getSwimmersCount(){
        String countQuery = "SELECT  * FROM " + TABLE_SWIMMERS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        return cursor.getCount();
    }

    public List<Swimmer> getAllSwimmers() {
        List<Swimmer> swimmerList = new ArrayList<Swimmer>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SWIMMERS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Swimmer swimmer = new Swimmer();
                swimmer.setID(Integer.parseInt(cursor.getString(0)));
                swimmer.setname(cursor.getString(1));
                swimmer.setnotes(cursor.getString(2));
                // Adding swimmer to list
                swimmerList.add(swimmer);
            } while (cursor.moveToNext());
        }

        // return swimmer list
        return swimmerList;
    }

}

public class ViewSwimmerProfile extends ListActivity {

private ArrayList<String> results = new ArrayList<String>();
 private String TABLE_SWIMMERS = dbhelper.TABLE_SWIMMERS;
 private SQLiteDatabase newDB;

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{       
        getMenuInflater().inflate(R.menu.activity_sfdmain, menu);
        return true;
    }

@Override
public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.i("viewProfileActivity", "Intent Text");
    setContentView(R.layout.viewprofile);
    openAndQueryDatabase();
    displayResultList();
}   

    private void displayResultList() {

                setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, results));
                getListView().setTextFilterEnabled(true);
         }

    private void openAndQueryDatabase() {
          try {
           dbhelper dbHelper = new dbhelper(this.getApplicationContext());
           newDB = dbHelper.getWritableDatabase();
           Cursor c = newDB.rawQuery("SELECT NAME, TEAM FROM " +
                TABLE_SWIMMERS +
                null, null);

              if (c != null ) {
               if  (c.moveToFirst()) {
                do {
                 String NAME = c.getString(c.getColumnIndex("NAME"));
                 String TEAM = c.getString(c.getColumnIndex("TEAM"));
                 results.add("NAME: " + NAME + ",TEAM: " + TEAM);
                }while (c.moveToNext());
               } 
              }   
          } catch (SQLiteException se ) {
                 Log.e(getClass().getSimpleName(), "Could not create or Open the database");
                } finally {
                 if (newDB != null) 
                  newDB.execSQL("DELETE FROM " + TABLE_SWIMMERS);
                  newDB.close();
                }

         }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

错误消息告诉您它尝试从表sfdnull读取,而不是sfd

这可能来自此代码,您在表名中追加null

Cursor c = newDB.rawQuery("SELECT NAME, TEAM FROM " +
     TABLE_SWIMMERS +
     null, null);