语法错误(代码1):,编译时:CREATE TABLE

时间:2014-12-31 13:39:23

标签: android sql sqlite

我的sql命令中缺少什么?

public class PhoneDal extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = Constants.DB_NAME;

    public static final String BLOCKED_PHONES_TABLE = "BLOCKED_PHONES_TABLE";

    public PhoneDal(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_BLOCKED_PHONES_TABLE =
                "CREATE TABLE "+ BLOCKED_PHONES_TABLE +
                        " ( "+ KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, "
                        + KEY_PHONE+" TEXT, "
                        + KEY_IS_BLOCKED+" BIT," +
                        " UNIQUE "+ KEY_PHONE+" )";

        db.execSQL(CREATE_BLOCKED_PHONES_TABLE);
    }

,错误是

 android.database.sqlite.SQLiteException: near "KEY_PHONE": syntax error (code 1): , while compiling: CREATE TABLE BLOCKED_PHONES_TABLE ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, KEY_PHONE TEXT, KEY_IS_BLOCKED BIT, UNIQUE KEY_PHONE )
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

2 个答案:

答案 0 :(得分:3)

必须在parens中定义

UNIQUE列。变化

" UNIQUE "+ KEY_PHONE+" )";

" UNIQUE ("+ KEY_PHONE+") )";

答案 1 :(得分:1)

您在定义唯一字段时遇到语法错误 请改用此KEY_PHONE + " TEXT UNIQUE"

sqlite中的列定义:
Column definition