-4

I am trying to create a table in sqlite db, but getting the error saying

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learnsocial.testingdatabase/com.learnsocial.testingdatabase.MainActivity}: android.database.sqlite.SQLiteException: near "Index": syntax error (code 1): , while compiling: create table hierarchie (Index int, Robot_Num int, Nom text, C_0 text, C_1 text, C_2 text, C_3 text, C_4 text)

And the code snippet is

public class DbHelper extends SQLiteOpenHelper{

static final String TAG = "DbHelper";
public static final String DB_NAME = "wikwio_idao.db";
public static final int DB_VERSION = 1;

//hierarchie table columns
public static final String TABLE = "hierarchie";
public static final String H_INDEX = "Index";
public static final String H_ROBOT = "Robot_Num";
public static final String H_NOM = "Nom";
public static final String H_C0 = "C_0";
public static final String H_C1 = "C_1";
public static final String H_C2 = "C_2";
public static final String H_C3 = "C_3";
public static final String H_C4 = "C_4";



public DbHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    // TODO Auto-generated constructor stub
}


@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "create table " + TABLE + " (" + H_INDEX + " int, "
            + H_ROBOT + " int, " + H_NOM + " text, " + H_C0 + " text, " + H_C1 + " text, " + H_C2 + " text, " 
            + H_C3 + " text, " + H_C4 + " text)";
    db.execSQL(sql);    
    Log.d(TAG,"oncreate-database");


}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("drop table if exists "+ TABLE); 
    onCreate(db);
}

}

I gave correct space only, but i am not able to find the problem..

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
Karthik Yeruva
  • 155
  • 2
  • 13

3 Answers3

1

"Index" is a reserved word in SQLite. Choose a different name for that column.

http://www.sqlite.org/lang_keywords.html

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • but in my table i need that columns, Is there any possible way that i can do it.. – Karthik Yeruva Jan 28 '15 at 05:31
  • 1
    You can quote the column name between backquotes or with square brackets, but you will have to do it everywhere that column is used and it will be annoying. It's far better to choose a different name for the column. – Karakuri Jan 28 '15 at 05:34
0

index is key word, you can name it like key_index or any thing like that, or try using Index but I am not 100% sure it will help.

I found this, I think it will help you.

Community
  • 1
  • 1
DCoder
  • 3,486
  • 7
  • 36
  • 68
0

You cannot use Index as column name because it is a reserved keyword in sqlite.Try using different name.

Ashok Kateshiya
  • 659
  • 4
  • 10