3

我有这个提示的 SQLite 表

CATEGORY    COUNTRY   ID    NAME          EMAIL
A           GE         1    BMW           sample1@salple.it
A           GE         2    Lamborghini   sample2@salple.it
B           GE         3    BMW           sample3@salple.it

我想选择所有具有指定名称或指定类别的条目,并将所有参数传递给构造函数中的每一行

Vehicle(String category, String country, int id, String name, String email)

我已经使用一些教程实现了这个适配器:

public class TestAdapter  
{ 
    protected static final String TAG = "DataAdapter"; 

    private final Context mContext; 
    private SQLiteDatabase mDb; 
    private DataBaseHelper mDbHelper; 

    public TestAdapter(Context context)  
    { 
        this.mContext = context; 
        mDbHelper = new DataBaseHelper(mContext); 
    } 

    public TestAdapter createDatabase() throws SQLException  
    { 
        try  
        { 
            mDbHelper.createDataBase(); 
        }  
        catch (IOException mIOException)  
        { 
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase"); 
            throw new Error("UnableToCreateDatabase"); 
        } 
        return this; 
    } 

    public TestAdapter open() throws SQLException  
    { 
        try  
        { 
            mDbHelper.openDataBase(); 
            mDbHelper.close(); 
            mDb = mDbHelper.getReadableDatabase(); 
        }  
        catch (SQLException mSQLException)  
        { 
            Log.e(TAG, "open >>"+ mSQLException.toString()); 
            throw mSQLException; 
        } 
        return this; 
    } 

    public void close()  
    { 
        mDbHelper.close(); 
    } 



    public boolean SaveVehicles(String category , String country, String id, String name, String email) 
    {
        try
        {
            ContentValues cv = new ContentValues();
            cv.put("Category", category);
            cv.put("Country", country);
            cv.put("id", id);
            cv.put("Name", name);
            cv.put("Email", email);

            mDb.insert("Vehicles", null, cv);

            Log.d("SaveVehicles", "informationsaved");
            return true;

        }
        catch(Exception ex)
        {
            Log.d("SaveVehicles", ex.toString());
            return false;
        }
    }


} 

但我不知道如何实现我需要的各种 get 方法来解决我的问题。

4

2 回答 2

4

从 SQL 查询创建对象看起来像这样

/**
 * @return Returns a list of all objects.
 */
public ArrayList<Object> getAllObjects() 
{
    // Select All Query
    String selectQuery = "SELECT * FROM SOME_TABLE";
    // Get the isntance of the database
    SQLiteDatabase db = this.getWritableDatabase();
    //get the cursor you're going to use
    Cursor cursor = db.rawQuery(selectQuery, null);

    //this is optional - if you want to return one object
    //you don't need a list
    ArrayList<Object> objectList = new ArrayList<Object>();

    //you should always use the try catch statement incase
    //something goes wrong when trying to read the data
    try
    {           
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                 //the .getString(int x) method of the cursor returns the column
                 //of the table your query returned
                Object object= new Object(Integer.parseInt(cursor.getString(0)),
                                        Integer.parseInt(cursor.getString(1)),
                                        Integer.parseInt(cursor.getString(2)),
                                        cursor.getString(3),
                                        cursor.getString(4),
                                        cursor.getString(5),
                                        Boolean.parseBoolean(cursor.getString(6))
                                        );                                      
                // Adding contact to list
                objectList.add(object);
            } while (cursor.moveToNext());
        }
    }
    catch (SQLiteException e)
    {
        Log.d("SQL Error", e.getMessage());
        return null;
    }
    finally
    {
      //release all your resources
        cursor.close();
        db.close();
    }
    return objectList;      
}

上面的代码假设您的数据库中有一个名为“SOME_TABLE”的表,并且您有一个带有 7 个参数的对象,但您应该能够更改代码片段以使其适合您。

于 2012-09-13T15:26:05.890 回答
0

您需要查询数据库中的数据,然后遍历返回的游标以提取您需要的数据并将其放入字符串中以提供给您的构造函数。

查询看起来像这样(使用您提供的信息和query方法):

public Cursor fetchList(String category) {
    return mDb.query("Vehicles", new String[] { "CATEGORY", "COUNTRY", "ID", "NAME", "EMAIL" }, "Category =" + category,
            null, null, null, null);
}

请注意,这是一个基本查询,会受到 SQL 注入攻击,应该对其进行参数化以使其不易受到攻击,除非您不允许用户输入类别,而是让他们从您提供的列表中进行选择。

无论如何,这将在游标中返回您的数据,每条记录与搜索参数匹配一行。从那里,您需要遍历返回的光标并将数据从中提取出来并放入您可以使用的字符串中。

于 2012-09-13T15:08:11.043 回答