I have a database table in my android app. I want to compare database table name with a particular string.
Here is my code
if(branchName == TABLE_NAME)
{
}
else
{
Toast.makeText(this,"table not available",Toast.LENGTH_LONG).show();
}
Here branchName is the string.TABLE_NAME is a static string and it is the name of the table.
Whenever I run this code, control goes to else part and shows the toast message.
Why? How can I fix that?
for comparing two string you must use equal or equalsIgnoreCase ... ...
if (branchName.equalsIgnoreCase(TABLE_NAME) ){
// do sth
}
else{
Toast.makeText(this,"table not available",Toast.LENGTH_LONG).show();
}
try this way.
Related
I want to make sure that time is not same.
It's an appointment booking app, so it cannot be the same.
public void clients()
{
String name=t2.getText().toString().trim();
String gender=t1.getText().toString().trim();
String barber=t4.getText().toString().trim();
String dt=t5.getText().toString().trim();
String concerng=t6.getText().toString().trim();
if(!TextUtils.isEmpty(name) || !TextUtils.isEmpty(gender) || !TextUtils.isEmpty(barber) || !TextUtils.isEmpty(dt) || !TextUtils.isEmpty(concerng))
{
String id=databaseReference.push().getKey();
client client=new client(id,name,gender,barber,dt,concerng);
databaseReference.child(id).setValue(client);
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Something Went Wrong Please Check Again", Toast.LENGTH_SHORT).show();
}
To be able to compare two dates, you need to store the values of the cdatetime property as a timestamp and not as a String, as I see in your database now. Here you can find how to add and retrieve a timestamp from a Firebase database. If you follow those steps, you'll be able to compare very easy two long primitives.
I have a register form. i need to save the content in db. my data are saved successfully. but when i use space or some special characters my datas are not stored in db. what should i use to store the special characters and space in db,
if (Name.getText().toString().equals("")) {
Name.setError("Enter your name");
Name.requestFocus();
} else if (Dob.getText().toString().equals("")) {
Dob.setError("Enter Date of Birth");
Dob.requestFocus();
} else if (Collegeid.getText().toString().equals("")) {
Collegeid.setError("Enter your CollegeID");
Collegeid.requestFocus();
}
else {
String sname = Name.getText().toString();
String dob = Dob.getText().toString().trim();
String cid = Collegeid.getText().toString().trim();
}
Refer the screenshot above
Try to use [] brackets in the SQLite statements. It allow to you to add anything what you want.
I'm having problems with an IF-statement and comparing strings.
I did check this out to know more about string comparing.
How do I compare strings in Java?
If I understood this right "cursor.getString(0)" will return a string. Anyway everthing in my Sqlite table is of the type "text" I guess. I can print with log.d the string "name" and that show the correct word. But when it gets to compare strings in the IF-statement it does not match anything. I have no problem with Sqlite or any other parts of the code.
My if-statement does not work
Here is my code:
String Food = "Food";
while(cursor.moveToNext()){
String name = cursor.getString(0);
Log.d("name", "name: " + name); //This sprints: name: Food
if(name.equals(Food)){
// ...do something
}
else if (Food.equalsIgnoreCase(name)){
// ...do something else
}
}
It will just continue to print the rest of the column but I want to "do something" if I find a name "Food".
Can someone help find out what is wrong with my IF-statement?
just a short thing, please don't name a variable with a capital at the beginning (looks like a class). However, perhaps one of the Strings contains a space or something like that. Use the method trim before you compare your strings:
String food = "Food";
while(cursor.moveToNext()){
String name = cursor.getString(0);
Log.d("name", "name: '" + name + "'"); //This sprints: name: 'Food' ?
if(name.trim().equals(food.trim())){
// ...do something
} else if (food.trim().equalsIgnoreCase(name.trim())) {
// ...do something else
}
}
If this doesn't solve your problem, you need probably post more code, if it solves your problem, please rate this answer.
Am not a learner to boolean expressions but this seems to be giving me a bit of an headache.
In my app when i search for a user,I am recieving certain values from my datase using this MYSQL statement.
SELECT `u3`.`username`,
CASE WHEN `u3`.`username` IS NULL THEN 'Not Existing'
ELSE 'Existing' END is_following
FROM `userinfo` `u3` WHERE `u3`.`username` LIKE '%".mysql_real_escape_string($data)."%' OR `u3`.`email` LIKE '%".mysql_real_escape_string($data)."%'
UNION
SELECT `u2`.`username`,
CASE WHEN `u2`.`username` IS NULL THEN 'Follow'
ELSE 'Following' END is_following
FROM
`userinfo` `u1` LEFT JOIN `followers` `f`
ON `u1`.`username` = `f`.`source_id`
LEFT JOIN `userinfo` `u2`
ON `f`.`destination_id` = `u2`.`username`
AND (`u2`.`username` LIKE '%".mysql_real_escape_string($data)."%' OR `u2`.`email` LIKE '%".mysql_real_escape_string($data)."%')
WHERE
`u1`.`username` = '$name'
Now the syntax above would return these set of values, a username and My own metadata. For example it would return
set of values 1: (davies and Existing)[if davies is in the userinfo table] AND (davies and Following)[if davies is following halex in the followers table]
OR
set of values 2: (null and Not Existing)[if davies is not in the userinfo table] AND (null and not followed)[davies does not exist]
OR
set of values 3: (davies and Existing)[if davies is in the userinfo table] AND (null and Not Following)[if davies is not following halex]
These are the set of values i recieve and i need an if statement to sieve through so i can display to my users this simple information
davies and Following OR davies and Follow[if davies is not following halex] OR User Does not Exist
Now am not sure if i should change the structure of my SOL statement or i could get a better way of handling this logic with if and else statements
This was the structure of my if else statement but it doesn't seem to work the way i want it to
if(username != null && metadata.contains("Existing"))//user exist
{
value = username;
//user exists but skip
}else
{
if(username != null)
{
map = new HashMap<String, String>();
map.put(TAG_USERNAME, value);
map.put(TAG_METADATA, metadata);
listvalue.add(map);
}else
{
//user does not exist
Log.i("search ", "user does not exist");
}
}
The codes above belongs to android.
Thanks any suggestion is appreciated. Thanks again in Advance.
What if you split your query into two parts? Querying for one set of information at a time will make things simpler and easier to debug.
I could imagine doing something that looks like this (in pseudo-code):
query = "SELECT username
FROM `userinfo` `u3`
WHERE `u3`.`username` LIKE '%".mysql_real_escape_string($data)."%'
OR `u3`.`email` LIKE '%".mysql_real_escape_string($data)."%'"
var result = db.execute(query);
// set to true if the above query returns at least one row
bool user_exists = (result.RowCount() > 0)
var username = (user_exists)? result.GetRow().GetField("username") : null;
if(user_exists)//user exist
{
value = username;
//user exists but skip
} // ....
(I've only replicated the functionality pertaining to your UNION's first SELECT statement; the logic associated with the second statement can be similarly rewritten.)
i have two different tables.I want to compare the names in two tables,if both the names are equal then i want to get the details.how can i do that? please help me.
Here is the code:
Cursor cr=mDbManager.fetchnewcustomerdata();
Cursor cr1 = mDbManager.fetchinvoicecustomerdata();
cr1.moveToFirst();
//String address;
while (!(cr1.isAfterLast())) {
String name = cr1.getString(cr1.getColumnIndex("icstname"));
if(name.equals(cr.getString(cr.getColumnIndex("cname"))))
{
Toast.makeText(this,"equal",1000).show();
}
String address = cr1.getString(cr1.getColumnIndex("caddress"));
map.put(name, address);
nameAList.add(cr1.getString(cr1.getColumnIndex("icstname")));
cr1.moveToNext();
}
mDbManager.close();
}
I think two ways it is possible
1) Either you need to establish relationship between those tables using the name column you are thinking of same between two tables.
2) Two separate queries to database, where condition for both queries will be the name
You can use CursorJoiner
http://developer.android.com/reference/android/database/CursorJoiner.html
Firstly, the names are ordered .
Then, sort it as the following sample:
while( !eofInTable1 && !eofInTable2) {
String name1 = getFieldFromTable1();
String name2 = getFiledFromTable2();
int tmpResult = name1.compareTo(name2);
if(tmpResult == 0) {
doYourAction();
table1.moveToNext();
table2.moveToNext();
}
else if( tmpResult == -1) {
table1.moveToNext();
}
else {
table2.moveToNext();
}
}