Android SQLite(1)没有这样的表

时间:2014-04-01 16:55:19

标签: android sqlite

我对Android中的编程很新,这是我第一次尝试创建一个sqlite数据库。每当我调用insert() - Method时,我都会收到错误“(1)no such table:songs.db”。我搜索了这个错误,但我找不到任何解决方案。这是我的班级:

public class HearOpenHandler extends SQLiteOpenHelper {

public static final String TABLE_SONGS = "songs";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_INTERPRET = "interpret";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_URI = "uri";

private static final String DATABASE_NAME = "songs.db";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE = "create table " 
        + TABLE_SONGS + "(" 
        + COLUMN_ID + " integer primary key autoincrement, " 
        + COLUMN_INTERPRET + " text not null, " 
        + COLUMN_TITLE + " text not null, " 
        + COLUMN_URI + " text not null);";

private static final String DATABASE_DROP = "drop table if exists " + TABLE_SONGS;

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


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DATABASE_DROP);
    onCreate(db);
}


public void insert(String interpret, String title, String uri) {

    long rowId;

    // Open database
    SQLiteDatabase db = getWritableDatabase();

    // Data which should be stored
    ContentValues values = new ContentValues();
    values.put(COLUMN_INTERPRET, interpret);
    values.put(COLUMN_TITLE, title);
    values.put(COLUMN_URI, uri);

    // insert in database
    rowId = db.insert(DATABASE_NAME, null, values);

    if(rowId == -1)
        Log.d(HearOpenHandler.class.getName(), "Error while inserting values into database");

}

}

祝你好运

3 个答案:

答案 0 :(得分:2)

rowId = db.insert(DATABASE_NAME, null, values);

应该是

rowId = db.insert(TABLE_SONGS, null, values);

答案 1 :(得分:1)

插入行意味着在表格内。您对insert的查询指向Db,错误中对此进行了解释。

db.insert(TABLE_NAME, null, values);  

songs,它正在寻找一个表名 - songs.db而不是

答案 2 :(得分:0)

之前的答案是正确的,但请记住这个提示,以便了解日食的提示,请看:

eclipse help