0

My MainActivity looks like this:

public class MainActivity extends AppCompatActivity {

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

    DBHandler db = new DBHandler(this);

    // Inserting Questions
    db.addQuestion(new Question("1. Question1", 1));
    db.addQuestion(new Question("2. Question2", 2));

    // Inserting Answers
    db.addAnswer(new Answer("2", 1, 0));
    db.addAnswer(new Answer("3", 1, 0));
    db.addAnswer(new Answer("4", 1, 1));
    db.addAnswer(new Answer("5", 1, 0));

    // Reading all Questions
    List<Question> questions = db.getAllQuestions();
    // Reading all Answers
    List<Answer> answers = db.getAllAnswers();

    // other code
}

I've created a new java file and i moved some of the code there. My new activity looks like this:

public class DatabaseContent extends AppCompatActivity {

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

    DBHandler db = new DBHandler(this);

    // Inserting Questions
    db.addQuestion(new Question("1. Question1", 1));
    db.addQuestion(new Question("2. Question2", 2));

    // Inserting Answers
    db.addAnswer(new Answer("2", 1, 0));
    db.addAnswer(new Answer("3", 1, 0));
    db.addAnswer(new Answer("4", 1, 1));
    db.addAnswer(new Answer("5", 1, 0));
}

The ramaining code from the MainActivity looks like this:

public class MainActivity extends AppCompatActivity {

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

    DBHandler db = new DBHandler(this);

    // Reading all Questions
    List<Question> questions = db.getAllQuestions();
    // Reading all Answers
    List<Answer> answers = db.getAllAnswers();

    // other code
}

I want to insert records in database from the DatabaseContent activity and in MainActivity i want only to read them. I know that in java it's not like in php to include files, but kind of. With this code, the records are not inserted in the database. Also, when the MainActivity is recreated, i want the data not to be inserted again. How can i do that? Thanks!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

2 Answers2

0

Inside your DatabaseHelper Class while overriding onCreate Method

   DatabaseHelper extends SQLiteOpenHelper {

  @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL(//Create your table first);
      --this will insert the master data when the table will be created for the first time 
   db.execSQL("INSERT INTO " + TABLE + " (" + COL1 + ", " + COL2 + "," + COL3 + "," + COL4 + "," + COL5 + ","+COL6+")" +
            " VALUES(VAL1,VAL2,VAL3,VAL4,VAL5,VAL6);");
    }

  @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   }
 }
bond007
  • 2,434
  • 1
  • 17
  • 17
  • Yes, i already have this. I just want the `db.addQuestion` and the `db.addAnswer` to be added from another activity. In this case, `DatabaseContent ` activity, rather than `MainActivity`. – Alex Mamo Nov 12 '15 at 18:13
  • Sorry but i can't edit my post yet. This is [my DBHandler](http://www.gogo.ro/29.txt) – Alex Mamo Nov 12 '15 at 18:18
  • Please also show code for addQuestion and addAnswer – bond007 Nov 12 '15 at 18:30
  • Please refresh the page! You will find there the entire [DBHandler](http://www.gogo.ro/29.txt) – Alex Mamo Nov 12 '15 at 18:40
  • 1
    your addQuestion and addAnswer code seems to be fine.. wat is the exception coming.. can you check with loggers – bond007 Nov 12 '15 at 18:58
  • What does it mean 'check with loggers'? When i turn on the emulator, i get no errors and when the application starts, no question and no answer are displayed. This means that no record was inserted. Am i wrong? – Alex Mamo Nov 12 '15 at 19:03
  • 1
    you are right!!! Reinstall your application long id = db.insert(TABLE_QUESTIONS, null, values); check value of id – bond007 Nov 12 '15 at 19:05
  • Do you have any idea what can be done, to insert those records into the database? – Alex Mamo Nov 12 '15 at 19:06
  • If i use `long id = db.insert(TABLE_QUESTIONS, null, values);` in stead of `db.insert(TABLE_QUESTIONS, null, values);` also nothing happens :| – Alex Mamo Nov 12 '15 at 19:12
  • I don't where to place `long id = db.insert(TABLE_QUESTIONS, null, values);` and where to read that `-1`. Can you please give me an example or edit that text? – Alex Mamo Nov 12 '15 at 19:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94968/discussion-between-zero-and-alex-m). – bond007 Nov 12 '15 at 19:32
0

Can't see your insert and fetch queries but I recommend you follow the correct programming practice of inserting data using Models and implementing the Base Columns class which gives you handy utilities like _ID , Count and other stuff. You can use this tutorial as a walk through

aman shivhare
  • 334
  • 2
  • 13
  • Thanks. Do you have any idea of how can i solve this issue? This is my [DBHandler](http://www.gogo.ro/29.txt) – Alex Mamo Nov 12 '15 at 18:46