Click here to Skip to main content
15,884,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hai,

How can i solve this error. I'm new in android apps. please help.

my code is like

Java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_USERNAME = "username";
    public static final String KEY_PWDL = "pwdl";
    public static final String KEY_PWDD = "pwdd";
    public static final String KEY_ROLE = "role";
    
    private static final String TAG = "DBAdap";	
    private static final String DATABASE_NAME = "Paydb";
    private static final String DATABASE_TABLE = "table1";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_CREATE = "create table table1 (_id integer primary key, " + "username text not null,"+ " pwdl text not null, "
    		+ "pwdd text not null,"+  "role integer not null);"; 
    
    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    public DBAdapter(Context ctx) 
    {
    	this.context = ctx;
    	DBHelper = new DatabaseHelper(context);
    }
    public static class DatabaseHelper extends SQLiteOpenHelper 
        {
    	DatabaseHelper(Context context) {
    		super(context, DATABASE_NAME, null, DATABASE_VERSION);
    	}
    	@Override
    	public void onCreate(SQLiteDatabase db) 
    	{
    		db.execSQL(DATABASE_CREATE);
    	}
    	@Override
    	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
    	+ newVersion + ", which will destroy all old data");
    		db.execSQL("DROP TABLE IF EXISTS table1");
    		onCreate(db);
    	}
    }
    
    // ---opens the database---
    public DBAdapter open() throws SQLException 
    {
    	db = DBHelper.getWritableDatabase();
    	return this;
    }
    
    // ---closes the database---
    public void close() 
    {
    	DBHelper.close();
    }
    // ---insert a table1into the database---
    public long insertUser(Integer id, String name, String pwdl,String pwdd,Integer role) 
    {
    	ContentValues initialValues = new ContentValues();
    	initialValues.put(KEY_ROWID, id);
    	initialValues.put(KEY_USERNAME, name);
    	initialValues.put(KEY_PWDL, pwdl);
    	initialValues.put(KEY_PWDD, pwdd);
    	initialValues.put(KEY_ROLE, role);
    	return db.insert(DATABASE_TABLE, null, initialValues);
    }
    
	// ---retrieves a particular table1--- 

//In here I'm passing an integer variable and this function working smoothly
    public Cursor getUser1(Integer Role) throws SQLException
    {
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_PWDL, KEY_PWDD,KEY_ROLE },  KEY_ROLE +"="+ Role, null, null, null, null, null);

        if (mCursor != null)
                {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
 
//in here I'm passing a string variable and this function find the error.
    public Cursor getUser2(String user) throws SQLException
    {
                Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_PWDL, KEY_PWDD,KEY_ROLE },  KEY_USERNAME +"=" + user, null, null, null, null, null);
        if (mCursor != null)
                {
            mCursor.moveToFirst();
        }
        return mCursor;
    }


}


regards
vani
Posted
Updated 30-Oct-14 1:47am
v2
Comments
Richard MacCutchan 30-Oct-14 7:45am    
What is the error and where exactly does it occur, in this code or in the db.query method?
Vani.VN 31-Oct-14 5:47am    
the error coming in the db.query method. I believe the problem has to do with the double-quotes in the below code:
"
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_PWDL, KEY_PWDD,KEY_ROLE }, KEY_USERNAME +"=" + user, null, null, null, null, null);
"
please give me correct code.
Richard MacCutchan 31-Oct-14 5:54am    
Use your debugger (or add some code) to capture the exact, complete string, rather than all these macro values.
JoCodes 30-Oct-14 16:14pm    
Post the error log you are getting and may be only the relevant code block from where error generates.

1 solution

Try something like:
Java
public Cursor getUser2(String user) throws SQLException
    Cursor cursor = null;
    String[] columns = null; // select all columns
    String selection = KEY_USERNAME + "=?";
    String[] selectionArgs = new String[1];
    selectionArgs[0] = user;
    String groupBy = null;
    String having = null;
    String orderBy = null;
    cursor = db.query(DATABASE_TABLE,
    		columns,
    		selection, 
    		selectionArgs,
    		groupBy,
    		having,
    		orderBy
    		);
    
    return cursor;
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900