RuntimeException:无法恢复活动

时间:2015-05-04 16:47:23

标签: android android-sqlite android-runtime

我是Android的新手并尝试创建一个包含两个表的数据库,但是当我解雇我的项目时,我在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)

这是扩展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) {

    }
}

作为清理代码的样式,我在单独的类中声明了SqlGenerator对象,如下所示:

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();
    }

}

并在MainActivity中声明并初始化DatabaseSource对象,如下所示:

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);

    }


}

等待你的赞赏......

2 个答案:

答案 0 :(得分:1)

正如错误消息所示,您的request.POST.get('caption', 'Default'). 语句缺少必要的空格

替换

CREATE TABLE

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

答案 1 :(得分:0)

正如它所说,这是一个语法错误:in the CREATE_STUDENTS_TABLE STRING

public class SqlGenerator extends SQLiteOpenHelper {

     [...]

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

之后应该有一个空格
" CREATE TABLE<here>"   
===>   " CREATE TABLE "+ STUDENTS_TABLE+"...