【问题标题】:column ' content ' does not exist [closed]列“内容”不存在[关闭]
【发布时间】:2021-04-18 04:28:36
【问题描述】:

我正在为 sqlite 使用 sqliteOpenHelper 类。我正在学习一本关于概念的安卓书。一切都与书中的代码相同。但在我的情况下,应用程序崩溃并显示错误。请帮我解决这个问题。 在 logcat 中 列内容不存在。当我在设备上运行应用程序时,应用程序崩溃并且 logcat 显示此

Caused by: java.lang.IllegalArgumentException: column ' content ' does not exist

at com.example.remindersapp.ReminderSimpleCursorAdapter.<init>(ReminderSimpleCursorAdapter.java:13)
        at com.example.remindersapp.RemindersActivity.onCreate(RemindersActivity.java:41)

RemindersActivity.java

package com.example.remindersapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class RemindersActivity extends AppCompatActivity {
    private ListView mlistView;
    private RemindersDbAdapter remindersDbAdapter;
    private ReminderSimpleCursorAdapter reminderSimpleCursorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminders);
        mlistView = (ListView) findViewById(R.id.reminder_listView);
        mlistView.setDivider(null);
        remindersDbAdapter = new RemindersDbAdapter(this);
        remindersDbAdapter.open();
        if (savedInstanceState == null) {
            //clear all data
            remindersDbAdapter.deleteAllReminders();
            //add some data
            insertSomeReminders();

        }

        Cursor cursor = remindersDbAdapter.fetchAllReminders();
        //from columns defined in the db
        String[] from = new String[]{remindersDbAdapter.COL_CONTENT};

        //to the ids of views in the layout
        int[] to = new int[]{R.id.row_text};
        reminderSimpleCursorAdapter = new ReminderSimpleCursorAdapter(
                RemindersActivity.this,
                //the layout of the row
                R.layout.reminders_row,
                cursor,
                //from columns defined in the db
                from,
                //to the ids of views in the layout
                to,
                //flag - not used
                0);

//        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
//                this,
//                R.layout.reminders_row,
//                R.id.row_text,
//                new String[]{"fisrt record", "second record", "third record"
//                        , "fourth record", "fifth record"});
        mlistView.setAdapter(reminderSimpleCursorAdapter);
        //Adapter is a
        //special Java class defined as part of the Android SDK that functions as the Controller in
        //the Model-View-Controller relationship
    }

    private void insertSomeReminders() {
        remindersDbAdapter.createReminder("Learn android Development", true);
        remindersDbAdapter.createReminder("data Mining Assignment on 23-04-21", false);
        remindersDbAdapter.createReminder("Networking Assignment on 25-04-2021", false);
        remindersDbAdapter.createReminder("English Assignment on 30-04-2o21", false);
        //  //There are several calls to the createReminder() method, each taking a String value
        //with the reminder text and a boolean value flagging the reminder as important. We set
        //a few values to true to provide a good visual effect.
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.reminder_menu, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_new:
                //create new reminder
                Log.d(getLocalClassName(), "create new reminder");
                return true;
            case R.id.action_exit:
                finish();
                return true;
            default:
                return false;
        }

    }
}

ReminderSimpleCursorAdater.java

package com.example.remindersapp;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;

import androidx.recyclerview.widget.RecyclerView;

public class ReminderSimpleCursorAdapter extends SimpleCursorAdapter {
    public ReminderSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int i) {
        super(context, layout, c, from, to,i);
    }
    ////to use a viewholder, you must override the following two methods and define a ViewHolder class




    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        return super.newView(context, cursor, parent);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        ViewHolder holder=(ViewHolder)view.getTag();
        if (holder == null){
            holder=new ViewHolder();
            holder.colImp=cursor.getColumnIndexOrThrow(RemindersDbAdapter.COL_IMPORTANT);
            holder.listTab=view.findViewById(R.id.row_tab);
            view.setTag(holder);
        }
        if (cursor.getInt(holder.colImp) > 0){
            holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.orange));
        }else {
            holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.txt_color));
        }
    }
    //Here you see an example of the ViewHolder pattern. This is a well-known Android pattern
    //in which a small ViewHolder object is attached as a tag on each view. This object adds
    //decoration for View objects in the list by using values from the data source, which in this
    //example is the Cursor. The ViewHolder is defined as a static inner class with two instance
    //variables, one for the index of the Important table column and one for the row_tab view you
    //defined in the layout.
    static class ViewHolder{
        //store the column index
        int colImp;
        //store the view
        View listTab;
    }
}

RemindersDbAdapter.java

