如何解决列“内容”不存在
我正在为 sqlite 使用 sqliteOpenHelper 类。我正在学习一本有关概念的 android 书。一切都与书中的代码相同。但在我的情况下,应用程序崩溃并显示错误。请帮助我解决此问题。 在 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,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,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);
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,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,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);
}
//sqlite open helper
private static class DatabaseHelper extends sqliteOpenHelper {
public DatabaseHelper(Context context) {
super(context,DATABASE_NAME,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);
}
}
}
解决方法
column ' content ' does not exist
删除 content
中 COL_CONTENT
周围的空格。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。