samedi 17 octobre 2015

Inserting into wrong table database Android

So I'm trying to learn how to use databases in android and everything was fine until I tried using two tables the problem is that when I try to insert an entry onto restau table it always gets inserted into LOGIN table. Can someone help?

Here's the code for my Database Adapter:

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); ";
    static final String DATABASE_CREATE_1 =
        "create table "+"restau"+
        "( " +"ID"+" integer primary key autoincrement,"+
                "USERNAME  text,PASSWORD 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 insertEntryrestau(String userName,String password)
{
    ContentValues newValues = new ContentValues();
    // Assign values for each row.
    newValues.put("USERNAME", userName);
    newValues.put("PASSWORD",password);

    // Insert the row into your table
    db.insert("restau", null, newValues);
    ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public void insertEntrycusto(String userName,String password)
{
    ContentValues newValues = new ContentValues();
    // Assign values for each row.
    newValues.put("USERNAME", userName);
    newValues.put("PASSWORD", password);

    // 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 table)
{
    //String id=String.valueOf(ID);
    String where="USERNAME=?";
    int numberOFEntriesDeleted= db.delete(table, where, new String[]{UserName}) ;
    // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
    return numberOFEntriesDeleted;
}
public String getSingleEntryrestau(String userName)
{
    String returnVar;
    Cursor cursor=db.query("restau", null, " USERNAME=?", new String[]{userName}, null, null, null);
    if(cursor.getCount()<1) // UserName Not Exist
    {
        cursor.close();
        returnVar="NOT EXIST";
        return returnVar;
    }
    cursor.moveToFirst();
    returnVar= cursor.getString(cursor.getColumnIndex("PASSWORD"));
    cursor.close();
    return returnVar;
}
public String getSingleEntrycusto(String userName)
{
    String returnVar;
    Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
    if(cursor.getCount()<1) // UserName Not Exist
    {
        cursor.close();
        returnVar="NOT EXIST";
        return returnVar;
    }
    cursor.moveToFirst();
    returnVar= cursor.getString(cursor.getColumnIndex("PASSWORD"));
    cursor.close();
    return returnVar;
}
public void  updateEntry(String userName,String password,String table)
{
    // 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(table,updatedValues, where, new String[]{userName});
}
public void clearDatabase() {
    close();
    context.deleteDatabase(DATABASE_NAME);
}

}

Here's the code for where i call insert

if(userName.equals("")||password.equals("")||confirmPassword.equals(""))
            {
                Toast.makeText(getApplicationContext(), "Field Vacant", 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;
            }
            else
            {
                // Save the Data in Database
                loginDataBaseAdapter.insertEntryrestau(userName, password);
                Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
                finish();
            }
        }

Aucun commentaire:

Enregistrer un commentaire