1

please help me. I'm trying to make a registration app that will save consumer's data. The user needs to enter his complete name together with the company he wants to register to. He can register many times as long as it is in different company. My problem in my code below is that the program only compare the user input on the first data he enter. for example, the user enter his name and the company is ABC, the program accept it, when the user enter again his name and the company is DEF the program still accept it, but when the user enter his name and the company DEF again the program still accept it which is wrong, when you change the company in ABC the program did not accept it. So what I see is that my code only comparing the data on the first info entered.

here's my code:

in my databaseAdapter

public Constructor getNameCp(String name) { SQLiteDatabase db = this.getReadableDatabase(); Constructor consName = new Constructor(); try { Cursor c = db.query(Constants.DATABASE_TABLE_CONSUMER,null, Constants.CONSUMER_FULLNAME + "=?", new String[]{String.valueOf(name)},null, null, null); if (c == null) { return consName; } else{ c.moveToFirst(); consName = new Constructor(Integer.parseInt(c.getString(0)),c.getString(c.getColumnIndex(Constants.CONSUMER_EMPNO)),c.getString(c.getColumnIndex(Constants.CONSUMER_FULLNAME)), c.getString(c.getColumnIndex(Constants.CONSUMER_CELLPHONENO)), c.getString(c.getColumnIndex(Constants.CONSUMER_COMPANY))); } }catch(Exception e){ e.printStackTrace(); } return consName; } 

in my activity

String name = databaseAdapter.getNameCp(fullName).getConsumerFullname(); String comp = databaseAdapter.getNameCp(fullName).getCompanyname(); if ((fullName.equals(name)) && (companyName.equals(comp))){ UpdateByEmployeeName(fullName); Toast.makeText(RegistrationActivity.this, "Account info already exist.", Toast.LENGTH_LONG).show(); }else{ String currentDateandTime = sdf.format(new Date()); databaseAdapter.SaveConsumerDetails(new Constructor(Emp.toUpperCase(), Lastname, Firstname, Middlename.toUpperCase(), Cp, Email, fullName, careFriend, companyName, emailmark, currentDateandTime, consumerId, consumercompanycode)); refresh(); } 
7
  • provide your create table query so i can help you.. Commented Jun 18, 2013 at 4:13
  • i'm using a preloaded database. Commented Jun 18, 2013 at 4:18
  • have you got valid name and comp from database from first two lines.?? Commented Jun 18, 2013 at 4:20
  • yes. but comp only get the company name that was entered first. Commented Jun 18, 2013 at 4:40
  • @lolliloop you can prevent duplicate entries to the column by havaing aunique contraint for the column required Commented Jun 18, 2013 at 4:44

1 Answer 1

0

Use below method in your database class for getting list of all companies for particular employee name:

public ArrayList<String> getCompName(String name) { ArrayList<String> alCompNm = new ArrayList<String>(); try { Cursor c = db.rawQuery("select CompName FROM projsitemast where Name='"+name+"'", null); if (c != null) { c.moveToFirst(); for (int j = 0; j < c.getCount(); j++) { alCompNm.add(c.getString(0)); System.out.println("Client Site Name : " + c.getString(0)); c.moveToNext(); } c.close(); } return alCompNm; } catch (Exception e) { e.printStackTrace(); } return alCompNm; } 

In your Main Activity:

flag = 0; ArrayList<String> cNm = new ArrayList<String>(); dba.open(); cNm = dba.getCompName(yourname); for(int i=0;i< cNm.size();i++){ String cmpname = cNm.get(i).toString(); if(cmpname.equals(yourcmpname)){ flag = 1; } } if(flag==0){ String currentDateandTime = sdf.format(new Date()); databaseAdapter.SaveConsumerDetails(new Constructor(Emp.toUpperCase(), Lastname, Firstname, Middlename.toUpperCase(), Cp, Email, fullName, careFriend, companyName, emailmark, currentDateandTime, consumerId, consumercompanycode)); refresh(); // OR as per your requirement } else{ Toast.makeText(RegistrationActivity.this, "Account info already exist.", Toast.LENGTH_LONG).show(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.