Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNiyamat Almass
8,176 Pointsandroid.database.sqlite.SQLiteException: near "TABLEnote_table": syntax error (code 1).............
I am making a simple note taking app.So I made a single database to store note title and note body. But I app isn't running and crash.
The error is
01-04 05:45:38.964 2451-2451/? I/art: Late-enabling -Xcheck:jni 01-04 05:45:39.480 2451-2451/com.example.niyamat.notepad W/System: ClassLoader referenced unknown path: /data/app/com.example.niyamat.notepad-1/lib/x86 01-04 05:45:39.662 2451-2451/com.example.niyamat.notepad E/SQLiteLog: (1) near "TABLEnote_table": syntax error 01-04 05:45:39.662 2451-2451/com.example.niyamat.notepad D/AndroidRuntime: Shutting down VM 01-04 05:45:39.663 2451-2451/com.example.niyamat.notepad E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.niyamat.notepad, PID: 2451 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.niyamat.notepad/com.example.niyamat.notepad.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEnote_table": syntax error (code 1): , while compiling: CREATE TABLEnote_table(IDINTEGER PRIMARY KEY ,NOTE_TITLEVARCHAR,NOTE_BODYVARCHAR) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.database.sqlite.SQLiteException: near "TABLEnote_table": syntax error (code 1): , while compiling: CREATE TABLEnote_table(IDINTEGER PRIMARY KEY ,NOTE_TITLEVARCHAR,NOTE_BODYVARCHAR) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) 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.niyamat.notepad.DataBaseHelper.onCreate(DataBaseHelper.java:32) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251) at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187) at com.example.niyamat.notepad.DataBaseHelper.getAllRecords(DataBaseHelper.java:66) at com.example.niyamat.notepad.MainActivity.onCreate(MainActivity.java:33) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
The database code is
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME + "("
+ NOTE_ID + "INTEGER PRIMARY KEY ,"
+ NOTE_TITLE + "VARCHAR,"
+ NOTE_BODY + "VARCHAR" + ")";
db.execSQL(CREATE_TABLE);
}
I am new to android sqlite, so I can't figure out what's the problem is.Can someone help me?
I also update my project on github.Here is link https://github.com/niyamatalmass/Notepad
1 Answer
Steve Hunter
57,712 PointsOne initial thought (as Android Studio takes ages loading the emulator) - don't you need more spacing in your SQL?
You've got "CREATE TABLE" + TABLE_NAME which will all come out as one string with no string separation. That's what's causing this to fail initially.
Look at your error; the SQL statement being executed is:
CREATE TABLEnote_table(IDINTEGER PRIMARY KEY ,NOTE_TITLEVARCHAR,NOTE_BODYVARCHAR)
There's no spacing so fix that first it may work with that sorted out.
Let me know how you get on.
Steve.
Steve Hunter
57,712 PointsHere's the offending line edited with proper spacing; I'll check it out and see if that solves this issue.
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
+ NOTE_ID + " INTEGER PRIMARY KEY, "
+ NOTE_TITLE + " VARCHAR, "
+ NOTE_BODY + " VARCHAR" + ")";
Steve Hunter
57,712 PointsYep. I tried that and it loads your app. Woohoo! \o/
Steve.
Steve Hunter
57,712 PointsYou have a pull request from my Github fork.
Niyamat Almass
8,176 PointsThanks Steve Hunter
That's really help.
Steve Hunter
57,712 PointsNo problem; happy to help out! :-)
Niyamat Almass
8,176 PointsHi Steve Hunter Need help again.
In my app, the title of note is break and leave a space.
In res/layout/note_list_view you can see this
That's look silly.How to get rid of the blank spaces showing in the picture.
And how to make the title of note good looking.
Also,
When I add text into edit text in NoteTakingActivity ,it start writing in the middle of the edittext.Why middle?How to solve this?
Please help!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsJust forking your github project - I'll see what I can do but SQL is not my strong point!