0

我试图在 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);

       }
     }
4

4 回答 4

1

适当的间距和格式将使您和任何阅读您的代码的人的生活更轻松:

private static final String DATABASE_CREATE =
        " CREATE TABLE " + TABLE_COMMENTS
        + " ( "
        +     COLUMN_ID      + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        +     COLUMN_COMMENT + " TEXT NOT NULL "
        + " )";

这里也是:

db.execSQL(" DROP TABLE IF EXISTS " + TABLE_COMMENTS);
于 2013-08-16T06:10:29.440 回答
1

您的 create 语句中缺少一些空格。

尝试使用:

private static final String DATABASE_CREATE="create table " + TABLE_COMMENTS + " (" + COLUMN_ID + " integer primary key autoincrement " + COLUMN_COMMENT + " text not null);"
于 2013-08-16T05:49:03.427 回答
1

可能您至少在这里需要一些空间:

... COLUMN_ID+" "+"integer primary key autoincrement"+ ...
于 2013-08-16T05:50:10.003 回答
0

您只需要在"create table "之后"COLUMN_ID+" integer " 和之后添加空格"COUMN_COMMENT "+" text...",如下所示:

  private static final String DATABASE_CREATE="create table  "+TABLE_COMMENTS+" 
 ("+COLUMN_ID+" integer primary key autoincrement "+COLUMN_COMMENT+"  text not null);";
于 2013-08-16T05:53:39.200 回答