SQLite - 如何修复语法错误(代码1)

时间:2015-06-30 18:04:15

标签: android sqlite

我的Input.java文件里面有

public class Input {

    private int _id;
    private String _username;

    public Input(){

    }

    public Input(String username){
        this._username = username;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_username(String _username) {
        this._username = _username;
    }

    public int get_id() {
        return _id;
    }

    public String get_username() {
        return _username;
    }
}

和我的DBHandler.java内部

public class DBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "input.db";
    public static final String TABLE_INPUT = "input";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_USERNAME = "username";

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

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_INPUT + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
            COLUMN_USERNAME +  " TEXT " +
            ");";
    db.execSQL(query);
}

每当我尝试执行此操作时,都会收到错误:

Caused by: android.database.sqlite.SQLiteException: near "username": syntax error (code 1): , while compiling: CREATE TABLE input(_id INTEGER PRIMARY KEY AUTOINCREMENT username TEXT );

我究竟做错了什么?

1 个答案:

答案 0 :(得分:0)

您在用户名前缺少逗号:

" INTEGER PRIMARY KEY AUTOINCREMENT<missing comma here> "

更正后的代码:

 String query = "CREATE TABLE " + TABLE_INPUT + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_USERNAME +  " TEXT " +
            ");";