Reputation: 117
i am pretty new to programming in android and here is my first try to create a sqlite database. Every time i call the insert()-Method, i get the error "(1) no such table: songs.db". I searched this error but i couldn't find any solution. Here is my Class:
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");
}
}
Best regards
Upvotes: 1
Views: 1554
Reputation:
The previous answers are correct but keep this tip in your mind to have an eye on the eclipse's tips, see this:
Upvotes: 0
Reputation: 8134
Inserting the row means inside the table. Your query of insert is pointing to the Db, which is explained in the error.
db.insert(TABLE_NAME, null, values);
which is songs
, its looking for a table name - songs.db
instead
Upvotes: 1
Reputation: 924
This
rowId = db.insert(DATABASE_NAME, null, values);
should probably be
rowId = db.insert(TABLE_SONGS, null, values);
Upvotes: 2