I'm writting you because I having tried to find out how to get this query works on Android, I know that the sintaxis is OK, because i have tried on SQLite Browser before but I don't know why it doesn't give me any result on Android, this is the code that I'm using on Android:
public long getAdminOrMemberOrGroupID (String arg, char option){
Cursor result = null;
String[] selectionArgs = new String[] { arg };
long idResult = 0;
int idIndex=0;
switch(option){
case 'a':
result = db.rawQuery("SELECT _id FROM Admins WHERE mac=?", selectionArgs);
//result = db.rawQuery("SELECT _id FROM Admins WHERE mac= '"+ arg +"'", null);
//result = db.query(DATABASE_TABLE_ADMINS, new String[]{KEY_ID}, KEY_MAC + "= '" + arg + "'" , null, null, null, null);
case 'm':
result = 开发者_C百科db.rawQuery("SELECT _id FROM Members WHERE mac=?", selectionArgs);
//result = db.rawQuery("SELECT _id FROM Members WHERE mac= '"+ arg +"'", null);
//result = db.query(DATABASE_TABLE_MEMBERS, new String[]{KEY_ID}, KEY_MAC + "= '" + arg + "'", null, null, null, null);
case 'g':
result = db.rawQuery("SELECT _id FROM Groups WHERE name=?", selectionArgs);
////result = db.rawQuery("SELECT _id FROM Groups WHERE name= '"+ arg +"'", null);
//result = db.query(DATABASE_TABLE_GROUPS, new String[]{KEY_ID}, KEY_NAME + "= '" + arg + "'", null, null, null, null);
}
idIndex = result.getColumnIndex(KEY_ID);
if(result!=null){
if (result.isFirst()){
idResult= result.getLong(idIndex);
}
}
return idResult;
}
As you have seen, I have tried to do it in several ways but none of them works fine, it doesn't return any result. I have pulled the database file to my pc using DDMS and trying the same query on SQLite Browser and this works. So, where is the problem on the code shown before?
Thanks for any help on this issue,
Tim, here it is:
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final int NAME_COLUMN = 1;
public static final String KEY_MAC = "mac";
public static final int MAC_COLUMN = 2;
public static final String KEY_GPS = "gps";
public static final int GPS_COLUMN = 3;
public static final String KEY_ID_ADMIN = "idAdmin";
public static final String KEY_ID_GROUP = "idGroup";
private static final String DATABASE_CREATE_ADMINS = "create table " + DATABASE_TABLE_ADMINS +
"(" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME + " text not null, " + KEY_MAC + " text not null);";
private static final String DATABASE_CREATE_GROUPS = "create table " + DATABASE_TABLE_GROUPS +
"(" + KEY_ID + " integer primary key autoincrement, " + KEY_ID_ADMIN + " integer, " + KEY_NAME + " text not null);";
private static final String DATABASE_CREATE_MEMBERS = "create table " + DATABASE_TABLE_MEMBERS +
"(" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME + " text not null, " + KEY_MAC + " text not null, "
+ KEY_GPS + " text, " + KEY_ID_GROUP + " integer);";
Method that creates the tables
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
db = context.openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL(DATABASE_CREATE_ADMINS);
db.execSQL(DATABASE_CREATE_MEMBERS);
db.execSQL(DATABASE_CREATE_GROUPS);
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
Documentation says that returned cursor object is positioned before first entry. I think that isFirst in this case will always return false. result.moveToFirst()? Check returned entries count.
Do you get an error or just no results? Are you sure the data is in the database?
Have you setup your sqlite database with android_metadata table? See that post, it might help. For my app I'm using the database helper class from that post and it's working like a charm.
精彩评论