android.database.sqlite.SQLiteException:near" tableMy_table&#34 ;:语法错误(代码1),新手在这里

时间:2016-04-14 21:08:47

标签: android-studio android-sqlite

我不知道是什么问题。对不起,我是一个新手,对此不太了解。

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Class.db";
public static final String TABLE_NAME = "My_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "OLD_NAME";
public static final String COL_3 = "NEW_NAME";
public static final String COL_4 = "DIRECTION";
public static final String COL_5 = "NOTES";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,OLD_NAME TEXT, NEW_NAME TEXT, DIRECTION TEXT, NOTES TEXT)");
}

这里是logcat

android.database.sqlite.SQLiteException: near "tableMy_table": syntax error (code 1): , while compiling: create tableMy_table(ID INTEGER PRIMARY KEY AUTOINCREMENT,OLD_NAME TEXT, NEW_NAME TEXT, DIRECTION TEXT, NOTES TEXT)

1 个答案:

答案 0 :(得分:0)

嘿,我写过这堂课。根据您的要求进行修改。

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "PersonsDB";
    public static final String TABLE_NAME = "Persons";
    public static final String COLUMN_NAME ="name";
    public static final String COLUMN_ADD = "address";
    public static final String COLUMN_ID = "_id";
    public static final String  COLUMN_AVAILABLE="available";

    private static final int DB_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context,DB_NAME,null,DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE " +TABLE_NAME
                +"(" +COLUMN_ID+
                " INTEGER PRIMARY KEY AUTOINCREMENT, " +COLUMN_NAME+
                " VARCHAR, " +COLUMN_ADD+
                " VARCHAR,"+COLUMN_AVAILABLE+" BOOLEAN);";
        db.execSQL(sql);

    }
    //penOrCreateDatabase
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS Persons";
        db.execSQL(sql);
        onCreate(db);
    }
}

注意:您应该将ID定义为“_id”,否则您将收到错误。