I am developing a app in android and here i want to fetch data from table according to rowId and subjectId but I am getting this error,
The method query(boolean, String, String[], String, String[], String, String, String, String) in the type SQLiteDatabase is not applicable for the arguments
(boolean, String, String[], String, String, null, null, null, null)
And the here's that query,
public Cursor getText(long rowId, long subId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_id, KEY_ques,KEY_correctans,KEY_wrongans1,KEY_wrongans2,KEY_wrongans3,KEY_subject,KEY_subjectid }, KEY_id + "="
+ rowId, KEY_subjectid + "=" + subId,null,null, null,null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
I want to fetch table data as where subId = KEY_subjectid and rowId = KEY_id
If any thing is not clear in this question please ask me. And if its clear please help me..
Thanks in Advance.
Change the query to following:
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_id, KEY_ques,KEY_correctans,KEY_wrongans1,KEY_wrongans2,KEY_wrongans3,KEY_subject,KEY_subjectid }, KEY_id + "="
+ rowId + " AND " + KEY_subjectid + "=" + subId,null, null,null, null,null);
The 5-th parameter needs to be a String array, not a string the way you have. The 4-th parameter is the WHERE SQLite clause.
Do this way
public Cursor getText(long rowId, long subId) throws SQLException
{
Cursor mCursor = db.rawQuery("select * from "+DATABASE_TABLE+" where KEY_subjectid="+subId+" and KEY_id="+rowId+"",null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
Related
I am a beginner in Android. I created a database with unique id, name, email etc. I want to retrieve the specific data from the database using the unique id. I don't know what to do. I read the previous answers but didn't get any idea from that.
At first you should read SQL Database
Below code is DEMO
public String getSpecificResult(int getID) // you should pass getID from your Class
{
SQLiteDatabase db = this.getWritableDatabase();
String get_Status = "0";
String selectQuery = "SELECT your_coloum_ FROM "+ TABLE_NAME + " WHERE " + YOUR_UNIQUE_KEY + " = '"+ getID +"'";
Cursor c = db.rawQuery(selectQuery, null);
if (c != null && c.moveToFirst()) {
get_Status = c.getString(0); // Return 1 selected result
}
db.close();
return get_Status;
}
Try this..
pass ur unique_id to get the contents.. alter names according to your table.
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_NUMBER}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Adapter.java
public String getID(String i) throws SQLException
{
db=DBHelper.getReadableDatabase();
String ij="No Track Found";
Cursor mCursor =
db.query(true, TABLE, new String[] {KEY_ID}, KEY_NAME + "=" + "'"+i+"'", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
if (mCursor.moveToFirst())
{
ij=mCursor.getString(0);
}
return ij ;
}
The Question is when I pass any song's name that contain "'"(e.g. alexander's blade) , it throws and error. Otherwise all fine.
You should use the selectionArgs parameter:
db.query(true, //distinct
TABLE, //table
new String[] {KEY_ID}, //columns
KEY_NAME + "=?", //selection
new String[] {i}, //selectionArgs
null, //groupBy
null, //having
null, //orderBy
null //limit
);
Using selectionArgs also help to prevent SQL Injection
You need to call your method like this:
Cursor c = bd.query(Constantes.TABELA_APLICATIVO, COLS, "urlandroid = ?", new String[]{url}, null, null, null);
The caracter "?" represents the values on Third parameter(String[]).
In my application i have to retrieve data from database to display in a listview.My requirement is i have to retrieve data from database with "groups" as household and "category" as income.
My query is as follows:
public Cursor gethouseholdTitle(String grps) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[]
{
KEY_ROWID,
KEY_INCOME,
KEY_DESC,
KEY_QUANTITY,
KEY_TOTAL,
KEY_CATEGORY,
KEY_RECURR,
KEY_DATE,
KEY_GROUP
//KEY_PUBLISHER
},
KEY_GROUP + "=" + grps,
KEY_CATEGORY + "=" + "'Income'",
null,
null,
null,
null,
null
);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
But it shows error:
The method query(boolean, String, String[], String, String[], String, String, String, String) in the type SQLiteDatabase is not applicable for the arguments (boolean, String, String[],
String, String, null, null, null, null, null).
As i new to this,please help me.
Looking at the logic, seems like you need to change
KEY_GROUP + "=" + grps, KEY_CATEGORY + "=" + "'Income'",
to
KEY_GROUP + "=" + grps + " and " + KEY_CATEGORY + "=" + "'Income'",
Hope this helps...
I got this code in one of my methods. It should check if the string is already in the database and if it is the db entry gets updated. If it is not there I catch the exception and just create the entry.
String s="hallo";
try {
Cursor c1 = dba.fetchSubject(s);
dba.updateSubject(c1.getLong(c1.getColumnIndex(DbAdapter.KEY_ROWID)), s, t, r);
} catch (SQLException e){
dba.createSubject(s, t, r);
}
The problem is fetchSubject allway throws an Exception.
I use a similar method to get a db entry by a rowId instead of a String, which is working:
public Cursor fetchSubject(long rowId) throws SQLException {
Cursor mCursor = database.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SUBJECT, KEY_TEACHER, KEY_ROOM}, KEY_ROWID
+ "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The method called on top:
public Cursor fetchSubject(String subject) throws SQLException {
Cursor mCursor = database.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SUBJECT, KEY_TEACHER, KEY_ROOM}, KEY_SUBJECT
+ "=" + subject, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The error:
sqlite returned: error code = 1, msg = no such column: hallo
I don't know where my error is. Isn't it possible to use a string at the selection parameter?
Try adding single quotes around hallo
String s = "'hallo'";
Essentially what you're doing is telling your query: select blah blah blah from TABLE where subject = hallo. Since hallo isn't surrounded by quotes, it is trying to find a variable / column with that name. When we surround it with quotes, we are saying that we are looking for the string within these quotes, rather than a variable.
Think of it like normal programming..
String s = hallo;
is completely different than
String s = "hallo";
When i try to search in row POINTA (text data type in SQLite) and I compare it to a String the program stops. This is the code:
public Cursor getpoints(String start,String end) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_PRIM,
NAME,
POINTA,
POINTA_LANG,
POINTA_LAT,
POINTB,
POINTB_LANG,
POINTB_LAT
},
POINTA +"=" +start,//here is the problem
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
...
Same problem as this, you need single quotes around your start String, i.e.
POINTA + "='" + start + "'",