内容值无法访问value.put();

时间:2018-03-20 17:14:18

标签: java android xml

我想要实现Sqlite,以便在我的Android应用程序中保存数据。 (https://developer.android.com/training/data-storage/sqlite.html#WriteDbRow) 但是,我无法通过使用内容

简单地创建新的值映射
 public static final class FeedReaderContract {
        // To prevent someone from accidentally instantiating the contract class,
        // make the constructor private.
        private FeedReaderContract() {}

        /* Inner class that defines the table contents */
        public static class FeedEntry implements BaseColumns {
            public static final String TABLE_NAME = "entry";
            public static final String COLUMN_NAME_TITLE = "title";
            public static final String COLUMN_NAME_SUBTITLE = "subtitle";
        }


        public static final String SQL_CREATE_ENTRIES =
                "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
                        FeedEntry._ID + " INTEGER PRIMARY KEY," +
                        FeedEntry.COLUMN_NAME_TITLE + " TEXT," +
                        FeedEntry.COLUMN_NAME_SUBTITLE + " TEXT)";

        public static final String SQL_DELETE_ENTRIES =
                "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
    }
    public class FeedReaderDbHelper extends SQLiteOpenHelper {


        // If you change the database schema, you must increment the database version.
        public static final int DATABASE_VERSION = 1;
        public static final String DATABASE_NAME = "FeedReader.db";

        public FeedReaderDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_DELETE_ENTRIES);
        }
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // This database is only a cache for online data, so its upgrade policy is
            // to simply to discard the data and start over
            db.execSQL(SQL_DELETE_ENTRIES);
            onCreate(db);
        }
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }


        FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getBaseContext());
        SQLiteDatabase db= mDbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
         values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
        values.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle);



    }

Android studio自动更正第一个到values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,title); 这也会导致错误:未知类; 第二个说:未知的符号。 顺便说一句,如果你知道在Android工作室使用sqlite我可以学习的任何好的和简单的资源,如果你分享它我会很感激

1 个答案:

答案 0 :(得分:0)

您需要添加一个方法,如下所示:

public boolean insert(String title,String subtitle)
{
    FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getBaseContext());
    SQLiteDatabase db= mDbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME_TITLE, title);
    values.put(COLUMN_NAME_SUBTITLE, subtitle);
    long ins = db.insert("entry",null,values);
    if(ins == -1) return false;
    else return true;

}

基本上,此方法将返回一个布尔值,具体取决于插入是否成功。

哦,你在onCreate()方法中有错误。
替换此代码:

public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_DELETE_ENTRIES);
    }

有了这个:

public void onCreate(SQLiteDatabase db) {
       db.execSQL(SQL_CREATE_ENTRIES);
   }