Cant connect to database

Put your problem here if it does not fit any of the other categories.

Cant connect to database

Postby blues90 » Wed Sep 26, 2012 5:00 pm

Hey there, i really need help now. i try to make an application to store an information to database in SQLite using android eclipse. but i got error when i try to save the data to database.. hope someone can solve my problem:

Code: Select all
public class Semester_1 extends Activity{

   private final String SAMPLE_DB_NAME = "GPA_Calculator";
   private final String SAMPLE_TABLE_NAME = "GPA_Data";
   SimpleCursorAdapter cursorAdapter;
   
   // 1st input
   EditText edtCourseName1_Sem1;
   EditText edtPointer1_Sem1;
   EditText edtCredit1_Sem1;
   CharSequence edtCourseNameValue1_Sem1;
   CharSequence edtPointerValue1_Sem1;
   CharSequence edtCreditValue1_Sem1;
   
   // 2nd input
   EditText edtCourseName2_Sem1;
   EditText edtPointer2_Sem1;
   EditText edtCredit2_Sem1;
   CharSequence edtCourseNameValue2_Sem1;
   CharSequence edtPointerValue2_Sem1;
   CharSequence edtCreditValue2_Sem1;
   
   // 3rd input
   EditText edtCourseName3_Sem1;
   EditText edtPointer3_Sem1;
   EditText edtCredit3_Sem1;
   CharSequence edtCourseNameValue3_Sem1;
   CharSequence edtPointerValue3_Sem1;
   CharSequence edtCreditValue3_Sem1;
   
