请不要投票我的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";
}
}
答案 0 :(得分:0)
试试这个,
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + LyricsColumns.NAME);
onCreate(db);
}
答案 1 :(得分:0)
您的代码问题:
无需为
MusicDB
类创建静态实例。使用
onCreate, onUpgrade and default callback
类本身中的MusicDb
。从光标读取值时,需要迭代。检查
getLyrics
类中的代码LyricsStore
方法。
改变档案。
MusicDb文件
public class MusicDB extends SQLiteOpenHelper { private static final String DB_NAME = "MusicDb"; private static int mDbVersion = 1; public MusicDB(Context context) { super(context, DB_NAME, null, mDbVersion); Log.v(MusicDB.class.getSimpleName(), "Called music db constructor"); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS " + DbColumns.MusicDb.NAME + " (" + DbColumns.MusicDb.ID + " LONG NOT NULL," + DbColumns.MusicDb.Lyrics + " STRING NOT NULL);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DbColumns.MusicDb.NAME); onCreate(db); } }
LyricsStore文件
public class LyricsStore { private static LyricsStore sInstance = null; private MusicDB mMusicDatabase = null; private LyricsStore(final Context context) { mMusicDatabase = new MusicDB(context); } public static final synchronized LyricsStore getInstance(final Context context) { if (sInstance == null) { sInstance = new LyricsStore(context.getApplicationContext()); } return sInstance; } int result = 0; public boolean AddUpdateLyrics(long SongID, String SongLyrics) { final SQLiteDatabase database = mMusicDatabase.getWritableDatabase(); database.beginTransaction(); Cursor c = getData(database, SongID); if (c == null) { database.endTransaction(); return false; } if (c.getCount() > 0) { ContentValues contentValues = new ContentValues(); contentValues.put(DbColumns.MusicDb.ID, SongID); contentValues.put(DbColumns.MusicDb.Lyrics, SongLyrics); result = database.update(DbColumns.MusicDb.NAME, contentValues, DbColumns.MusicDb.ID + "=?", new String[]{Long.toString(SongID)}); } else { ContentValues contentValues = new ContentValues(); contentValues.put(DbColumns.MusicDb.ID, SongID); contentValues.put(DbColumns.MusicDb.Lyrics, SongLyrics); result = (int) database.insert(DbColumns.MusicDb.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(DbColumns.MusicDb.NAME, new String[]{DbColumns.MusicDb.ID,DbColumns.MusicDb.Lyrics}, DbColumns.MusicDb.ID + "=?", new String[]{String.valueOf(songid)}, null, null, null); if (res == null) return ""; if (res.getCount() > 0) { // This is the place you made the mistake. you need to iterate and then try to get the value. while (res.moveToNext()) { lyrics = res.getString(res.getColumnIndex(DbColumns.MusicDb.Lyrics)); } } } catch (Exception ex) { ex.printStackTrace(); } return lyrics; } public Cursor getData(SQLiteDatabase database, long id) { Cursor res = null; String error = ""; try { res = database.query(DbColumns.MusicDb.NAME, new String[]{DbColumns.MusicDb.ID, DbColumns.MusicDb.Lyrics}, DbColumns.MusicDb.ID + "=?", new String[]{String.valueOf(id)}, null, null, null); } catch (Exception ex) { error = ex.getMessage(); } return res; } }
DbColumns文件
public class DbColumns { public abstract class MusicDb implements BaseColumns { public static final String NAME = "lyricsstore"; public static final String ID = "songid"; public static final String Lyrics = "lyrics"; } }
供参考在github中查看此project。