0
public void onClick(View v) {
     if (v==showids){
    String query="SELECT  * FROM " + Tablestud + " WHERE "
                        + "ID" + " = " + studid;
                String name;
                SQLiteDatabase db= controller4.getReadableDatabase();
                Cursor cursor=db.rawQuery(query,null);
                cursor.moveToNext();
                name=cursor.getString(1);
                studn.setText(name);
                cursor.moveToLast();
                db.close();
                Toast.makeText(getApplicationContext(), "Succesfully Name Display", Toast.LENGTH_SHORT).show();
            }
        }

I Have Problem in my Where clause or in Cursor?

studn (TEXTVIEW) not responding not displaying.

package afinal.androidsql.com.afinal;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class borrow extends AppCompatActivity implements View.OnClickListener{
    DB_controller controller4;
    EditText studid;
    Button showids;
    TextView studn;
    private static final String Tablestud="stud";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_borrow);
        controller4 =new DB_controller(getApplicationContext());
        studid = findViewById(R.id.studid);
        showids =findViewById(R.id.showids);
        showids.setOnClickListener(this);
        studn = findViewById(R.id.studn);
    }


    @Override
    public void onClick(View v) {
        if (v==showids){
            String query="SELECT  * FROM " + Tablestud + " WHERE "
                    + "ID" + " = " + studid ;
            String name;
            SQLiteDatabase db= controller4.getReadableDatabase();
            Cursor cursor=db.rawQuery(query,null);
            if (cursor.moveToNext()) {
                name=cursor.getString(1);
                studn.setText(name);
            }
            db.close();
            Toast.makeText(getApplicationContext(), "Succesfully Data Recorded!", Toast.LENGTH_SHORT).show();
        }
    }
}

DB_controller.java

package afinal.androidsql.com.afinal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.Editable;
import android.widget.TableLayout;
import android.widget.TableRow;

public class DB_controller extends SQLiteOpenHelper {

    private static final String Tablebook="book";
    private static final String Tablestud="stud";
    public DB_controller(Context context) {
        super(context,"lib.db", null, 1);


    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE stud( sid INTEGER PRIMARY KEY ,sname TEXT UNIQUE,sgender TEXT, scay TEXT);");
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

0

The SELECT statement seems ok if Tablestud is the name of the table, ID is the name of the column and studid is a valid integer value. The problem could be here:

cursor.moveToNext();

moveToNext() returns true or false if there is or not a next row but you don't make this check:

if (moveToNext()) {
    name=cursor.getString(1);
    studn.setText(name);
}

Of course if there are no rows returned then studn.setText(name); will not be executed.
You don't need cursor.moveToLast();
EDIT I see in the code you posted that the name of the column is sid and not ID so make this change:

String query="SELECT  * FROM " + Tablestud + " WHERE sid = " + studid;
forpas
  • 160,666
  • 10
  • 38
  • 76
0

The problem is you are running the db query in the UI thread. The UI thread is getting blocked because of it and hence your application is not responding (ANR error)

You need to run it in the background thread. For the starter, use AsyncTask to do so.

Refer this and this

Binary Baba
  • 1,953
  • 2
  • 16
  • 23
0

I believe that you have a few issues.

Issue 1.

The first issue is that you are using the object studid (an EditText object) as the search argument rather than the data that has been entered into the EditText.

  • you are effectively saying SELECT * FROM stud WHERE ID = android.support.v7.widget.AppCompatEditText@53465a40 ) instead of SELECT * FROM stud WHERE ID = whatever_the_user_has_entered)

  • whatever_the_user_has_entered represetns a value that the user enters into the studin editText.

  • The syntax error is because of the period/full stop as SQlite as a period/fill stop not enclosed indicates a prefix and the most you could have is 2 (schema.table.column) and there are 4.

You would get a crash along the lines of :-

10-28 19:58:41.795 1681-1681/so53029037.so53029037 E/AndroidRuntime: FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: SELECT  * FROM stud WHERE sid = android.support.v7.widget.AppCompatEditText@53465a40
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
        at so53029037.so53029037.borrow.onClick(borrow.java:39)
        at android.view.View.performClick(View.java:4084)
        at android.view.View$PerformClick.run(View.java:16966)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

You need to get the data entered via the object's getText method and then convert the returned CharSequence to a String via the CharSequence toString method.

As such, the fix for this issue is to not use .... + studid but to instead use .... + studid.getText().toString() to get the value that has been entered into the ExitText.

  • (.... represents other code not shown for brevity)

Issue 2 (Maybe).

The second issue (potential issue) is that if the user enters a non-numeric value then the App will crash due to an SQLite syntax error e.g. if the user entered A then :-

10-28 19:52:10.031 1604-1604/so53029037.so53029037 E/AndroidRuntime: FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: no such column: A (code 1): , while compiling: SELECT  * FROM stud WHERE sid = A

As such it would be safer to enclose the argument being searched for in single quotes.

Issue 3.

The last issue is that you are searching for a column that doesn't exist. That is, you have WHERE ID = .... when the column name has been defined as sid.


As such the complete fix could be to use :-

String query="SELECT  * FROM " + Tablestud + " WHERE "
        + "sid" + " = '" + studid.getText().toString() + "'" ;
  • ID has been changed to sid.
  • studid has been changed to studid.getText().ToString().

    - The result from studid.getText.toString() has been wrapped/enclosed in single quotes.

MikeT
  • 51,415
  • 16
  • 49
  • 68