androidandroid-sqliteandroid-runtime

RuntimeException: Unable to resume activity


I am newbie in Android and trying to create a database with two tables, but when I fire my project, I got the following errors in logcat:

 05-04 11:21:29.678    1634-1634/com.example.keco.myapplication E/SQLiteLog﹕ 

(1) near "TABLEstudents": syntax error
05-04 11:21:29.680    1634-1634/com.example.keco.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.keco.myapplication, PID: 1634
    java.lang.RuntimeException: Unable to resume activity {com.example.keco.myapplication/com.example.keco.myapplication.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEstudents": syntax error (code 1): , while compiling: CREATE TABLEstudents(_id INTEGER PRIMARY KEY AUTOINCREMENT, _name VARCHAR(30))
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2951)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.database.sqlite.SQLiteException: near "TABLEstudents": syntax error (code 1): , while compiling: CREATE TABLEstudents(_id INTEGER PRIMARY KEY AUTOINCREMENT, _name VARCHAR(30))
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            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:1674)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
            at com.example.keco.myapplication.SqlGenerator.onCreate(SqlGenerator.java:38)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
            at com.example.keco.myapplication.DatabaseSource.<init>(DatabaseSource.java:17)
            at com.example.keco.myapplication.MainActivity.onResume(MainActivity.java:63)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
            at android.app.Activity.performResume(Activity.java:6019)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Here is the class that extends SQLiteOpenHelper:

  package com.example.keco.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

/**
 * Created by Keco on 04/05/2015.
 */
public class SqlGenerator extends SQLiteOpenHelper {
    private static final String STUDENTS_TABLE="students";
    private static final String COLUMN_name="_name";
    private static final String DB_NAME="school.db";
    private static final int DB_VERSION=1;


    public static final String CREATE_STUDENTS_TABLE=
            " CREATE TABLE"+ STUDENTS_TABLE+"("+ BaseColumns._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
                                  COLUMN_name+" VARCHAR(30)) ";



    public SqlGenerator (Context context){
        super(context,DB_NAME,null,DB_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(CREATE_STUDENTS_TABLE);


    }

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

    }
}

and as a style to clean my code I declare the SqlGenerator object in separate class as following:

package com.example.keco.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Keco on 04/05/2015.
 */
public class DatabaseSource {

    private Context mContext;
    private SqlGenerator mSqlGenerator;

    public DatabaseSource (Context context){
        mContext=context;
        mSqlGenerator=new SqlGenerator(context);
        SQLiteDatabase database=mSqlGenerator.getReadableDatabase();
        database.close();
    }

}

and in MainActivity I declare and initialize the DatabaseSource object as following:

package com.example.keco.myapplication;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {



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


    }


    @Override
    protected void onResume() {
        super.onResume();

       DatabaseSource databaseSource=new DatabaseSource(this);

    }


}

waiting for your appreciated help...


Solution

  • As the error message tells you, your CREATE TABLE statement is missing necessary whitespace

    Replace

    " CREATE TABLE"+ STUDENTS_TABLE+"..."
    

    with

    " CREATE TABLE " + STUDENTS_TABLE + "..."
    //            ^