0

I am trying to create a application where i need to create two table register n details . i want registered user can login and then they can save their details. but can any one tell me how to establish this connection between these two table. i tried doing it with like this but getting error.

(PasswordKeeperDb.java)

public class PasswordKeeperDb {
private static final int DATABASE_VERSION = 1;
private DbHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;
public static final String DATABASE_NAME = "PasswordKeeper";
private static final String REGISTER_TABLE_NAME = "RegisterTable";
private static final String DETAIL_TABLE_NAME = "DetailsTable";


private static String FIRST_NAME = "firstName", LAST_NAME = "lastName", EMAIL = "email",
        PASSWORD = "password", LOGIN_ID = "loginId", DETAIL_ID = "detailsId", TITLE = "title";


private static final String REGISTER_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
        + REGISTER_TABLE_NAME
        + "("
        + LOGIN_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT , "
        + FIRST_NAME
        + " VARCHAR(80) , "
        + LAST_NAME
        + " VARCHAR(80) , "
        + EMAIL
        + " VARCHAR(80) , "
        + PASSWORD
        + " VARCHAR(80) ) ";

private static final String DETAIL_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
        + DETAIL_TABLE_NAME
        + "("
        + DETAIL_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT , "
        + LOGIN_ID
        + " INTEGER ,"
        + TITLE
        + " VARCHAR(80) , "
        + EMAIL
        + " VARCHAR(80) , "
        + PASSWORD
        + " VARCHAR(10) , )";



private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(REGISTER_TABLE_CREATE);
        db.execSQL(DETAIL_TABLE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}


public PasswordKeeperDb(Context c) {
    dbHelper = new DbHelper(c);
}

public long insertRegisterTable(final RegisterModel registerModel)
        throws SQLException {
    ContentValues values = new ContentValues();
    values.put(FIRST_NAME, registerModel.getFname());
    values.put(LAST_NAME, registerModel.getLname());
    values.put(EMAIL, registerModel.getEmail());
    values.put(PASSWORD, registerModel.getPassword());
    return sqLiteDatabase.insert(REGISTER_TABLE_NAME, null, values);
}

public long insertDetailTable(final AddModel addModel)
        throws SQLException {
    ContentValues values = new ContentValues();
    values.put(LOGIN_ID, addModel.getLoginId());
    values.put(TITLE, addModel.getTitle());
    values.put(EMAIL, addModel.getEmail());
    values.put(PASSWORD, addModel.getPassword());
    return sqLiteDatabase.insert("DetailsTable", null, values);
}


public PasswordKeeperDb open() throws SQLException {
    sqLiteDatabase = dbHelper.getWritableDatabase();
    sqLiteDatabase = dbHelper.getReadableDatabase();
    return this;
}


public Cursor getUser(String email, String pass) {
    return sqLiteDatabase.rawQuery("select * from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
            "' and password=" + "'" + pass + "'", null);
}

public Cursor getLoginId(String email) {
    return sqLiteDatabase.rawQuery("select loginId from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
            "'", null);
}

public void close() {
    dbHelper.close();
}
}

(MainActivity.java)

 public class MainActivity extends AppCompatActivity {

FloatingActionButton floatingActionButton;

ListView listView;
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
PasswordKeeperDb passwordKeeperDb;
View mView = getLayoutInflater().inflate(R.layout.dialog_details, null);
final EditText mEmail  = (EditText) mView.findViewById(R.id.email);
final EditText mTitle  = (EditText) mView.findViewById(R.id.title);
final EditText  mPassword = (EditText) mView.findViewById(R.id.password);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.list_view);
    floatingActionButton = (FloatingActionButton) findViewById(R.id.add);

    passwordKeeperDb = new PasswordKeeperDb(this);

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);

            mBuilder.setView(mView);
            final AlertDialog dialog = mBuilder.create();



            Button mAdd = (Button) mView.findViewById(R.id.dadd);
            mAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mTitle.getText().toString().isEmpty()) {
                        Toast.makeText(MainActivity.this, "Add Title", Toast.LENGTH_SHORT).show();

                    } else if (mEmail.getText().toString().isEmpty()) {
                        Toast.makeText(MainActivity.this, "Add Email", Toast.LENGTH_SHORT).show();

                    } else if (mPassword.getText().toString().isEmpty()) {
                        Toast.makeText(MainActivity.this, "Add Password!", Toast.LENGTH_SHORT).show();

                    } else {
                        passwordKeeperDb.open();
                        AddModel addModel = new AddModel("", getLoginId(mEmail), mTitle.getText().toString(), mEmail.getText().toString(), mPassword.getText().toString());
                        Long result = passwordKeeperDb.insertDetailTable(addModel);
                        Log.d("ADB", result + "");
                        if (result > 0) {
                            Toast.makeText(MainActivity.this, "ADDED Successfully", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        } else {
                            Toast.makeText(MainActivity.this, "Error Occurred !", Toast.LENGTH_SHORT).show();
                        }
                        passwordKeeperDb.close();

                    }
                }
            });
            dialog.show();


        }
    });


}

private String getLoginId(EditText mEmail) {

    return getLoginId(this.mEmail);
}

}

