我试图在 android 中实现一个数据库,并且在运行我的应用程序时它会在没有进入活动的情况下停止。我在下面给出了我的日志猫,我需要一些好的建议来克服这个错误。
08-16 05:40:15.381: E/AndroidRuntime(1096): FATAL EXCEPTION: main
08-16 05:40:15.381: E/AndroidRuntime(1096): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.de.vogella.android.sqlite.first/com.de.vogella.android.sqlite.first.TestDatabaseActivity}: android.database.sqlite.SQLiteException: near "tablecomments": syntax error (code 1): , while compiling: create tablecomments(_idinteger primary key autoincrementcommenttext not null);
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.os.Looper.loop(Looper.java:137)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-16 05:40:15.381: E/AndroidRuntime(1096): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 05:40:15.381: E/AndroidRuntime(1096): at java.lang.reflect.Method.invoke(Method.java:511)
08-16 05:40:15.381: E/AndroidRuntime(1096): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-16 05:40:15.381: E/AndroidRuntime(1096): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-16 05:40:15.381: E/AndroidRuntime(1096): at dalvik.system.NativeStart.main(Native Method)
08-16 05:40:15.381: E/AndroidRuntime(1096): Caused by: android.database.sqlite.SQLiteException: near "tablecomments": syntax error (code 1): , while compiling: create tablecomments(_idinteger primary key autoincrementcommenttext not null);
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-16 05:40:15.381: E/AndroidRuntime(1096): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
它说的是我认为的语法错误,但我找不到任何..给出下面的代码
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_COMMENTS="comments";
public static final String COLUMN_ID="_id";
public static final String COLUMN_COMMENT="comment";
private static final String DATABASE_NAME="comments.db";
private static int DATABASE_VERSION=1;
//Database creation SQL statements
private static final String DATABASE_CREATE="create table"+TABLE_COMMENTS+"
("+COLUMN_ID+"integer primary key autoincrement"+COLUMN_COMMENT+"text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase database) {
// TODO Auto-generated method stub
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(MySQLiteHelper.class.getName(),"upgrading database from version "+
oldVersion+"to"+newVersion+",which will destroy all data");
db.execSQL("DROP TABLE IF EXISTS"+TABLE_COMMENTS);
onCreate(db);
}
}