没有这样的列:AA(代码 1):,编译时:从注释中选择注释,其中注释 = aa



我尽量防止重复 SQLite 中的条目, 所以我使用这段代码,但它给出了错误 没有这样的列:AA(代码 1( 我的 DatabseHelper 类是:

数据库助手.java

public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
private static final int DATABASE_VERSION = 7;
private static final String DATABASE_NAME = "db_notes";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Note.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
onCreate(db);
}
public String insertNote(String note, String address) {
SQLiteDatabase db  = this.getWritableDatabase();
ContentValues values = new ContentValues();
Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " + 
Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + " = " + note ,null,null);
if (resultSet.getCount() == 0)
{
values.put(Note.COLUMN_NOTE, note);
values.put(Note.COLUMN_ADDRESS, address);
db.insert(Note.TABLE_NAME, null, values);
}
else Toast.makeText(context,note+" already 
exists",Toast.LENGTH_SHORT).show();
return note;
}
public Note getNote(String id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(Note.TABLE_NAME,
new String[]{Note.COLUMN_ID, 
Note.COLUMN_NOTE,Note.COLUMN_ADDRESS, Note.COLUMN_TIMESTAMP},
Note.COLUMN_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Note note = new Note(
cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)),
cursor.getString(cursor.getColumnIndex(Note.COLUMN_ADDRESS)),
cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
cursor.close();
return note;
}
public List<Note> getAllNotes() {
List<Note> notes = new ArrayList<>();
String selectQuery = "SELECT  * FROM " + Note.TABLE_NAME + " ORDER BY " +
Note.COLUMN_TIMESTAMP + " DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
note.setAddress(cursor.getString
(cursor.getColumnIndex(Note.COLUMN_ADDRESS)));
note.setTimestamp(cursor.getString
(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
notes.add(note);
} while (cursor.moveToNext());
}
db.close();
return notes;
}
}

我收到此错误

android.database.sqlite.SQLiteException: no 这样的列: aa(代码 1(: , 编译时: 从笔记中选择注释,其中注释 = aa

我在这一行收到错误:

Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " +   Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + " = " + note ,null,null);

值需要放在双引号内。

将查询更改为此查询

Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " + 
Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + " = "" + note + """ ,null,null);

指示SQL 解析或执行出错的SQLite exception

您应该添加SINGLE QUOTE

在此处添加单引号 ->"='" + note + "'"

" SELECT " + Note.COLUMN_NOTE + " FROM " +   Note.TABLE_NAME  + "  WHERE
" + Note.COLUMN_NOTE + "='" + note + "'"

最后

Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " +   Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + "='" + note + "'",null,null); 

然后Clean-Rebuild-Run.

最新更新