0

I couldn't give a good title to the problem I'm having, but basically I have two columns Name and MaxNum string and int respectively! I need int method that makes a query which retrieves the MaxNum for specific Name that I give in parameter!

.. so if I have this data

ID | Name | MaxNum

0 | Mike | 50

1 | John | 40

2 | Jess | 30

..when I put Jess in as parameter it will return 30 !

My method so far.. but I can't use it since it doesn't return int value !

public void  maxFromName(String name){

  SQLiteDatabase db = this.getWritableDatabase();
  String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "=" + name ;
  db.execSQL(query); 
}
6
  • Just parse it using Integer.valueOf(yourvalue); Commented May 30, 2017 at 7:38
  • @Amy parse what exactly ? the method ?! Commented May 30, 2017 at 7:39
  • No.. the return value Commented May 30, 2017 at 7:39
  • well, I can't just return db.execSQL(query); it's void !? can u give me an answer so that I can try it..!? Commented May 30, 2017 at 7:41
  • Instead of using raw SQLite, I'd suggest you go for ORM like GreenDao. A little difficult to begin with, but it'll make your life much much simpler once you get used to it. Also, it makes db handling easy when your application starts to get more complex. See link for details. Commented May 30, 2017 at 7:46

5 Answers 5

4

You have to get the result using Cursor

String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "=" + name ;
Cursor c = db.rawQuery(query, null);
c.moveToFirst();   
return c.getInt(c.getColumnIndex("MaxNum"));

Update

Since you are facing the problem, I suggest you use the below query:

String query = Select MaxNum from azkar WHERE Name ='Jess';

after this use my code from Cursor c line, this will work.

Sign up to request clarification or add additional context in comments.

14 Comments

gives me this error > android.database.sqlite.SQLiteException: near "Table": syntax error (code 1): , while compiling: SELECT * FROM Table
@SamZar Try now!
why not c.getInt
Look at Amys query. There are inverted commas around name.
@NeerajJain The column iin the question s called 'Name' not ''name_col'.
|
3

Use rawQuery for getting data from SQLite. And from the cursor get integer value using getInt function.

Change your maxFromName function with the following.

public int maxFromName(String name){
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "= '" + name +"'" ;
        Cursor c = db.rawQuery(query, null);
        if(c.getCount()!=0){
            c.moveToFirst();
            return c.getInt(c.getColumnIndex("MaxNum"))
          }
    return 0;
}

7 Comments

what the selectQuery?
@AliAhsan sorry for mistake in the race forgotten to change the variable name
hehehe it happens :)
same error as @Neeraj method > android.database.sqlite.SQLiteException: no such column: Jess (code 1): , while compiling: SELECT _max as maxValue FROM azkar WHERE name_col=Jess
I have update the query copy and paste it into your project
|
2

In the API it says the following in the description of execSQL():

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

Use a query method that returns a Cursor type in order to retrieve results

Comments

2

The easiest way to get a single value from a database is with the stringForQuery() or longForQuery() helper functions:

public long maxFromName(String name) {
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT "+COLUMN_MAX+" FROM "+TABLE_AZKAR+" WHERE "+COLUMN_NAME+" = ?";
    return DatabaseUtils.longForQuery(db, query, new String[]{ name });
}

(To prevent string formatting problems and SQL injection attacks, always use parameters instead of inserting string values directly into the query.)

Comments

1

You can use rawQuery

public ArrayList<String> maxFromName(String name){

ArrayList<String> maxNums = new ArrayList<>();

SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " 
+COLUMN_NAME+ "=" + name ;

Cursor c = db.rawQuery(query, null);
c.moveToFirst();  
do { // if that kind of rows are more then one, this metod get all of them
maxNums.add(c.getInt(c.getColumnIndex("MaxNum")));
}
return maxNums;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.