Log Cat:

 FATAL EXCEPTION: main
                                                                                     Process: com.example.hope.passwordkeeper, PID: 26605
                                                                                 android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS DetailsTable(detailsId INTEGER PRIMARY KEY AUTOINCREMENT , loginId INTEGER ,title VARCHAR(80) , email VARCHAR(80) , password VARCHAR(10) , )
                                                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                                     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                                     at com.example.hope.passwordkeeper.PasswordKeeperDb$DbHelper.onCreate(PasswordKeeperDb.java:69)
                                                                                     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                     at com.example.hope.passwordkeeper.PasswordKeeperDb.open(PasswordKeeperDb.java:105)
                                                                                     at com.example.hope.passwordkeeper.HomeActivity$1$1.onClick(HomeActivity.java:51)
                                                                                     at android.view.View.performClick(View.java:5207)
                                                                                     at android.view.View$PerformClick.run(View.java:21168)
                                                                                     at android.os.Handler.handleCallback(Handler.java:746)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
5
  • 1
    perhaps bcz of extra comma in + " VARCHAR(10) , )"; Commented Feb 27, 2017 at 6:40
  • you have add comma for last column please remove comma like this + " VARCHAR(10) )"; for DETAIL_TABLE_CREATE Commented Feb 27, 2017 at 6:44
  • Possible duplicate of SQLiteDatabase error "sqlite: (1) near ")": syntax error" Commented Feb 27, 2017 at 6:46
  • whats error you get?? after change clean project as well uninstall app then again try to run Commented Feb 27, 2017 at 6:49
  • @SuruchiSinha example of login and register with sqlite androidtutorialshub.com/… Commented Feb 27, 2017 at 7:07

2 Answers 2

2

At first rectify your DETAIL_TABLE_CREATE

 private static final String DETAIL_TABLE_CREATE  =
            "CREATE TABLE " + TABLE_TEAM + " ("
                    + DETAIL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
                    + LOGIN_ID + " INTEGER ,"
                    + TITLE + " VARCHAR(80) ,"
                    + EMAIL + " VARCHAR(80) ,"
                    + PASSWORD + " VARCHAR(10)" + ")";

Then Clean Rebuild and Uninstall OLD app and run again

Sign up to request clarification or add additional context in comments.

Comments

0

Please find the correct code:-

public class PasswordKeeperDb {
private static final int DATABASE_VERSION = 1;
private DbHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;
public static final String DATABASE_NAME = "PasswordKeeper";
private static final String REGISTER_TABLE_NAME = "RegisterTable";
private static final String DETAIL_TABLE_NAME = "DetailsTable";


private static String FIRST_NAME = "firstName", LAST_NAME = "lastName", EMAIL = "email",
        PASSWORD = "password", LOGIN_ID = "loginId", DETAIL_ID = "detailsId", TITLE = "title";


private static final String REGISTER_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
        + REGISTER_TABLE_NAME
        + "("
        + LOGIN_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT , "
        + FIRST_NAME
        + " VARCHAR(80) , "
        + LAST_NAME
        + " VARCHAR(80) , "
        + EMAIL
        + " VARCHAR(80) , "
        + PASSWORD
        + " VARCHAR(80) ) ";

private static final String DETAIL_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
        + DETAIL_TABLE_NAME
        + "("
        + DETAIL_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT , "
        + LOGIN_ID
        + " INTEGER ,"
        + TITLE
        + " VARCHAR(80) , "
        + EMAIL
        + " VARCHAR(80) , "
        + PASSWORD
        + " VARCHAR(10) )";



private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(REGISTER_TABLE_CREATE);
        db.execSQL(DETAIL_TABLE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}


public PasswordKeeperDb(Context c) {
    dbHelper = new DbHelper(c);
}

public long insertRegisterTable(final RegisterModel registerModel)
        throws SQLException {
    ContentValues values = new ContentValues();
    values.put(FIRST_NAME, registerModel.getFname());
    values.put(LAST_NAME, registerModel.getLname());
    values.put(EMAIL, registerModel.getEmail());
    values.put(PASSWORD, registerModel.getPassword());
    return sqLiteDatabase.insert(REGISTER_TABLE_NAME, null, values);
}

public long insertDetailTable(final AddModel addModel)
        throws SQLException {
    ContentValues values = new ContentValues();
    values.put(LOGIN_ID, addModel.getLoginId());
    values.put(TITLE, addModel.getTitle());
    values.put(EMAIL, addModel.getEmail());
    values.put(PASSWORD, addModel.getPassword());
    return sqLiteDatabase.insert("DetailsTable", null, values);
}


public PasswordKeeperDb open() throws SQLException {
    sqLiteDatabase = dbHelper.getWritableDatabase();
    sqLiteDatabase = dbHelper.getReadableDatabase();
    return this;
}


public Cursor getUser(String email, String pass) {
    return sqLiteDatabase.rawQuery("select * from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
            "' and password=" + "'" + pass + "'", null);
}

public Cursor getLoginId(String email) {
    return sqLiteDatabase.rawQuery("select loginId from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
            "'", null);
}

public void close() {
    dbHelper.close();
}

You have add extra comma on create detail table at the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.