我不明白为什么我的笔记应用程序不断崩溃。我单击一个按钮以插入一个注释,该注释使用SQLite数据库显示在listView中。
给出的错误是:
(1) no such column: TITLE
FATAL EXCEPTION: main
Unable to resume activity: android.database.sqlite.SQLiteException: no such column: TITLE (code 1 SQLITE_ERROR): , while compiling: SELECT ID, TITLE FROM note_table
位于mainactivity.java
中的此处:
cursor = database.query(table_name, columns, where, where_args, group_by, having, order_by);
数据库:
public class DBOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "notes.db";
public static final String TABLE_NAME = "note_table";
public static final String ID_COLUMN = "ID";
public static final String TITLE_COLUMN = "TITLE";
public static final String TEXT_COLUMN = "ITEM2";
public static final String DATE_COLUMN = "DATE";
SQLiteDatabase db = this.getWritableDatabase();
public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
" TITLE TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table notes_table");
onCreate(db);
}
Mainactivity.java:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> notes_list;
private ArrayAdapter adapter;
private DBOpenHelper helper;
private SQLiteDatabase database;
private TextView noNotesView;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Edit_notes.class);
startActivity(intent);
}
});
noNotesView = findViewById(R.id.empty_notes);
listView = (ListView) findViewById(R.id.listView);
notes_list = new ArrayList<>();
helper = new DBOpenHelper(this);
database = helper.getWritableDatabase();
}
private void ViewData(){
String table_name = "note_table";
String[] columns = {"ID", "TITLE"};
String where = null;
String where_args[] = null;
String group_by = null;
String having = null;
String order_by = null;
cursor = database.query(table_name, columns, where, where_args, group_by, having, order_by); // the error is here
String total_text = "total number of rows: " + cursor.getCount() + "\n";
cursor.moveToLast();
notes_list = new ArrayList<>();
adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,notes_list);
for(int i = 0; i < cursor.getCount(); i++) {
// total_text += c.getInt(0) + " " + c.getString(1) + "\n";
notes_list.add(cursor.getString(1));
listView.setAdapter(adapter);
cursor.moveToPrevious();
}
// noNotesView.setText(total_text);
}
private void hide_or_show() {
if (cursor.getCount() == 0) {
noNotesView.setVisibility(View.VISIBLE);
} else {
noNotesView.setVisibility(View.GONE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.delete_all) {
Delete_all();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onResume()
{
super.onResume();
ViewData();
hide_or_show();
}
public void Delete_all() {
database.execSQL("delete from " + "note_table");
adapter.clear();
}
由于列名正确,我不明白为什么收到此错误消息。我尝试删除并重新编译我的应用程序,但没有成功。
答案 0 :(得分:2)
您的问题出在数据库类的构造函数上:
public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
特别是1
的最后一个参数中的super()
。那是您的数据库版本,是对数据库结构进行更改的关键。您在评论中提到TITLE
以前称为ITEM1
,但在您的设备上它仍称为ITEM1
,因为您的应用程序不知道数据库结构已更改。
您可以通过在类中引入版本常量来解决此问题,如下所示:
private static final int DB_VERSION = 2;
并在构造函数中使用此变量:
public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DB_VERSION);
}
无论何时对数据库进行任何结构更改,都应递增DB_VERSION
,这将导致SQLiteOpenHelper
调用其onUpgrade()
方法。