1

I am new here (and in the programming in general) and was hoping you could help me with this. When I try to start the app, it just "Stops working". I looked through all the similar questions here, but couldn't solve it. This is my code:

package com.example.me.mac_xyz;

     import android.content.Context;
     import android.database.sqlite.SQLiteDatabase;
     import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "mac.db";
public static final String TABLE_NAME = "1";
public static final String COL_1 = "NUMBER";
public static final String COL_2 = "MAC";
public static final String COL_3 = "X";
public static final String COL_4 = "Y";
public static final String COL_5 = "Z";

/* Constructor*/
public DatabaseHelper(Context context) { /*, String name,   SQLiteDatabase.CursorFactory factory, int version*/
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME +"(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)");

}

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

And this is in the log:

11-12 22:34:47.706  31044-31044/com.example.me.mac_xyz E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.me.mac_xyz, PID: 31044
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.me.mac_xyz/com.example.me.mac_xyz.MainActivity}: android.database.sqlite.SQLiteException: near "table1": syntax error (code 1): , while compiling: create table1(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.sqlite.SQLiteException: near "table1": syntax error (code 1): , while compiling: create table1(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)
        *  *  *

What did I do wrong?

1 Answers1

5

Problem is with your SQL statement. This is how your SQL query looks like to android system :

create table1(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER). 
Change query to : 
create table table_name (NUMBER INTEGER PRIMARY KEY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)

Change your onCreate as :

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + 
               " (NUMBER INTEGER PRIMARY KEY, 
                 MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER);");

}

Note : Table name can not be numeric, hence change it to a valid table name. Refer : What are valid table names in SQLite?

Community
  • 1
  • 1
abhishesh
  • 3,246
  • 18
  • 20
  • Hi and thanks for the suggestion, but I am afraid it didn't work... Do you have any other ideas? I have to make a table with 5 columns and about 80 rows and thought that the SQLite DB is the best way to do it. Do you know any other way for this? – Noob_Trying_Hard Nov 15 '15 at 16:53
  • Wat problem you are facing with db? – abhishesh Nov 15 '15 at 16:54