I need to pass a parameter from an EditText and when I click the button, it'll start another activity and get that parameter and pass it to a query. It follows:
final Button ara = (Button) findViewById(R.id.maddebutton);
ara.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String maddeno = madde.getText().toString(); //madde is my EditText
Intent intent = new Intent(Anayasa.this, MaddeBul.class);
intent.putExtra("maddeler", maddeno);
startActivity(intent);
}
});
my second class is as follows:
Intent intent = getIntent();
int maddebul = intent.getIntExtra("maddeler", 0); //I don't want to set a default value but it pushes me
try {
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = maddebul", null, null, null, null));
ShowRecord(cursor);
} finally {
database.close();
}
my FetchRecord(Cursor c) and ShowRecord(Cursor cursor) functions work fine, since I'm using them in other classes. There is "no" column in my "anayasa" database which holds integer values.
On LogCat, it says "no column such maddebul". It is true, there isn't. It suppose to be:
SELECT * FROM anayasa WHERE no = maddebul; //as sql command
Any help?
You are adding your extra as a String here:
intent.putExtra("maddeler", maddeno);
But when you try to retrieve the extra you are retrieving it as an int:
int maddebul = intent.getIntExtra("maddeler", 0);
Try using this insteald
String maddebul = intent.getStringExtra("maddeler", "");
For reference here are the docs for the getStringExtra() method.
On LogCat, it says "no column such maddebul". It is true, there isn't.
due to in whereClause "no = maddebul", maddebul is not a variable it is string part so change the whereClause to take it's value
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = maddebul", null, null, null, null));
should be
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = "+maddebul, null, null, null, null));
There are multiple problems:
You put maddebul as String here so need to get it like this:
String maddebul = intent.getStringExtra("maddeler", "");
Also you are forming your SQL query wrong, maddebul is passed as a String to the db, whereas you actually want to pass the value of that variable. So it could be like this instead:
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = ?",new String[]{maddebul}, null, null, null));
you are writing maddebul in Double Quotes.
Replace
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = maddebul", null, null, null, null));
By
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = "+maddebul, null, null, null, null));
Better use following
Cursor cursor = FetchRecord(db.query("anayasa", SELECT,
"no = ?",new String[]{String.valueOf(maddebul)}, null, null, null));
You are put Extra as String and geting it as Integer - maybe that's the problem?
Related
How to get specific value from DB by id.
This is my table: TABLE-RECORDS-(name of table) and KEY-ID , KEY-PRICE ... I'm trying to get KEY-PRICE by KEY-ID and can not. How to do it?
I don't know if this is exactly what you are looking for, but this is the query.
SELECT key-price FROM table-record WHERE key-id='id number you need';
// please change the column names of database if i have mistaken
public Cursor getCursor(int id) {
SQLiteDatabase db = this.getReadableDatabase();
String from[] = {"key-price"};//this is the edit1
String where = "key-id=?";//this is the edit2
String[] whereArgs = new String[]{String.valueOf(id)+""}; //this is the edit3
Cursor cursor = db.query(true, table-records, from, where, whereArgs, null, null, null, null);
return cursor;
}
//just call this function and see the magic
private int getPrice(int id) {
Cursor c = getCursor(id);
int price=-1;
if(c != null)
{
while(c.moveToNext){
//assuming price is an integer
price = c.getInt(0);//edit 4
// use these strings as you want
}
}
return price;
}
am making an application. There is a login page on which the user enters username in the edittext field. That is then passed to a function in my databasehelper class which checks the username against the entries in the table in which all the username reside.
Here is the function i wrote for the validation in my databasehelper class:
public String verify_user(String is_user)
{
String verify = " ";
String check = " ";
SQLiteDatabase fg = this.getReadableDatabase();
Cursor c = fg.query(Login_Table, null, null, null, null, null, null);
int iuser = c.getColumnIndex(colUsername);
for(c.moveToFirst() ; !c.isAfterLast();c.moveToNext())
{
verify = c.getString(iuser);
if(is_user == verify)
{
check = "Valid User!";
}
else
{
check = "Not Valid User!";
}
}
return check;
}
is_user is coming from the .xml layout file in which user enters username... when i debugged i found a strange behavior. Although in the condition is_user and verify have same values at a point but still else is executed. so me knowing the user is valid this code says Not Valid User! . Kindly guide me through. Thank You!
From a new suggestion:
String check = " ";
SQLiteDatabase fg = this.getReadableDatabase();
String[] args = new String[1];
args[0] = is_user;
Cursor c = fg.query(Login_Table, null, colUsername + " = ?", args, null, null, null);
int iuser = c.getColumnIndex(colUsername);
if(is_user.equals(c.getString(iuser)))
{
check = "Valid User!";
}
return check;
In Java, you compare Strings by using equals(), not ==
if(is_user.equals(verify))
Also you don't need to loop through all users. Just add a condition in your query instead. See the documentation for more info.
EDIT: the good query would be:
String[] args = new String[1];
args[0] = is_user;
Cursor c = fg.query(Login_Table, null, colUsername + " = ?", args, null, null, null);
Always use arguments, never concatenate like colUsername+"="+is_user, this is vulnerable to SQL injection.
Use
.equals(...).
if(is_user.equals(verify)){
...
I have two application application A and application B. From A I am saving an image, two string values into the database by using contentProvider.
From the application B i am accessing this database and getting the image. The code which i am using is given below
byte b[] = null;
Cursor c = mContext.getContentResolver().query(allTitles, projection,null, null, null);
if (c.moveToFirst()) {
do{
if(info.activityInfo.name != null) {
if(c.getString(1).equals(info.activityInfo.name)){
b=c.getBlob(0);
}
}
} while (c.moveToNext());
}
c.close();
Here in the selection area i am giving null so i am getting all the values in the database and i have to run this loop. Instead of that I want to give a where clause to get the exact raw.
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name ="+info.activityInfo.name, null, null);
So i have changed like this but it does not worked. How i should give the selection args ? Please help.
try..
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = "+info.activityInfo.name, null, null);
or
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = ? ", new String[]{info.activityInfo.name}, null);
in case if activity_name is text
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = "+"'"+info.activityInfo.name+"'", null, null);
or
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = ? ", new String[]{"'"+info.activityInfo.name+"'"+}, null);
You need a '?' for Android to put in values for you in the WHERE clause.
The syntax should be,
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name =?", new String[]{info.activityInfo.name}, null);
Integer c=0;
DatabaseHelper dbh1 = new DatabaseHelper(this);
Cursor ca = dbh1.getReadableDatabase().query(DatabaseHelper.TABLE_NAME, null, null, null, null, null, null);
while (ca.moveToNext())
{
String Ans6 = ca.getString(ca.getColumnIndex(DatabaseHelper.VALUE6));
if ( Ans6.equals("Automatic"))
{
c+=1;
}
}
String strI = Integer.toString(c);
Toast toast=Toast.makeText(this, strI, 2000);
toast.show();
I have a SQlite database setup in android. The table has been successfully set up and I am trying to check values from column VALUE6 if it matches Automatic. The cursor has to go through every row and check the value of VALUE6 in that row and return how many rows have Automatic. Is my code right? any ideas? strI returns 0 every time i run the code.
That's a brute force way of doing it. Why not query for that value and then get the count of the rows in the returned cursor?
Cursor ca = dbh1.getReadableDatabase().query(DatabaseHelper.TABLE_NAME, null, DatabaseHelper.VALUE6 + "='AUTOMATIC'", null, null, null, null);
int count = ca.getCount();
Hope this helps!
Hey guys,im working on a simple quiz and im gone crazy !!
The problem is that everything is working (database created as it shoulds) except when i am trying to get string from database shows java.lang.NullPointerException.I checked the uri is corrected and the number of items in array!!I am trying to find out why this is happening for 5 hours and i am stucked here!!!I dont know what elso to do!!Your help is more than appreciated!!
My main class where i am trying to get string is that one with bold
Uri newUri = ContentUris.withAppendedId(
QuestionsProvider.CONTENT_URI,
this.currentQuestion);
Log.d(TAG, "SHOWQUESTION " + " URI="+newUri.toString());
Cursor cursor = cr.query(newUri,
null, null, null, null);
if (cursor.moveToFirst()) {
**question.setText(cursor.getString(
QuestionsProvider.QUESTION_COLUMN)); //HERE I AM GETTING THE ERROR
currentAnswer = cursor.getString(
QuestionsProvider.ANSWER_COLUMN);**
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text;
String answerGiven =
answer.getText().toString();
answer.setText("");
if (answerGiven.
equalsIgnoreCase(currentAnswer))
{text = "Correct";
}else{
text = "Wrong - "+currentAnswer;
Toast.makeText(getApplicationContext(),
text, Toast.LENGTH_SHORT).show();
}
}});
}
cursor.close();
dialog.show();
and in my manifest i add succesfully the provider and is loading as it should!!
Why this error happens??I can see anything wrong!!
It doesn't look like you're specifying a projection in the query:
Cursor cursor = cr.query(newUri, null, null, null, null);
Try adding a projection with the columns you want returned:
Cursor cursor = cr.query(newUri, new String[] {KEY_ID, KEY_QUESTION, KEY_ANSWER}, null, null, null);
I found the solution guys! Thank you for helping me unstucking heh :P
This is the solution i found!!
String columns[] = new String[] { QuestionsProvider.KEY_QUESTION, QuestionsProvider.KEY_ANSWER };
Uri mUri = QuestionsProvider.CONTENT_URI;
Cursor cur = managedQuery(mUri, columns, // Which columns to return
QuestionsProvider.KEY_ID+"="+currentQuestionNumber, // WHERE clause; which rows to return(all rows)
null, // WHERE clause selection arguments (none)
null // Order-by clause (ascending by name)