this is my first post and I am sorry if I am posting incorrectly.
I am currently trying to learn some simple android development and am running into an error I can't figure out. I downloaded some simple login tutorial code that came only storing a username and password.
The code worked fine as downloaded. When I added some entries to the database, I started getting errors. I can create an account still, but when I try to log in, it says information doesn't match. I have done some testing and I think the database is not storing any information during create account, even though I am getting the confirmation message that is displayed if the values are inserted. I also get this error in the console:"java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters."
Any help is appreciated. Here is the java code I think is relevant to this issue. The first is where I am creating an account, and the second is where I am interacting with the database. Let me know if you need other code. Thanks!
public class SignUPActivity extends Activity
{
EditText editTextUserName,editTextPassword,editTextConfirmPassword,editTextDog,editTextFColor, editTextCat;
Button btnCreateAccount;
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get References of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
editTextDog=(EditText)findViewById(R.id.editTextDog);
editTextFColor=(EditText)findViewById(R.id.editTextFColor);
editTextCat=(EditText)findViewById(R.id.editTextCat);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
String dog=editTextDog.getText().toString().toLowerCase();
String fcolor=editTextFColor.getText().toString().toLowerCase();
String cat=editTextCat.getText().toString().toLowerCase();
// check if any of the fields are vaccant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")||dog.equals("")||fcolor.equals("")||cat.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
if (!dog.equals("dog"))
{
Toast.makeText(getApplicationContext(),"You did not type 'dog' correctly",Toast.LENGTH_LONG ).show();
return;
}
if (!cat.equals("cat"))
{
Toast.makeText(getApplicationContext(),"You did not type 'cat' correctly",Toast.LENGTH_LONG ).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password, dog, fcolor, cat);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
loginDataBaseAdapter.close();
}
}
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text, DOG text, FCOLOR text, CAT text ); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String password, String dog, String fcolor, String cat )
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("DOG",dog);
newValues.put("FCOLOR,",fcolor);
newValues.put("CAT",cat);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
//Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number of Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getPasswordEntry(String userName)
{
Cursor cursor=db.query("LOGIN", null, "USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}
Aucun commentaire:
Enregistrer un commentaire