dimanche 27 décembre 2015

Android: Inserting Data in another table

I'm having problem in inserting data in another table, I created a contacts table and quiz table, I created the another class for quiz table which is AdminContact class and I call the AdminContact class in my DatabaseHelper. But when I save data in my quiz table and I will click the the save button it will only go back to the previous activity, and there's no data that have been save in the sqlite database. please check my codes. thank You..

    AdminContact Class:
        public class AdminContact {

            public static  abstract class Question{

                public static final String COLUMN_QUESTION = "question";
                public static final String COLUMN_ANSWER1 = "answer1";
                public static final String COLUMN_ANSWER2 = "answer2";
                public static final String COLUMN_ANSWER3 = "answer3";
                public static final String COLUMN_ANSWER4 = "answer4";
               public static final String TABLE_QUIZ = "quiz";




            }

        }


    My DatabaseHelper:
    public class DatabaseHelper extends SQLiteOpenHelper {

       // contact table
       private  static final int DATABASE_VERSION = 1;
        private  static final String DATABASE_NAME = "contacts.db";
        private  static final String TABLE_NAME = "contacts";
        private  static final String COLUMN_ID = "id";
        private  static final String COLUMN_NAME = "name";
        private  static final String COLUMN_EMAIL = "email";
        private  static final String COLUMN_UNAME = "uname";
        private  static final String COLUMN_PASS = "pass";

        SQLiteDatabase db;

        private static final String CREATE_QUERY = "CREATE TABLE "+ AdminContact.Question.TABLE_QUIZ+"("+ AdminContact.Question.COLUMN_QUESTION+" TEXT,"+
                AdminContact.Question.COLUMN_ANSWER1+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER2+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER3+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER4+" TEXT);";

        private static final String TABLE_CREATE = "create table contacts (id integer primary key not null ," +
                "name text not null, email text not null, uname text not null, pass text not null);";


        public DatabaseHelper(Context context)
        {
            super(context , DATABASE_NAME , null , DATABASE_VERSION);
           Log.e("DATABASE OPERATION", "Database created");
        }

    public void addQuestion(String question, String answer1, String answer2, String answer3, String answer4, SQLiteDatabase db){

        ContentValues contentValues = new ContentValues();
        contentValues.put(AdminContact.Question.COLUMN_QUESTION, question);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER1, answer1);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER2, answer2);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER3, answer3);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER4, answer4);
        db.insert(AdminContact.Question.TABLE_QUIZ, null,contentValues);
        Log.e("DATABASE OPERATION", "One row inserted");

    }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(TABLE_CREATE);
            db.execSQL(CREATE_QUERY);
            this.db = db;
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
            String query1 = "DROP TABLE IF EXISTS "+AdminContact.Question.TABLE_QUIZ;
            db.execSQL(query);
            db.execSQL(query1);
            this.onCreate(db);

        }

My OnSaveClick Button:


public class Add  extends Activity {

    DatabaseHelper helper;
    SQLiteDatabase sqlitedatabase;
    EditText editTextQuestion, editTextAns1, editTextAns2, editTextAns3, editTextAns4;
    Button clear;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_question);
        helper = new DatabaseHelper(this);

        editTextQuestion = (EditText) findViewById(R.id.TFquestion);
        editTextAns1 = (EditText) findViewById(R.id.TFans1);
        editTextAns2 = (EditText) findViewById(R.id.TFans2);
        editTextAns3 = (EditText) findViewById(R.id.TFans3);
        editTextAns4 = (EditText) findViewById(R.id.TFans4);
}


    public void OnSaveClick(View view)
    {
        String question = editTextQuestion.getText().toString();
        String answer1 = editTextAns1.getText().toString();
        String answer2 = editTextAns2.getText().toString();
        String answer3 = editTextAns3.getText().toString();
        String answer4 = editTextAns4.getText().toString();
        helper = new DatabaseHelper(context);
        sqlitedatabase = helper.getWritableDatabase();
        helper.addQuestion(question,answer1,answer2,answer3,answer4,sqlitedatabase);
        Toast.makeText(getBaseContext(), "Successfully save!",Toast.LENGTH_LONG).show();

        helper.close();
    }
}

Aucun commentaire:

Enregistrer un commentaire