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

android.database.sqlite.SQLiteException: near "tableplayers": syntax error (code 1)

点击插入按钮出现如下错误

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: near "tableplayers": syntax error (code 1): , while compiling: create tableplayers(_idinteger primary key autoincrement,nametext not null,scoreinteger not null);

如何解决这个问题?

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

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.

的数据类型之前添加的 space

尝试使用 创建 table(如果不存在) 而不是 创建 table。并在创建 table 和“(”之间提供 space。修改后查询应类似于

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);";