   // 4th input
   EditText edtCourseName4_Sem1;
   EditText edtPointer4_Sem1;
   EditText edtCredit4_Sem1;
   CharSequence edtCourseNameValue4_Sem1;
   CharSequence edtPointerValue4_Sem1;
   CharSequence edtCreditValue4_Sem1;
   
   
   // 5th input
   EditText edtCourseName5_Sem1;
   EditText edtPointer5_Sem1;
   EditText edtCredit5_Sem1;
   CharSequence edtCourseNameValue5_Sem1;
   CharSequence edtPointerValue5_Sem1;
   CharSequence edtCreditValue5_Sem1;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.semester_1);

       
         Button okButton = (Button) findViewById(R.id.btn_Calculate_Grade_Sem1);
         okButton.setOnClickListener(okOnClickListener);
  }
     private Button.OnClickListener okOnClickListener =
               new Button.OnClickListener(){
         public void onClick(View v) {
                       
            float totalGrade = 0, totalCredit = 0;
            float  GPA = 0;
            float vlPointer = 0, vlCredit = 0;
            String nameCourse = "";
           
           
            //pengiraan 1 input ---------------------------------------------------------------------------
              edtCourseName1_Sem1 = (EditText)findViewById(R.id.edtCourseName1_Sem1);
               edtPointer1_Sem1 = (EditText)findViewById(R.id.edtPointer1_Sem1);
               edtCredit1_Sem1 = (EditText)findViewById(R.id.edtCredit1_Sem1);

             edtCourseNameValue1_Sem1 = edtCourseName1_Sem1.getText();
             edtPointerValue1_Sem1 = edtPointer1_Sem1.getText();
             edtCreditValue1_Sem1 = edtCredit1_Sem1.getText();
             
             try{
               
                vlPointer = Float.parseFloat(edtPointer1_Sem1.getText().toString());
                vlCredit = Integer.parseInt(edtCredit1_Sem1.getText().toString());
             }
             catch (NumberFormatException i){
               // Toast.makeText(getApplicationContext(), "Wrong Data!", Toast.LENGTH_SHORT).show();
                vlPointer = 0;
                vlCredit = 0;
                nameCourse = "";
             }
             
             totalGrade = totalGrade + (vlPointer * vlCredit);
             totalCredit = totalCredit + vlCredit;
             
             
             //pengiraan 2 input ---------------------------------------------------------------------------
             edtCourseName2_Sem1 = (EditText)findViewById(R.id.edtCourseName2_Sem1);
               edtPointer2_Sem1 = (EditText)findViewById(R.id.edtPointer2_Sem1);
               edtCredit2_Sem1 = (EditText)findViewById(R.id.edtCredit2_Sem1);

             edtCourseNameValue2_Sem1 = edtCourseName2_Sem1.getText();
             edtPointerValue2_Sem1 = edtPointer2_Sem1.getText();
             edtCreditValue2_Sem1 = edtCredit2_Sem1.getText();
             
             try{
               
                vlPointer = Float.parseFloat(edtPointer2_Sem1.getText().toString());
                vlCredit = Integer.parseInt(edtCredit2_Sem1.getText().toString());
             }
             catch (NumberFormatException i){
               // Toast.makeText(getApplicationContext(), "Wrong Data!", Toast.LENGTH_SHORT).show();
                vlPointer = 0;
                vlCredit = 0;
                nameCourse = "";
             }         
             
             totalGrade = totalGrade + (vlPointer * vlCredit);
             totalCredit = totalCredit + vlCredit;
             
             
           //pengiraan 3 input ---------------------------------------------------------------------------
             edtCourseName3_Sem1 = (EditText)findViewById(R.id.edtCourseName3_Sem1);
               edtPointer3_Sem1 = (EditText)findViewById(R.id.edtPointer3_Sem1);
               edtCredit3_Sem1 = (EditText)findViewById(R.id.edtCredit3_Sem1);

             edtCourseNameValue3_Sem1 = edtCourseName3_Sem1.getText();
             edtPointerValue3_Sem1 = edtPointer3_Sem1.getText();
             edtCreditValue3_Sem1 = edtCredit3_Sem1.getText();
             
             try{
               
                vlPointer = Float.parseFloat(edtPointer3_Sem1.getText().toString());
                vlCredit = Integer.parseInt(edtCredit3_Sem1.getText().toString());
             }
             catch (NumberFormatException i){
               // Toast.makeText(getApplicationContext(), "Wrong Data!", Toast.LENGTH_SHORT).show();
                vlPointer = 0;
                vlCredit = 0;
                nameCourse = "";
             }
             
             totalGrade = totalGrade + (vlPointer * vlCredit);
             totalCredit = totalCredit + vlCredit;
             
             //pengiraan 4 input ---------------------------------------------------------------------------
             edtCourseName4_Sem1 = (EditText)findViewById(R.id.edtCourseName4_Sem1);
               edtPointer4_Sem1 = (EditText)findViewById(R.id.edtPointer4_Sem1);
               edtCredit4_Sem1 = (EditText)findViewById(R.id.edtCredit4_Sem1);

             edtCourseNameValue4_Sem1 = edtCourseName4_Sem1.getText();
             edtPointerValue4_Sem1 = edtPointer4_Sem1.getText();
             edtCreditValue4_Sem1 = edtCredit4_Sem1.getText();
             
             try{
               
                vlPointer = Float.parseFloat(edtPointer4_Sem1.getText().toString());
                vlCredit = Integer.parseInt(edtCredit4_Sem1.getText().toString());
             }
             catch (NumberFormatException i){
               // Toast.makeText(getApplicationContext(), "Wrong Data!", Toast.LENGTH_SHORT).show();
                vlPointer = 0;
                vlCredit = 0;
                nameCourse = "";
             }
             
             totalGrade = totalGrade + (vlPointer * vlCredit);
             totalCredit = totalCredit + vlCredit;
             
             //pengiraan 5 input ---------------------------------------------------------------------------
             edtCourseName5_Sem1 = (EditText)findViewById(R.id.edtCourseName5_Sem1);
               edtPointer5_Sem1 = (EditText)findViewById(R.id.edtPointer5_Sem1);
               edtCredit5_Sem1 = (EditText)findViewById(R.id.edtCredit5_Sem1);

             edtCourseNameValue5_Sem1 = edtCourseName5_Sem1.getText();
             edtPointerValue5_Sem1 = edtPointer5_Sem1.getText();
             edtCreditValue5_Sem1 = edtCredit5_Sem1.getText();
             
             try{
               
                vlPointer = Float.parseFloat(edtPointer5_Sem1.getText().toString());
                vlCredit = Integer.parseInt(edtCredit5_Sem1.getText().toString());
             }
             catch (NumberFormatException i){
               // Toast.makeText(getApplicationContext(), "Wrong Data!", Toast.LENGTH_SHORT).show();
                vlPointer = 0;
                vlCredit = 0;
                nameCourse = "";
             }
             
             totalGrade = totalGrade + (vlPointer * vlCredit);
             String Final_totalGrade = String.format("%.2f", totalGrade);
             totalCredit = totalCredit + vlCredit;             
             
             
             GPA = totalGrade / totalCredit;
             String Final_GPA = String.format("%.2f", GPA);

             
             TextView result = (TextView) findViewById(R.id.txtTotalView);
             result.setText("Total Grade : "  + Final_totalGrade
                   + "\nTotal Credit: " + totalCredit
                   + "\n GPA: " + Final_GPA);   
             
             
             ADD();
         }
     };
     
     public void ADD(){   
         
        SQLiteDatabase sampleDB = null;
         
         try {
            sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

            //CREATE TABLE
            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                  SAMPLE_TABLE_NAME +
                  " (Semester VARCHAR, Subject VARCHAR, Pointer VARCHAR," +
                  " Credit VARCHAR);");
            
            //INSERT DATA 1
            sampleDB.execSQL("INSERT INTO " +
                  SAMPLE_TABLE_NAME +
                  " Values ('1', '" + edtCourseNameValue1_Sem1 + "', '" + edtPointerValue1_Sem1 + "', '" + edtCreditValue1_Sem1 + "');");
            
            //INSERT DATA 2
            sampleDB.execSQL("INSERT INTO " +
                  SAMPLE_TABLE_NAME +
                  " Values ('1', '" + edtCourseNameValue2_Sem1 + "', '" + edtPointerValue2_Sem1 + "', '" + edtCreditValue2_Sem1 + "');");
            
            
            //INSERT DATA 3
            sampleDB.execSQL("INSERT INTO " +
                  SAMPLE_TABLE_NAME +
                  " Values ('1', '" + edtCourseNameValue3_Sem1 + "', '" + edtPointerValue3_Sem1 + "', '" + edtCreditValue3_Sem1 + "');");

            //INSERT DATA 4
            sampleDB.execSQL("INSERT INTO " +
                  SAMPLE_TABLE_NAME +
                  " Values ('1', '" + edtCourseNameValue4_Sem1 + "', '" + edtPointerValue4_Sem1 + "', '" + edtCreditValue4_Sem1 + "');");
            
            //INSERT DATA 5
            sampleDB.execSQL("INSERT INTO " +
                  SAMPLE_TABLE_NAME +
                  " Values ('1', '" + edtCourseNameValue5_Sem1 + "', '" + edtPointerValue5_Sem1 + "', '" + edtCreditValue5_Sem1 + "');");
            
            
            
            /**

            //System.out.println("UPDATE DATA 3");
            //sampleDB.execSQL("UPDATE " + SAMPLE_TABLE_NAME + " SET Age = 21 WHERE FirstName = 'Collabera' ;");
         
            //SELECT/VIEW DATA
            Cursor c = sampleDB.rawQuery("SELECT SUM(Credit) as TC, SUM(Credit*Pointer) as TTPC, Subject FROM " + SAMPLE_TABLE_NAME +
                  " WHERE Semester = 1", null);
            
            String subject = null;
            String totalCredit = null;
            String totalCreditPointer = null;
            
            if (c != null ) {
               if  (c.moveToFirst()) {
                  do {
                     subject = c.getString(c.getColumnIndex("Subject"));
                     totalCredit = c.getString(c.getColumnIndex("TC"));
                     totalCreditPointer = c.getString(c.getColumnIndex("TTPC"));                    
                  }while (c.moveToNext());
               }
            }
            
            int ttotalCredit = Integer.parseInt(totalCredit);
            int ttotalCreditPointer = Integer.parseInt(totalCreditPointer);

            int totalGrade = ttotalCreditPointer / ttotalCredit;
            TextView display = (TextView) findViewById(R.id.contentlist);
            display.setText("Subject : " + subject + "\nPointer : " + totalCredit + "\nCredit : " + totalCreditPointer + "\nGPA : " + totalGrade);
            **/
         } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
         }

     }
}

blues90
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Sep 26, 2012 4:51 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests