尝试在我的Android应用程序中创建一个sqlite数据库。表创建和值插入工作完美,但是当我尝试从db中获取(选择)值时,使用
android.database.sqlite.SQLiteException: no such column: rowId (code 1): , while compiling: SELECT rowId, firstName, lastName, photoPrefix;, photoSuffix;, hasPhoto FROM friends WHERE rowId = ?
这是我的SQLiteOpenHelper继承类
的属性private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "GroupDB";
private static final String TABLE_GROUPS = "groups";
private static final String KEY_ID = "rowId";
private static final String KEY_NAME = "name";
private static final String KEY_FRIENDS = "friends";
private static Context context;
private static final String[] COLUMNSGroup = { KEY_ID, KEY_NAME,
KEY_FRIENDS };
private static final String TABLE_FRIENDS = "friends";
private static final String KEY_FIRST_NAME = "firstName";
private static final String KEY_LAST_NAME = "lastName";
private static final String KEY_PHOTO_PREFIX = "photoPrefix;";
private static final String KEY_PHOTO_SUFFIX = "photoSuffix;";
private static final String KEY_HAS_PHOTO = "hasPhoto";
private static final String[] COLUMNSFriends = { KEY_ID, KEY_FIRST_NAME,
KEY_LAST_NAME, KEY_PHOTO_PREFIX, KEY_PHOTO_SUFFIX, KEY_HAS_PHOTO };
onCreate方法:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_GROUP_TABLE = "CREATE TABLE groups ( "
+ "rowId INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT, "
+ "friends TEXT )";
db.execSQL(CREATE_GROUP_TABLE);
String CREATE_FRIEND_TABLE = "CREATE TABLE friends ( " + "rowId TEXT, "
+ "firstName TEXT, " + "lastName TEXT, " + "photoPrefix TEXT, "
+ "photoSuffix TEXT, " + "hasPhoto INTEGER )";
db.execSQL(CREATE_FRIEND_TABLE);
Log.d("DB", CREATE_GROUP_TABLE);
Log.d("DB", CREATE_FRIEND_TABLE);
}
addFriend方法也正常工作:
public void addFriend(Friends friend) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, friend.id);
values.put(KEY_FIRST_NAME, friend.firstName);
values.put(KEY_LAST_NAME, friend.LastName);
values.put(KEY_PHOTO_PREFIX, friend.photoPrefix);
values.put(KEY_PHOTO_SUFFIX, friend.photoSuffix);
values.put(KEY_HAS_PHOTO, friend.hasPhoto ? 1 : 0);
db.insert(TABLE_FRIENDS, null,values);
}
db.close();
}
getFriend方法给出了错误
public Friends getFriend(String id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_FRIENDS, //THIS
COLUMNSFriends, //LINE
" rowId = ?", //GIVES
new String[] { id }, null, null, null, null); //EXCEPTION
if (cursor != null)
cursor.moveToFirst();
Friends friend = new Friends();
friend.id = cursor.getString(0);
friend.firstName = cursor.getString(1);
friend.LastName = cursor.getString(2);
friend.photoPrefix = cursor.getString(3);
friend.photoSuffix = cursor.getString(4);
friend.hasPhoto = cursor.getString(5).equals("1") ? true : false;
return friend;
}
答案 0 :(得分:0)
我不知道Android SQLite API - 但是在其他sqlite APIS上你需要明确地指出你的数据库更改 - 所以在插入不执行后执行关闭,它可能会回滚。
答案 1 :(得分:0)
我错过了这部分,改变了“photoSuffix;”;到“photoSuffix”;和“photoPrefix;”;解决这个问题。谢谢Raghunandan!
答案 2 :(得分:0)
使用此查询:
Cursor cursor = db.query(TABLE_NAME,new String[]{ID,FIRST_NAME,LAST_NAME,EMAIL_ID,PASSWORD,PH_NUMB,POST_ADDRESS},null,null,null,null,FIRST_NAME);