[英]Storing data from an entire row, Android SQLite
So I have a method that allows me to get the id of a certain item by using a name i already have in an SQL Database. 因此,我有一种方法,可以通过使用我在SQL数据库中已有的名称来获取特定项目的ID。 How would I go about getting the entire row of information and storing each item in its own variable.
我将如何获取整行信息并将每个项目存储在其自己的变量中。
Method that only works with ID 仅适用于ID的方法
public Cursor getID(String name){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String query = " SELECT " + COL1 + " FROM " + TABLE_NAME + " WHERE " + COL2 + " = '" + name + "'";
Cursor data = sqLiteDatabase.rawQuery(query, null);
return data;
}
And the method that gets the query and stores the result 以及获取查询并存储结果的方法
Cursor data = mydb2.getID(name);
int itemId= -1;
while(data.moveToNext()){
itemId = data.getInt(0);
}
Using this method below how would i store all of the data in its own variable using this (or any other way to get data of entire row). 在下面使用此方法,我将如何使用此方法(或以其他任何方式获取整行数据的方式)将所有数据存储在其自己的变量中。
public Cursor rowData(String name){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String query = " SELECT * FROM " + TABLE_NAME + " WHERE " + COL2 + " = '" + name + "'";
Cursor data = sqLiteDatabase.rawQuery(query, null);
return data;
}
I know this might be a dumb question, and I have tried looking at other questions, I have gotten this far I just don't know what to do next (I'm very new to Sq Lite databases and Android development) 我知道这可能是一个愚蠢的问题,并且我尝试查看其他问题,但到目前为止,我只是不知道下一步该怎么做(我对Sq Lite数据库和Android开发非常陌生)
You'd use something like :- 您将使用类似:-
Cursor csr = instance_of_yourdbhelper.rowData();
while(csr.moveToNext()) {
long thisItemId = csr.getLong(csr.getColumnIndex(yourdbhelper.COL_1));
String thisName = csr.getString(csr.getColumnIndex(yourdbhelper.COL_2));
// etc for other columns
}
Notes 笔记
yourdbhelper is the class for your DatabaseHelper which is the class that extends SQLiteOpenHelper (the assumption is that the above methods are from such a class, as this is a common usage of SQLite) yourdbhelper是DatabaseHelper的类,它是扩展SQLiteOpenHelper的类(假设上述方法来自此类,因为这是SQLite的常用用法)
instance_of_yourdbhelper is the instance of the DatabaseHelper class ie you may have yourdbhelper dbhlpr = new yourdbhelper(parameters);
instance_of_yourdbhelper是DatabaseHelper类的实例,即您可能具有
yourdbhelper dbhlpr = new yourdbhelper(parameters);
, in which case dbhlpr
is the instance. ,在这种情况下,
dbhlpr
是实例。
This just overwrites the same variables (thisItemId and thisName) for each row in the cursor, although there is perhaps the likliehood that the rowData method only returns one row. 这只是为游标中的每一行覆盖了相同的变量(thisItemId和thisName),尽管rowData方法可能只返回一行。
You will likely encounter fewer errors using the getColumnIndex
method as it returns the offset according to the column name. 使用
getColumnIndex
方法可能会遇到较少的错误,因为它根据列名称返回偏移量。 Miscalculated offsets is a frequent cause of problems. 偏移量计算错误是造成问题的常见原因。
You may wish to handle a no rows returned situation in which case you can use cursor.getCount()
, which will return the number of rows in the cursor. 您可能希望处理没有返回行的情况,在这种情况下,您可以使用
cursor.getCount()
,这将返回游标中的行数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.