请不要投票我的qus。我在Android的nube。 我正在开发一个应用程序,我正在调用Web服务。和结果我想存储在Android的SQL数据库。它显示有一条记录被保存,但当我试图检索该记录时,它没有显示任何值。我无法弄清楚它出了什么问题。安卓数据库更新无法正常工作
public class LyricsStore {
private static LyricsStore sInstance = null;
private MusicDB mMusicDatabase = null;
public LyricsStore(final Context context) {
mMusicDatabase = MusicDB.getInstance(context);
}
public static final synchronized LyricsStore getInstance(final Context context) {
if (sInstance == null) {
sInstance = new LyricsStore(context.getApplicationContext());
}
return sInstance;
}
public void onCreate(final SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + LyricsColumns.NAME + " ("
+ LyricsColumns.ID + " LONG NOT NULL,"
+ LyricsColumns.Lyrics + " STRING NOT NULL);");
}
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + LyricsColumns.NAME);
onCreate(db);
}
int result = 0;
public boolean AddUpdateLyrics(long SongID, String SongLyrics) {
final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
database.beginTransaction();
//onCreate(database);
Cursor c = getData(database, SongID);
if (c == null)
return false;
if (c.getCount() > 0) {
ContentValues contentValues = new ContentValues();
//contentValues.put(LyricsColumns.ID, SongID);
contentValues.put(LyricsColumns.Lyrics, "this is test");
result = database.update(LyricsColumns.NAME, contentValues, LyricsColumns.ID + "=?", new String[]{Long.toString(SongID)});
} else {
ContentValues contentValues = new ContentValues();
contentValues.put(LyricsColumns.ID, SongID);
contentValues.put(LyricsColumns.Lyrics, SongLyrics);
result = (int) database.insert(LyricsColumns.NAME, null, contentValues);
}
database.setTransactionSuccessful();
database.endTransaction();
return true;
}
public String getLyrics(long songid) {
String lyrics = "";
Cursor res = null;
try {
SQLiteDatabase db = mMusicDatabase.getReadableDatabase();
res= db.query(LyricsColumns.NAME,
new String[]{LyricsColumns.ID,LyricsColumns.Lyrics}, null, null, null, null,
LyricsColumns.ID + " DESC", "10");
//res = db.rawQuery("select * from " + LyricsColumns.NAME + " where "+ LyricsColumns.ID + "=" + songid + "", null);
if (res == null)
return "";
if (res.getCount() > 0) {
lyrics = res.getString(res.getColumnIndex(LyricsColumns.Lyrics));
}
} catch (Exception ex) {
}
return lyrics;
}
public Cursor getData(SQLiteDatabase database, long id) {
Cursor res = null;
String error = "";
try {
SQLiteDatabase db = mMusicDatabase.getReadableDatabase();
res = database.rawQuery("select * from " + LyricsColumns.NAME + " where songid=" + id + "", null);
} catch (Exception ex) {
error = ex.getMessage();
}
return res;
}
public interface LyricsColumns {
String NAME = "lyricsstore";
String ID = "songid";
String Lyrics = "lyrics";
}
}
你能显示MusicDB文件吗? –
这是简单的类,其中我声明公共无效onCreate(SQLiteDatabase分贝){LyricsStore.getInstance(mContext).onCreate(db); –
为什么你没有扩展SQLiteOpenHelper? –