-1

@After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor @After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor @After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor

public class DatabaseOperations extends SQLiteOpenHelper {

public static final int Database_version = 2;

public static final String Tag = DatabaseOperations.class.getSimpleName();

private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + TableData.TableInfo.TABLE_NAME + " (" +
                TableData.TableInfo.USER_ID + " INTEGER PRIMARY KEY," +
                TableData.TableInfo.USER_PASS +" TEXT "+ "," +
                TableData.TableInfo.USER_EMAIL +" TEXT "+ ");";

public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null,Database_version);
    Log.d("Tag", "Database created");
}

@Override
public void onCreate(SQLiteDatabase sdb) {
    sdb.execSQL(SQL_CREATE_ENTRIES);
    Log.d("Tag", "Table created");

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

}

public void putInformation(DatabaseOperations drop, String name, String   pass, String email) {
    SQLiteDatabase SQ = drop.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(TableData.TableInfo.USER_ID, name);
    cv.put(TableData.TableInfo.USER_PASS, pass);
    cv.put(TableData.TableInfo.USER_EMAIL, email);
    long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d("Tag", "inert a row");
 }

public Cursor getInformation(DatabaseOperations dop) {
    SQLiteDatabase SQ = dop.getReadableDatabase();
    String[] coloumns = {TableData.TableInfo.USER_ID,  TableData.TableInfo.USER_PASS, TableData.TableInfo.USER_EMAIL};
    Cursor CR = SQ.query(TableData.TableInfo.TABLE_NAME, coloumns, null, null, null, null, null);
    return CR;
}
}

RegisterActivity

public class RegisterActivity extends AppCompatActivity {


    EditText USER_NAME, USER_PASS, CON_PASS, USER_EMAIL;
    String user_name, user_pass, con_pass, user_email;
    Button REG;
    Context ctx = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        USER_NAME = (EditText) findViewById(R.id.reg_user);
        USER_PASS = (EditText) findViewById(R.id.reg_pass);
        CON_PASS = (EditText) findViewById(R.id.con_pass);
        USER_EMAIL = (EditText) findViewById(R.id.reg_email);
        REG = (Button) findViewById(R.id.user_reg);
        REG.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                user_name = USER_NAME.getText().toString();
                user_pass = USER_PASS.getText().toString();
                con_pass = CON_PASS.getText().toString();
                user_email = USER_EMAIL.getText().toString();
                if (!(user_pass.equals(con_pass))) {
                    Toast.makeText(getBaseContext(), "Password are not matching", Toast.LENGTH_LONG).show();
                    USER_NAME.setText("");
                    USER_PASS.setText("");
                    CON_PASS.setText("");
                    USER_EMAIL.setText("");
                } else {
                    DatabaseOperations DB = new DatabaseOperations(ctx);
                    DB.putInformation(DB, user_name, user_pass, user_email);
                    Toast.makeText(getBaseContext(), "Registration is suessful", Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        });


    }
}
Mohammad
  • 31
  • 5
  • 1
    why are you repeating yourself so much? I don't understand why you're repeating yourself so much. Please refrain from repeating yourself so much. – Kritner Dec 14 '16 at 13:14
  • Please don't spam one sentence over and over again in your question. If Stackoverflow tells you you should add more text to your question, then you should add more and detailed descriptions of your Problem. Simply repeating the same sentence again isn't the way to go. – OH GOD SPIDERS Dec 14 '16 at 13:15
  • @Kritner so the question gets accepted. if you have too few text and so much code, SO is not accepting the question – Aelop Dec 14 '16 at 13:15
  • put more detail into your problem area then, not repeating the same sentence over and over. If you have a statement that "Creates a table" each time a function is invoked, then it's going to attempt to create the table every time "onCreate" is invoked. – Kritner Dec 14 '16 at 13:16
  • Possible duplicate of [Create table in SQLite only if it doesn't exist already](http://stackoverflow.com/questions/4098008/create-table-in-sqlite-only-if-it-doesnt-exist-already) – Parviz Rozikov Dec 14 '16 at 13:17

2 Answers2

2

It is because you are creating table each time you insert a row. To solve this problem you need to change the create table query "CREATE TABLE 'TABLE_NAME' IF NOT EXISTS". The "IF NOT EXISTS" will restrict the system to create the database again if it is created once.

Lalit Dhameliya
  • 348
  • 3
  • 8
0

You are calling creating a new Table every time, you create an instance of DatabaseOperations - as your SQL statement 'CREATES'

Modify SQL_CREATE_ENTRIES to something like below

private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE IF NOT EXISTS  " + TableData.TableInfo.TABLE_NAME + " (" +
                TableData.TableInfo.USER_ID + + " integer primary key autoincrement, "+
                TableData.TableInfo.USER_PASS +" TEXT "+ "," +
                TableData.TableInfo.USER_EMAIL +" TEXT "+ ");";

Edit 1) I've update query to include auto-increment your primary key

2) In your onUpgrade method add the line

    db.execSQL("DROP TABLE IF EXISTS " + TableData.TableInfo.TABLE_NAME);

This will delete the table when you change the version of DB.

Next, update the version of by 1, to 3.

Re-run app, it will be rebuild the DB table.

Zain
  • 2,336
  • 1
  • 16
  • 25
  • Thank you but it still not solve the problem 5828-15828/com.example.imros.foodsafety D/Tag: Database created 15828-15828/com.example.imros.foodsafety D/Tag: Table created 15828-15828/com.example.imros.foodsafety D/Tag: inert a row 15828-15828/com.example.imros.foodsafety I/Adreno200-EGLSUB: : Format RGBA_8888. I/Adreno200-EGLSUB: : Format RGBA_8888. 15828-15828/com.example.imros.foodsafety D/Tag: Database created 15828/com.example.imros.foodsafety D/Tag: inert a row – Mohammad Dec 14 '16 at 13:32