android.database.sqlite.SQLiteException:table数据没有名为_Y的列

时间:2014-02-22 10:42:56

标签: android sqlite

使用我的数据库时遇到问题。请检查一下:

// Database Name
private static final String DATABASE_NAME = "Poloha";

// Data table name
private static final String DATA = "Data";

// Data Table Columns names
private static final String KEY_Y= "Y";
private static final String KEY_X= "X";
private static final String KEY_ID = "_id";
private static final String KEY_NAZEV = "Nazev";
private static final String KEY_MAC = "MAC";

public void onCreate(SQLiteDatabase db) 
{
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
            + DATA + "("
            + KEY_ID + " INTEGER PRIMARY KEY autoincrement,"
            + KEY_NAZEV + " TEXT NOT NULL,"
            + KEY_MAC + " TEXT NOT NULL," 
            + KEY_X + " TEXT NOT NULL," 
            + KEY_Y + " TEXT NOT NULL);" ;
    db.execSQL(CREATE_CONTACTS_TABLE);
}

现在我想使用以下方法添加一些值:

db.pridejZaznam(new Data(1,1,1,"NAZEV_1","MAC_1"));
db.pridejZaznam(new Data(2,2,2,"NAZEV_2","MAC_2"));
db.pridejZaznam(new Data(3,3,3,"NAZEV_3","MAC_3"));
db.pridejZaznam(new Data(4,4,4,"NAZEV_4","MAC_4"));

问题是,LogCat向我显示了这个错误:

02-22 10:33:26.336: E/SQLiteLog(813): (1) table Data has no column named Y
02-22 10:33:26.375: E/SQLiteDatabase(813): Error inserting Y=1 MAC=MAC_1 Nazev=NAZEV_1 _id=1 X=1
02-22 10:33:26.375: E/SQLiteDatabase(813): android.database.sqlite.SQLiteException: table Data has no column named Y (code 1): , while compiling: INSERT INTO Data(Y,MAC,Nazev,_id,X) VALUES (?,?,?,?,?)

知道我该怎么办?我检查了类似的错误,发现在创建表时只有一个缺少SPACE的解决方案但是如果我不是盲目的话,我没有任何遗漏。谢谢大家:))

4 个答案:

答案 0 :(得分:5)

public void onCreate(SQLiteDatabase db) 
{
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
            + DATA + "("
            + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
            + KEY_NAZEV + " TEXT NOT NULL,"
            + KEY_MAC + " TEXT NOT NULL," 
            + KEY_X + " TEXT NOT NULL," 
            + KEY_Y + " TEXT NOT NULL);";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

答案 1 :(得分:1)

你不需要添加KEY_ID,数据库会自动添加它,从你的插入查询中删除第一个值:

  

db.pridejZaznam(新数据(1,1,“NAZEV_1”,“MAC_1”));

依旧......

并使用英文名称作为方法和变量以获得更好的不足

答案 2 :(得分:1)

您需要在关键字EXISTS和表名称之间留出空格:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
        + DATA + "("

更改为:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " 
        + DATA + "("

编辑表架构后,卸载您的应用,以便再次运行onCreate()。另见:When is SQLiteOpenHelper onCreate() / onUpgrade() run?

答案 3 :(得分:0)

只需删除该应用并重新安装即可。

MYSQLite表存储在本地设备中,并不附加表