gpt4 book ai didi

android - SQLite 查询以创建定义的 Java 对象

转载 作者:太空狗 更新时间:2023-10-29 12:50:36 33 4
gpt4 key购买 nike

我有这个类型的 SQLite 表

table 车辆:

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 方法来解决我的问题。

最佳答案

从 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 个参数的对象,但您应该能够更改代码 fragment 以使其适合您。

关于android - SQLite 查询以创建定义的 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12408698/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com