Java Android中来自创建方法的数据库错误
我得到这个logcat错误: 原因:android.database.sqlite.SQLiteException:无此类列:id代码1:,编译时:从id=0的notes中选择* 这是我的代码:Java Android中来自创建方法的数据库错误,java,android,Java,Android,我得到这个logcat错误: 原因:android.database.sqlite.SQLiteException:无此类列:id代码1:,编译时:从id=0的notes中选择* 这是我的代码: public static final String dbName="notesDb"; public static final Integer DB_version=1; public static final String key_id="id"; public static final Stri
public static final String dbName="notesDb";
public static final Integer DB_version=1;
public static final String key_id="id";
public static final String key_title="title";
public static final String key_subject="subject";
public static final String Table_notes="notes";
公共DbNotesContext上下文{
超级上下文,dbName,null,DB_版本
}
@Override
public void onCreate(SQLiteDatabase db) {
String create_table="create table "+Table_notes+"("+key_id+"Integer primary key,"+key_title+" varchar(30), "+key_subject+" varchar(50)) ";
db.execSQL(create_table);
}
public Note getNoteByID(int id){
Note note=null;
SQLiteDatabase db=this.getReadableDatabase();
String selectQuery="SELECT * FROM "+Table_notes+" WHERE "+key_id+"="+id;
Cursor cursor=db.rawQuery(selectQuery,null);
if (cursor.moveToFirst()){
String title=cursor.getString(cursor.getColumnIndex(key_title));
String subject=cursor.getString(cursor.getColumnIndex(key_subject));
int id_item=cursor.getInt(cursor.getColumnIndex(key_id));
note=new Note(title,subject,id_item);
}
cursor.close();
return note;
}
如何解决此问题?错误在您的创建查询中,您需要在列名后提供适当的空格
create table+table\U notes++key\U id+Integer主键,//您缺少Integer前面的空格
因此,sqlite将创建名为IDiteger而非id的列,您可以尝试使用select查询,将id列引用为IDiteger。错误的哪一部分让您感到困惑?Table notes没有名为id.public void的列onCreateSQLiteDatabase db{String create_table=create table+table_notes++key_id+Integer主键,+key_title+varchar30,+key_subject+varchar50;db.execSQLcreate_table;}create方法中有错误吗?正如我所说,表中没有名为id的列。由于id和Integer之间缺少空格,因此有一个名为IDITEGER的列,但没有声明类型。投票关闭,因为问题是由一个简单的印刷错误引起的。在添加空格后,我得到了相同的错误,没有这样的列:id代码1:,而compiling:从id=0的注释中选择*