Idon89
Idon89

Reputation: 141

My android application crash after i try to open the sqlite database

When i finish filling all my forms and try to create the database, My application crashes.

To create the database, I worked closely with the youtube tutorial: https://www.youtube.com/watch?v=zH7dmLjUrPA

I sew on the debuger that everytime i come to this line ( SQLiteDatabase SQ = dop.getWritableDatabase(); - At DataOperations.java ) The app crashes.

Heres my codes: DatabaseOperations.java:

package com.example.ido.grades;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version =1;
public String CREATE_QUERY = "CREATE_TABLE " + TableData.TableInfo.TABLE_NAME + "("+ TableData.TableInfo.YEAR+" TEXT,"
        + TableData.TableInfo.SEMESTER+" TEXT,"+ TableData.TableInfo.COURSE+" TEXT,"+ TableData.TableInfo.POINTS+" TEXT,"
        + TableData.TableInfo.GRADE+" TEXT);";
public DatabaseOperations(Context context) {
    super(context, TableData.TableInfo.DATABASE_NAME , null, database_version);
    Log.d("DatabaseOperations", "DataBase created");
}

@Override
public void onCreate(SQLiteDatabase sdb) {

    sdb.execSQL(CREATE_QUERY);
    Log.d("DatabaseOperations", "Table created");

}

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

}
public void putInformation(DatabaseOperations dop, String year, String semester, String course, String grade, String points){

    SQLiteDatabase SQ =  dop.getWritableDatabase();  //  <------The line that causes crash
    ContentValues cv = new ContentValues();
    cv.put(TableData.TableInfo.YEAR, year);
    cv.put(TableData.TableInfo.SEMESTER, semester);
    cv.put(TableData.TableInfo.COURSE, course);
    cv.put(TableData.TableInfo.POINTS,points);
    cv.put(TableData.TableInfo.GRADE,grade);

    long k =  SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d("DatabaseOperations", "One raw inserted");

}
}

NewCourseActivity.java:

package com.example.ido.grades;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class NewCourseActivity extends ActionBarActivity {
EditText COURSE, POINTS, GRADE;
Spinner YEAR, SEMESTER;
String year, semester, course,  points, grade;
Context ctx;


ArrayAdapter<String> yearAdapter;
ArrayAdapter<String> SemesterAdapter;
protected void onCreate(Bundle savedInstanceState) {
    ctx=this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_course);
    COURSE= (EditText)findViewById(R.id.courseText);
    GRADE= (EditText)findViewById(R.id.gradeText);
    POINTS= (EditText)findViewById(R.id.pointsText);

    YEAR = (Spinner)findViewById(R.id.spinnerYear);
    String[] yearItems = new String[]{"select year","1", "2", "3"};
     yearAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yearItems);
    YEAR.setAdapter(yearAdapter);

    SEMESTER = (Spinner)findViewById(R.id.spinnerSemester);
    String[] SemesterItems = new String[]{"select semeste","a'", "b'", "c"};
    SemesterAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SemesterItems);
    SEMESTER.setAdapter(SemesterAdapter);



    }

    public void save (View view){
         year = YEAR.getSelectedItem().toString();
         semester =SEMESTER.getSelectedItem().toString();
         course = COURSE.getText().toString();
         points= POINTS.getText().toString();
         grade = GRADE.getText().toString();

        if (year==null || year =="select year"){
            Toast.makeText(this, "select year",Toast.LENGTH_SHORT).show();
        }
        else  if (semester==null || semester=="select semester"){
            Toast.makeText(this, "select semester",Toast.LENGTH_SHORT).show();
        }
        else  if (course.equals("")){
            Toast.makeText(this, "select course",Toast.LENGTH_SHORT).show();
        }
        else  if (Integer.parseInt(points)>6 && Integer.parseInt(points) <0){
            Toast.makeText(this, "points between 0-6.",Toast.LENGTH_SHORT).show();
        }
        else  if (Integer.parseInt(grade)>100 && Integer.parseInt(grade) <0){
            Toast.makeText(this, "grade between 0-100.",Toast.LENGTH_SHORT).show();
        }

        else {

           DatabaseOperations DB  = new DatabaseOperations(NewCourseActivity.this);
            DB.putInformation(DB,year,semester,course,points,grade);
          //  DB.putInformation(DB, year, semester, course, points, grade);


            Toast.makeText(this, "saved succesfully", Toast.LENGTH_SHORT).show();
            Intent intent  = new  Intent (NewCourseActivity.this,MainActivity.class);
            finish();
            startActivity(intent);

        }
    }
    }

TebleData.java:

package com.example.ido.grades;
import android.provider.BaseColumns;
public class TableData {
public TableData(){

}
public static abstract class TableInfo implements BaseColumns{
public static final String YEAR = "year";
public static final String SEMESTER = "semester";
public static final String COURSE = "course";
public static final String  POINTS= "points";
public static final String GRADE = "grade";
public static final String DATABASE_NAME = "add_new_course";
public static final String TABLE_NAME = "add_course";
}
}

logcat:

08-05 18:13:33.711  12931-12931/com.example.ido.grades E/CliptrayUtils﹕ hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
08-05 18:13:35.157      343-433/? E/ThermalEngine﹕ [GPU_MON] 0 percent. Current Sampling Time is 1 sec
08-05 18:13:35.612  12931-12931/com.example.ido.grades E/SQLiteLog﹕ (1) near "CREATE_TABLE": syntax error
08-05 18:13:35.614  12931-12931/com.example.ido.grades E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.ido.grades, PID: 12931
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4014)
            at android.view.View.performClick(View.java:4763)
            at android.view.View$PerformClick.run(View.java:19821)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4009)
            at android.view.View.performClick(View.java:4763)
            at android.view.View$PerformClick.run(View.java:19821)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
     Caused by: android.database.sqlite.SQLiteException: near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE add_course(year TEXT,semester TEXT,course TEXT,points TEXT,grade TEXT);
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:894)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:505)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
            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:1775)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1706)
            at com.example.ido.grades.DatabaseOperations.onCreate(DatabaseOperations.java:26)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
            at com.example.ido.grades.DatabaseOperations.putInformation(DatabaseOperations.java:36)
            at com.example.ido.grades.NewCourseActivity.save(NewCourseActivity.java:73)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4009)
            at android.view.View.performClick(View.java:4763)
            at android.view.View$PerformClick.run(View.java:19821)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

Thanks !!!

Upvotes: 0

Views: 1140

Answers (2)

laalto
laalto

Reputation: 152867

near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE add_course(year TEXT,semester TEXT,course TEXT,points TEXT,grade TEXT);

Replace the underscore _ with a regular space in your CREATE TABLE to fix the syntax error.

Upvotes: 1

Lucifer
Lucifer

Reputation: 29672

Here is your mistake

DatabaseOperations DB  = new DatabaseOperations(Rigstretion.this);

Change this line to

DatabaseOperations DB  = new DatabaseOperations(NewCourseActivity.this);

Upvotes: 1

Related Questions