Reputation: 1
I have tried to implement a database in android and while running my application it is getting stopped without entering in to the activities. I am giving my log cat below,I need some good suggestions to overcome the error.
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)
It says about a syntax error I think but I couldn't find any ..giving the code below
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);
}
}
Upvotes: 0
Views: 137
Reputation: 29436
Proper spacing and formatting, will make life easier for you and any one who reads your code:
private static final String DATABASE_CREATE =
" CREATE TABLE " + TABLE_COMMENTS
+ " ( "
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_COMMENT + " TEXT NOT NULL "
+ " )";
And here too:
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_COMMENTS);
Upvotes: 1
Reputation: 23638
You just need to add the space after "create table "
also after the "COLUMN_ID+" integer "
and after the "COUMN_COMMENT "+" text..."
as below:
private static final String DATABASE_CREATE="create table "+TABLE_COMMENTS+" ("+COLUMN_ID+" integer primary key autoincrement "+COLUMN_COMMENT+" text not null);";
Upvotes: 0
Reputation: 24630
Probably you need some spaces at least here:
... COLUMN_ID+" "+"integer primary key autoincrement"+ ...
Upvotes: 1
Reputation: 82543
You're missing a few spaces in your create statement.
Try using:
private static final String DATABASE_CREATE="create table " + TABLE_COMMENTS + " (" + COLUMN_ID + " integer primary key autoincrement " + COLUMN_COMMENT + " text not null);"
Upvotes: 1