0

我的代码有问题:

public class DBAdapter {

static final String TAG = "DBAdapter";

static final String KEY_ID = "_id";
static final String KEY_NAME = "name";
static final String KEY_ADDRESS = "formatted_address";
static final String KEY_TYPE = "type";
static final String KEY_LAT = "lat";
static final String KEY_LON = "lon";

static final String DATABASE_NAME = "MyDB";
static final String DATABASE_TABLE = "places";
static final int DATABASE_VERSION = 1;

static final String DATABASE_CREATE = "Create table places (_id integer primary key autoincrement, name text, formatted_address text, type text, lat text, lon text);";

final Context context;

DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context context){
    this.context = context;
    DBHelper = new DatabaseHelper(context);
}

//open database
public DBAdapter open() throws SQLException{
    db = DBHelper.getWritableDatabase();
    return this;
}

//close database
public void close(){
    DBHelper.close();
}

//get all places in database
public Cursor getAll(){
    return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
            KEY_LAT, KEY_LON}, null, null, null, null, null);
}

//get a place by id
public Cursor getPlaceById(String id) throws SQLException{
    Cursor cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
            KEY_LAT, KEY_LON}, KEY_ID + "=" + id, null, null, null, null, null);
    if(cursor != null){
        cursor.moveToFirst();
    }
    return cursor;
}

//get places by type
public Cursor getPlaceByType(String type) throws SQLException{
    Cursor cursor = null;
    try{
        cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
                KEY_LAT, KEY_LON}, KEY_TYPE + "=" + type, null, null, null, null, null);
        if(cursor != null){
            cursor.moveToFirst();
        }
        return cursor;
    }catch(Exception e){
        e.printStackTrace();
    }
    return cursor;
}

private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try{
            db.execSQL(DATABASE_CREATE);
        }catch(SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.w(TAG, "Upgrade database from version " + oldVersion + "to version " + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS places");
        onCreate(db);
    }

}

}

public class Database {

private static DBAdapter da;

public Database(Context context){
    da = new DBAdapter(context);
}

/**
 * Get ArrayList of Places from database
 * 
 * @param Cursor cursor
 * @return ArrayList<Place> arrPlaces 
 */
private ArrayList<Place> getArrayListPlace(Cursor cursor){
    ArrayList<Place> arrPlaces = new ArrayList<Place>();
    try{
        if(cursor.moveToFirst()){
            do{ 
                Place place = getPlace(cursor);
                arrPlaces.add(place);
            }while(cursor.moveToNext());
        }
    }catch(Exception e){
        e.printStackTrace();
    }   
    return arrPlaces;
}

/**
 * Get Place from database
 * 
 * @param Cursor cursor
 * @return Place place
 */
private Place getPlace(Cursor cursor){
    Place place = new Place();
    place.set_id(cursor.getString(0));
    place.set_name(cursor.getString(1));
    place.set_type(cursor.getString(2));
    place.set_address(cursor.getString(3));
    place.set_lat(cursor.getString(4));
    place.set_lon(cursor.getString(5));
    return place;
}

/**
 * Get all list places from database
 * 
 * @return ArrayList<Place>
 */
public ArrayList<Place> getAll(){
    da.open();
    Cursor cursor = da.getAll();
    ArrayList<Place> arrPlaces = getArrayListPlace(cursor); 
    da.close();
    return arrPlaces;
}

/**
 * Get place by id from database
 * 
 * @return Place place
 */
public Place getPlaceById(String id){
    da.open();
    Cursor cursor = da.getPlaceById(id);
    Place place = getPlace(cursor);
    da.close(); 
    return place;
}

/**
 * Get list of places by type from database
 * 
 * @return ArrayList<Place>
 */
public ArrayList<Place> getPlaceByType(String type){
    da.open();
    Cursor cursor = da.getPlaceByType(type);
    ArrayList<Place> arrPlaces = getArrayListPlace(cursor); 
    da.close();
    return arrPlaces;
}

有2个错误,其中:

//Get Place to show in ListView
    String type = spType.getSelectedItem().toString();
    Database db = new Database(this);
    try{
        arrPlaces = db.getPlaceByType(type);//Can not get data
    }catch(Exception e){
        e.printStackTrace();
    }

    /*
     *  ERROR CODE
     *  Can not show places in ListView
     */
    int length = arrPlaces.size();
    if(length > 0){
        for(int i=0; i<length; i++){
            Place place = arrPlaces.get(i);
            lstID.add(place.get_id());
            lstName.add(place.get_name() + "\n" + place.get_address());
        }


        ArrayAdapter<String> adapterName = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lstName);
        adapterName.setNotifyOnChange(true);
        lstLocation.setAdapter(adapterName);    
    }
  1. 当我尝试按类型获取数据时出错:

    android.database.sqlite.SQLiteException:在“Places”附近:语法错误:,编译时:SELECT DISTINCT _id,name,type,formatted_address,lat,lon FROM places WHERE type=My Places

  2. 我无法在 ListView 中显示数据。我错误。

我非常感谢您能提供的任何帮助。

4

1 回答 1

0

您在查询中缺少引号。当您使用字符串而不是数字时,您需要将其括在引号中。Youe 查询代码应如下所示(注意添加的 `):

        cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS, 
            KEY_LAT, KEY_LON}, KEY_TYPE + "='" + type + "'", null, null, null, null, null); 

至于你的清单,你用findViewById设置了lstlocation吗?如果你这样做了,你没有在你发布的代码中显示它......

于 2012-06-17T15:26:15.217 回答