我想使用SQlite为我的Android应用程序存储一些数据。在尝试运行应用程序后,我收到此错误:
06-21 16:14:30.175 2375-2375/snet.app E/SQLiteLog﹕ (1) no such table: tracks.db
06-21 16:14:30.176 2375-2375/snet.app E/SQLiteDatabase﹕ Error inserting title=Goldchains _id=null artist=Marlin album=Filet Mignon - Volume II
android.database.sqlite.SQLiteException: no such table: tracks.db (code 1): , while compiling: INSERT INTO tracks.db(title,_id,artist,album) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at snet.tuberlin.app.DBHandler.addTrack(DBHandler.java:64)
at snet.tuberlin.app.TrackInformations$1.onReceive(TrackInformations.java:91)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我尝试了在谷歌中找到的所有内容来解决此问题,包括更改数据库版本。它的工作原理但是在尝试再次运行应用程序之后又回到了同样的错误。 这是我的DBHandler类:
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 6;// as you can see i changed the version many time already !!
private static final String DATABASE_NAME = "tracks.db";
public static final String TABLE_TRACKS = "tracks";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_ARTIST = "artist";
public static final String COLUMN_ALBUM = "album";
public static final String COLUMN_LOCATION = "location";
public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
final String DATABASE_CREATE = "create table "
+ TABLE_TRACKS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_TITLE
+ " text not null, " +COLUMN_ARTIST
+ " text not null, " + COLUMN_ALBUM
+ " text not null);";
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRACKS);
onCreate(db);
}
/*Add new row to the DB*/
public void addTrack(UserTrack usertrack) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, usertrack.getUserTrackId());
values.put(COLUMN_TITLE, usertrack.getTitle());
values.put(COLUMN_ARTIST, usertrack.getArtist());
values.put(COLUMN_ALBUM, usertrack.getAlbum());
SQLiteDatabase db = getWritableDatabase();
db.insert(DATABASE_NAME, null, values);
db.close();
}
}
答案 0 :(得分:2)
只需在addTrack方法中更改为db.insert(TABLE_TRACKS, null, values);
。
您提供了db名称而不是表名,因此您收到错误。
更正以下代码
public void addTrack(UserTrack usertrack) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, usertrack.getUserTrackId());
values.put(COLUMN_TITLE, usertrack.getTitle());
values.put(COLUMN_ARTIST, usertrack.getArtist());
values.put(COLUMN_ALBUM, usertrack.getAlbum());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_TRACKS, null, values); <----change here
db.close();
}