简体   繁体   中英

how to select last row values of particular column sql android

i am beginner in android i am not getting the concept where am i wrong,what are my mistakes.

i am trying to get last edited message from user or last saved message and when user click on send button it need to go to particular whats app number with current location of the user

my data base name is mydb and my table name is Mytable i have two column first is id auto increment and second column msg

but when i run my code this gives me output as below

//MY OUTPUT IS //

EMERGENCY

null

http://maps.google.com/maps?saddr=12.88758733,74.83737172

i am getting the null value instead of last message saved. please guide me, i am in new in android

thankyou

//MY MAIN ACTIVITY//   
package com.myproject.harjinder_singh_lohia_bobby.harjicateringapp;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
  SQLiteDatabase db;
  String ms,msgg;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db=openOrCreateDatabase("mydb",MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Mytable(id INTEGER PRIMARY KEY AUTOINCREMENT,msg VARCHAR(100));");
}
public void onclicksavemsg(View view)
{
    EditText tempmsg =(EditText) findViewById(R.id.etmsg);
    String name=tempmsg.getText().toString();
    db.execSQL("INSERT INTO Mytable(msg)VALUES('"+name+"');");
    TextView tv=findViewById(R.id.tvv);
    Cursor last_msg = db.rawQuery("SELECT * FROM Mytable WHERE id = '"+db.rawQuery("SELECT MAX(id) FROM mytable",null)+"'",null);
    if(last_msg.moveToLast())
    {
        msgg=last_msg.getString(1);
    }
    tv.setText(msgg);
}

public void send(View view)
{
    GPStracker g = new GPStracker(getApplicationContext());
    Location l = g.getLocation();
    if (l != null) {
        double lat = l.getLatitude();
        double lon = l.getLongitude();
        String message = "\nhttp://maps.go"+"ogle.com/maps?saddr=" + lat + "," + lon;

        //String selectQuery = "SELECT * FROM Mytable ORDER BY column DESC LIMIT 1";
       Cursor last_msg = db.rawQuery("SELECT msg FROM Mytable WHERE id = '"+db.rawQuery("SELECT MAX(id) FROM mytable",null)+"'",null);
        if(last_msg.moveToLast())
       {
           ms=last_msg.getString(0);

       }
        String num = "xxxxxxxxxx";
        Intent sendIntent = new Intent("android.intent.action.MAIN");
        sendIntent.setAction(Intent.ACTION_VIEW);
        sendIntent.setPackage("com.whatsapp");
        String url = "https://api.whatsapp.com/send?phone=" + num + "&text="+"\nEMERGENCY\n"+ms;
        sendIntent.setData(Uri.parse(url+message));
        if (sendIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
            startActivity(sendIntent);
        }
    }

}

}

//I WANT MY OUTPUT TO BE LIKE

EMERGENCY

SAVE ME I AM IN DANGER HELP ME

http://maps.google.com/maps?saddr=12.88758733,74.83737172

You are doing the same mistake twice in your code:

Cursor last_msg = db.rawQuery("SELECT * FROM Mytable WHERE id = '"+db.rawQuery("SELECT MAX(id) FROM mytable",null)+"'",null);

You can't add the result of 2 rawQuery() methods (meaning add 2 Cursor objects).
Change the 1st statement like this:

Cursor last_msg = db.rawQuery("SELECT * FROM Mytable WHERE id = (SELECT MAX(id) FROM mytable)", null);

and the 2nd to:

Cursor last_msg = db.rawQuery("SELECT msg FROM Mytable WHERE id = (SELECT MAX(id) FROM mytable)", null);

Change your query to this raw-query,

select max(id) as id from your_table_name

Since you are getting only one value from your query, you dont need to call,

if(last_msg.moveToLast())  
 { msgg=last_msg.getString(1);   
}   
tv.setText(msgg);  

Just use ,

cursor.movetoposition(0);  
    msgg=cursor.getString(1);
    tv.setText(msgg);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM