android数据库更新无法正常工作

android数据库更新无法正常工作,android,performance,sqlite,android-studio,android-fragments,Android,Performance,Sqlite,Android Studio,Android Fragments,请不要投我的票。我是安卓版的nube。 我正在开发一个应用程序,我在其中调用web服务。结果我想存储在android sql数据库中。它显示保存了一条记录,但当我试图检索该记录时,它没有显示任何值。我想不出是什么地方出了问题 public class LyricsStore { private static LyricsStore sInstance = null; private MusicDB mMusicDatabase = null; public Lyric

请不要投我的票。我是安卓版的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";
    }
}
试试这个

  public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + LyricsColumns.NAME);
    onCreate(db);
 }
代码的问题:

不需要为MusicDB类创建静态实例

在MusicDb类本身中使用onCreate、onUpgrade和default回调

当从游标读取值时,需要迭代。检查LyricsStore类中的code GetLyps方法

修改过的文件

音乐CDB文件

LyricsStore文件

DbColumns文件


为了便于参考,请在github中查看此文件。

您能显示MusicDB文件吗?这是一个简单的类,我在其中声明public void onCreateSQLiteDatabase db{LyricsStore.GetInstanceContext.onCreatedb;为什么您不扩展SQLiteOpenHelper?我确实在MusicDB类中扩展了它谢谢@mahendran,当res.moveToNext{part时我错过了这个问题。现在它来了/ 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); } } 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; } } 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"; } }