android.database.sqlite.SQLiteException:靠近“tableplayers”:语法错误(代码1)

时间:2016-09-14 08:53:48

标签: android sqlite android-sqlite

单击插入按钮

时发生以下错误

CustomOpenHelper.java

public class CustomOpenHelper extends SQLiteOpenHelper{

public static final String TABLE_NAME = "players";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_SCORE = "score";

private static final String DATABASE_CREATE = "create table" + TABLE_NAME
        + "(" + COLUMN_ID + "integer primary key autoincrement,"
        + COLUMN_NAME +"text not null,"
        + COLUMN_SCORE +"integer not null);";


public CustomOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
}

logcat的

  

android.database.sqlite.SQLiteException:靠近“tableplayers”:语法错误(代码1):,同时编译:create tableplayers(_idinteger主键autoincrement,nametext not null,scoreinteger not null);

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您需要在创建命令中的tableTABLE_NAME之间提供一个空格('')。

private static final String DATABASE_CREATE = "create table " + TABLE_NAME
        + "(" + COLUMN_ID + " integer primary key autoincrement,"
        + COLUMN_NAME +" text not null,"
        + COLUMN_SCORE +" integer not null);";

注意"create table "中每个column的数据类型之前的空间。

答案 1 :(得分:-2)

尝试使用创建表格(如果不存在)而不是创建表格。并且还在create table和"之间留出空间("。修改后的查询应该看起来像

private static final String DATABASE_CREATE = "create table if not exists " + TABLE_NAME
        + " (" + COLUMN_ID + " integer primary key autoincrement,"
        + COLUMN_NAME +" text not null,"
        + COLUMN_SCORE +" integer not null);";