android sqlite no such column,Android studio no such column SQL database

I do not understand why my note taking application keeps crashing. I click on a button to insert a note that shows in a listView using a SQLite database.

The error given is:

(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

located here in mainactivity.java:

cursor = database.query(table_name, columns, where, where_args, group_by, having, order_by);

Database:

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 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();

}

I don't understand why I get this error message since my column name is right. I have tried deleting and recompiling my application without success.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值