我正在尝试在 sqlite 数据库中创建一个表,但收到的错误是
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)
代码段是
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);
}
}
我只提供了正确的空间,但我无法找到问题..
答案 0 :(得分:1)
"指数"是SQLite中的保留字。为该列选择其他名称。
答案 1 :(得分:0)
index
是关键词,您可以将其命名为key_index
或类似的任何内容,或尝试使用Index
,但我并非100%确定它会有所帮助。
我找到this,我认为它会对你有帮助。
答案 2 :(得分:0)
您不能将Index用作列名,因为它是sqlite中的保留关键字。尝试使用不同的名称。