在SQLite中创建表时运行时异常(创建表已停止)

时间:2016-10-11 10:11:45

标签: android sqlite android-sqlite

  

这是我创建的DatabaseHelper calss

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABAE_NAME = "Student.db";
public static final String TABLE_NAME= "Student_table";
public static final String COL_1="ID";
public static final String COL_2="NAMME";
public static final String COL_3="SURNAME";
public static final String COL_4="MARKS";

public DatabaseHelper(Context context) {
    super(context, DATABAE_NAME, null, 1);
    SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME+" (ID INTEGER PRIMARY KEY ,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
    onCreate(db);

}

}

  这是我的主要课程       公共类MainActivity扩展AppCompatActivity {       DatabaseHelper myDB;

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

}

  

这是logcat

 10-11 15:34:25.793 3807-3807/user_profile.createtable E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: user_profile.createtable, PID: 3807
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{user_profile.createtable/user_profile.createtable.MainActivity}: android.database.sqlite.SQLiteException: near "tableStudent_table": syntax error (code 1): , while compiling: create tableStudent_table (ID INTEGER PRIMARY KEY ,NAME TEXT,SURNAME TEXT,MARKS INTEGER)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                        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:905)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                     Caused by: android.database.sqlite.SQLiteException: near "tableStudent_table": syntax error (code 1): , while compiling: create tableStudent_table (ID INTEGER PRIMARY KEY ,NAME TEXT,SURNAME TEXT,MARKS INTEGER)
                                                                        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 user_profile.createtable.DatabaseHelper.onCreate(DatabaseHelper.java:26)
                                                                        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                        at user_profile.createtable.DatabaseHelper.<init>(DatabaseHelper.java:21)
                                                                        at user_profile.createtable.MainActivity.onCreate(MainActivity.java:13)
                                                                        at android.app.Activity.performCreate(Activity.java:6010)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                        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:905) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

10-11 15:39:26.133 3807-3807 / user_profile.createtable I / Process:发送信号。 PID:3807 SIG:9

3 个答案:

答案 0 :(得分:2)

变化

<div><!--block-->&nbsp; &nbsp; Some text is here&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; - Another text&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; - More text&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>

- non-breaking space
- non-breaking space
- text data
- br
- non-breaking space
...
- non-breaking space

表后没有空格导致问题

还有 变化

db.execSQL("create table" + TABLE_NAME+" (ID INTEGER PRIMARY KEY ,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");

db.execSQL("create table " + TABLE_NAME+" (ID INTEGER PRIMARY KEY ,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");

这不是此处显示的错误,但这将是一个问题

答案 1 :(得分:0)

我认为你应该在“创建表”和表名

之间添加一个空格

答案 2 :(得分:0)

在“”创建表格“”查询

的表格旁边放一个空格