1

通过给出示例寻找可以在 sqlite 查询生成器中使用 select args[] 的人。下面的代码无法正常工作。

public class DatabaseHelper extends SQLiteOpenHelper{

    public static String DB_PATH = "/data/data/yourpath/databases/";
    public static String DB_NAME ="your_sqlite";
    private static final int DATABASE_VERSION = 1;


    private final Context myContext;
    private SQLiteDatabase mydb;
    //private Cursor aliases;
    private Cursor m;

    public DatabaseHelper(Context ctx){ 
        super(ctx,DB_NAME,null,DATABASE_VERSION);
        this.myContext = ctx;

    }


    /**
     * creates an empty database and rewrites it with you own
     * */
    public void createDatabase() throws IOException{
        boolean dbExists = checkDatabase();

        if(dbExists){
            //do nothing 
        }else{
            // by calling this method an empty database will be created into default system path
            this.getReadableDatabase();

            try{
                copyDatabase();
            }
            catch(IOException e){
                throw new Error("Error copying database");
            }
        }

    }

/*
 * check if database exists to prevent re-copying the file each time you open the application
 * return true if exists otherwise false
 */
 private boolean checkDatabase(){
     SQLiteDatabase checkDB = null;

     try{
         String myPath = DB_PATH + DB_NAME;
         checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);

     }catch(SQLiteException e){

     }

     if (checkDB != null){
         checkDB.close();
     }
     return checkDB != null ? true : false;
 }

 /**
  * copies your database  from local assets folder to just created empty database
  * in the system folder from where it can be accessed and handled
  * */
 private void copyDatabase() throws IOException{
     //open your local db as the input stream
     InputStream myInput = myContext.getAssets().open(DB_NAME);

     //path to the just created empty db
     String outFileName = DB_PATH + DB_NAME;

     //open the empty db  as the outputStream
     OutputStream myOutput = new FileOutputStream(outFileName);

     //transfers bytes from the input file to output file
     byte[] buffer = new byte[1024];

     int length;

     while((length = myInput.read(buffer))>0){
         myOutput.write(buffer, 0, length);
     }
     //close the streams
     myOutput.flush();
     myOutput.close();
     myInput.close(); 
 }
 public void openDatabase() throws SQLException{
     //open the db
     String myPath = DB_PATH + DB_NAME;
     mydb = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
 }
 public synchronized  void close(){
     if(mydb != null){
         mydb.close();
     }
     super.close();
 }




    @Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
}


    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }



public Cursor getAllCharacters(){

    return mydb.query("char",new String[]{"_id","gender_id","race_id","name"},
            null, null, null,null,null);
}
public void setCharacter(long id){
     m  = mydb.query("char",new String[]{"gender_id","race_id"},"_id =" + id,null,null,null,null,null);
     if (m == null){
         System.out.print("Cursor is empty");
     }
}


public Cursor getAliases(){
    String gender_id = m.getString(0);
    String race_id   = m.getString(1);
    String where = "_id=? AND _id=?";
    String[] args ={gender_id,race_id};

    Cursor alias = mydb.query("alias",new String[]{"_id","gender_id","race_id","alias"},
            where,args,null,null,null);
    return alias;
}

}

我正在尝试从 getAliases() 获取光标

4

1 回答 1

2
String where = "_id=? AND _id=?";

看起来您正在尝试查询具有相同名称的两列?SQLite 表只能有唯一的列名,所以不能有两个名为_id.

于 2012-07-09T13:56:14.993 回答