Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android sqlite选择[]参数_Android_Database_Sqlite - Fatal编程技术网

Android sqlite选择[]参数

Android sqlite选择[]参数,android,database,sqlite,Android,Database,Sqlite,通过给出一个示例,寻找任何可以在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 fi

通过给出一个示例,寻找任何可以在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;
}

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


看起来您正试图查询两个同名的列?SQLite表只能有唯一的列名,因此不能有两个名为
\u id

的列创建表别名(“id”integer NOT NULL主键,“gender\u id”integer NOT NULL,“race\u id”integer NOT NULL,name text NOT NULL),这是表的sql结构,我想将where子句作为
字符串查询,其中=“性别_id=?”?种族id=?“;
String where = "_id=? AND _id=?";