我正在写你,因为我试图找出如何让这个查询在Android上运行,我知道sintaxis没问题,因为我之前尝试过SQLite浏览器,但我不知道为什么它没有'在Android上给我任何结果,这是我在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 = 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;
}
正如你所看到的,我曾尝试过几种方式,但没有一种方法可以正常工作,它不会返回任何结果。我已经使用DDMS将数据库文件拖到我的电脑上,并在SQLite浏览器上尝试相同的查询,这是有效的。那么,之前显示的代码问题在哪里?
感谢您对此问题的任何帮助,
蒂姆,这是: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);";
创建表的方法
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;
}
答案 0 :(得分:4)
文档说明返回的游标对象在第一次输入之前定位。我认为首先在这种情况下将永远返回false。 result.moveToFirst()? 检查返回的条目数。
答案 1 :(得分:0)
您是收到错误还是没有结果? 您确定数据在数据库中吗?
答案 2 :(得分:0)
您是否使用android_metadata表设置了sqlite数据库?请参阅that post,这可能有所帮助。对于我的应用程序,我正在使用该帖子中的数据库助手类,它就像一个魅力。