package com.example.remindersapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class RemindersDbAdapter {

    //these are the column names
    public static final String COL_ID = "_id";
    public static final String COL_CONTENT =  "content" ;
    public static final String COL_IMPORTANT = "important";

    //these are the corresponding indices

    private static final int INDEX_ID = 0;
    private static final int INDEX_CONTENT = INDEX_ID + 1;
    private static final int INDEX_IMPORTANT = INDEX_ID + 2;

    //used for logging
    private static final String TAG = "RemindersDbAdapter";

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_NAME = "dba_reminder";
    private static final String TABLE_NAME = "table_reminder";
    private static final int DATABASE_VERSION = 3;

    private final Context mCtx;

    ////SQL statement used to create the database
    private static final String DATABSE_CREATE = "CREATE TABLE if not exists " + TABLE_NAME + " ( " +
            COL_ID + " INTEGER PRIMARY KEY autoincrement, " +
            COL_CONTENT + " TEXT, " +
            COL_IMPORTANT + "INTEGER );";

    public RemindersDbAdapter(Context Ctx) {
        //The constructor saves an instance of Context, which is passed to DatabaseHelper
        this.mCtx = Ctx;
    }

//The open()
//method initializes the helper and uses it to get an instance of the database,

    public void open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
    }

    //the close()
    //method uses the helper to close the database.
    public void close() throws SQLException {
        if (mDbHelper != null) {
            mDbHelper.close();
        }
    }
    //CREATE
//note that the id will be created for you automatically

    public void createReminder(String name, boolean important) {
        ContentValues values = new ContentValues();
        values.put(COL_CONTENT, name);
        values.put(COL_IMPORTANT, important ? 1 : 0);
        mDb.insert(TABLE_NAME, null, values);

    }
    ////overloaded to take a reminder

    public long createReminder(Reminder reminder) {
        ContentValues values = new ContentValues();
        values.put(COL_CONTENT, reminder.getmContent()); //Contact name
        values.put(COL_IMPORTANT, reminder.getImportant()); //Contact phone number

        //// Inserting Row
        return mDb.insert(TABLE_NAME, null, values);
    }
    //READ

    public Reminder fetchReminderById(int id) {
        Cursor cursor = mDb.query(TABLE_NAME,
                new String[]{COL_ID, COL_CONTENT, COL_IMPORTANT},
                COL_ID + " =? ",
                new String[]{String.valueOf(id)},
                null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        return new Reminder(
                cursor.getInt(INDEX_ID),
                cursor.getString(INDEX_CONTENT),
                cursor.getInt(INDEX_IMPORTANT));


    }

    public Cursor fetchAllReminders() {

        Cursor mcursor = mDb.query(TABLE_NAME, new String[]{COL_ID, COL_CONTENT, COL_IMPORTANT},
                null, null, null, null, null);
        if (mcursor != null) {
            mcursor.moveToFirst();
        }
        return mcursor;
    }

    //UPDATE
    public void updateReminder(Reminder reminder) {
        ContentValues values = new ContentValues();
        values.put(COL_CONTENT, reminder.getmContent());
        values.put(COL_IMPORTANT, reminder.getImportant());
        mDb.update(TABLE_NAME, values, COL_ID + " =? ", new String[]{String.valueOf(reminder.getmId())});
    }
    //DELETE
    public void deleteReminderById(int id){
        mDb.delete(TABLE_NAME,COL_ID + "=?", new String[]{String.valueOf(id)});
    }
    public void deleteAllReminders(){
        mDb.delete(TABLE_NAME,null,null);
    }

//sqlite open helper

    private static class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABSE_CREATE);
            db.execSQL(DATABSE_CREATE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + "to " + newVersion + ",which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);


        }

        @Override
        public void onOpen(SQLiteDatabase db) {
            super.onOpen(db);
            onCreate(db);
        }
    }

}

【问题讨论】:

  • "INTEGER );": COL_IMPORTANT + " INTEGER );" 前少了一个空格。
  • @forpas 谢谢!有用。我想再问一个问题。在这个应用程序代码中是简单的 3 个 Java 类,其中代码。但应用程序大小为 12mb。它只包含一个 Sqlite 示例。这是什么?

标签: java android database android-sqlite simplecursoradapter


【解决方案1】:
column ' content ' does not exist

删除COL_CONTENTcontent 周围的空格。

【讨论】:

  • 感谢您的回复。当我删除空格时,logcat 显示 'Caused by: android.database.sqlite.SQLiteException: no such column: important (code 1): , while compile: SELECT _id, content, important FROM table_reminder'
  • 还有一件事 logcat 显示 'E/SQLiteDatabase: Error inserting important=1 content=English Assignment on 30-04-2o21 android.database.sqlite.SQLiteException: table table_reminder 没有名为重要的列 (代码 1): , 编译时: INSERT INTO table_reminder(important,content) VALUES (?,?);'
  • 这些问题与您未包含在问题中的 sqlite open helper 实现有关。
  • 对不起!现在请检查 RemindersDbAdapter.java 类。内部类是 DatabaseHelper 类。非常感谢您付出宝贵的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多