Reputation: 47
I'm starting my first trip into sqlite databases in an Android app, so far I can add to a database, I can update an entry in a database and I can display every bit of data in a database, but what I'm stuck with is how to get one entry (well one id and all its associated rows).
To retrieve all the rows I use this code
Database helper:
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+table_name,null);
return res;
}
and to call it in my activity class
public void viewallFunction() {
Cursor res = characterDB.getAllData();
if(res.getCount() == 0) {
showmessage("Error","no Data");
return;
} else {
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Internal ID : " + res.getString(0) + "\n");
buffer.append("Player Name : " + res.getString(1) + "\n");
buffer.append("Character Name : " + res.getString(2) + "\n");
buffer.append("NickName : " + res.getString(3) + "\n");
buffer.append("Alignment : " + res.getString(4) + "\n");
buffer.append("Race : " + res.getString(5) + "\n");
}
showmessage("Data", buffer.toString());
}
}
when called this works great (there's actually 81 entries so res.getString(0) all the way to res.getString(80) the functions trimmed for this post)
From what I understand the *
in:
Cursor res = db.rawQuery("select * from " + table_name, null);
means all columns.
So I created this method in my database helper :
public Cursor getOneData(int id) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select "+id+" from "+table_name,null);
return res;
}
and called it with
public void getOneId(int id) {
Cursor res = characterDB.getOneData(id);
if(res.getCount() == 0){showmessage("Error","no Data");
return;}
else{
showmessage("test","retreived id "+res.getString(0)+" which is "+res.getString(1));
displaySheet(res.getString(0),
res.getString(1),
res.getString(2),
res.getString(3),
res.getString(4),
res.getString(5),
);}
}
Again that's trimmed for this post, display sheet then deals with the data, however nothing happens, i do not get either of the messages (which I'm sure is an impossibility, one or the other should display!) I do not get any errors either, just nothing happens, at first i thought the method was not even firing but I rewrote it to:
public void getOneId(int id){
showmessage("Message","id is "+id);
}
and the test message showed perfectly
It has me completely stumped, a few hours of googling has got me more confused, each example i see is tailored very specifically to the app being made and looks nothing remotely like my code, which if the all display method didn't work I would say ah ha there's my problem, but my all display method does work...and well that's how I have learned it over the last 3 or 4 days (oh god please tell me I'm not doing it wrong)
So yeah what am I doing wrong?
How am I meant to retrieve one row as a set of strings?
For reference the first column in the table is set as
ID INTEGER PRIMARY KEY AUTOINCREMENT
not sure if that's relevant or not though
EDIT: the database is structured like so:
1 "david" "zarr" "the great" "choatic" "human"
2 "george" "tod" "toddy" "lawful" "elf"
ie:
int string string string string string
The column names being :
ID PLAYERNAME CHARNAME NICKNAME RACE
The goal I'm trying to achieve is to use that id row to retrieve the strings (which I then change edittext's to, that part I know, but it's getting those strings that I can't suss out)
Ideally I just want to say to the database, give me all the strings from the row with id 1 and the database to say here you are their "david" "zarr" "the great" "choatic" "human"
ok the answer came from Pitty
A change of the database geto one method to :
public Cursor getOneData(int id){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+table_name+" WHERE ID='" + id + "'",null);
return res;
}
and a change of the method i was using to call it :
public void getOneId(int id){
Cursor res = characterDB.getOneData(id);
if(res.getCount() == 0){showmessage("Error","no Data");
return;}
else{
if (res != null && res.moveToFirst()); do {
showmessage("test","retreived id "+res.getString(0)+" which is "+res.getString(1));
displaySheet(res.getString(0),
res.getString(1),
res.getString(2),
res.getString(3),
res.getString(4),
res.getString(5),
); } while (res.moveToNext()); }
}
Upvotes: 0
Views: 2309
Reputation: 1997
You are fetching Id column in your query and you want entire one row for that Use this :
public Cursor getOneData(int id){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+table_name+" WHERE id='" + id + "'",null);
if (res != null && res.moveToFirst()) {
do {
//Call Your Function
}
while (res.moveToNext());
}
return res;
}
Upvotes: 1