android sqlite:如何在不同的数据库中选择不同的表数据

时间:2014-10-21 08:54:55

标签: java android database sqlite

我导入了两个数据库(一个是omstar0,其中有一个表omstar,另一个是omstar1,它有一个表omstarline),它们存储在android的sdcard的资产文件中。 android sqlite:如何在不同的数据库中选择差异表数据?我的意思是我想选择两个数据库中的所有数据。以下是我的代码:

 public class DBManager {

 private Context context;

public final String DB_PATH2 =    

android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/tianke";

private final String DB_NAME = "omstar0.db";
private SQLiteDatabase database;
private final int BUFFER_SIZE = 1024; 

DBManager(Context context) {
      this.context = context;   
}

public SQLiteDatabase openDatabase() {

    for(int i = 0; i<2; i++) {
    File dbFile = new File(DB_PATH2+"/omstar"+String.valueOf(i)+".db");


      if(!dbFile.exists()){
      //InputStream is = this.context.getResources().openRawResource(R.raw.);
     try {
     //创建目录  
     File pmsPaht = new File(DB_PATH2);
     Log.i("pmsPaht", "pmsPaht: "+pmsPaht.getPath());
     pmsPaht.mkdirs();
     AssetManager  am = this.context.getAssets();
     InputStream is = am.open("/omstar"+String.valueOf(i)+".db");
     FileOutputStream  fos = new FileOutputStream(dbFile);
      byte[] buffer = new byte[BUFFER_SIZE];
      int count = 0;
      while ((count = is.read(buffer)) > 0) {
          fos.write(buffer, 0, count);
        }
      fos.flush();
      fos.close();
      is.close();
      am.close();
      }catch (IOException e) {
          e.printStackTrace();
       }
      }
      database = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
    }
      System.out.println("2: sucesseful");

      return database;
    } 
    //http://jalhaskazakhlike.lofter.com/post/3f320a_15df9b3
    //http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html
  public void closeDatabase() {
       if (database!=null){
         this.database.close();
       }  
    }

}

公共类SDDatabaseActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Button create = (Button) findViewById(R.id.button1);
    Button query = (Button) findViewById(R.id.button2);
    create.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        createDB(); 
        }
    });
    query.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
         queryDB();

        }
    });
}
  public void createDB()
    {
    DBManager dbm = new DBManager(this);
    dbm.openDatabase(); 
    dbm.closeDatabase();
    }
  public void queryDB()
    {
    String sql = "select * from omstar";

    DBManager dbm = new DBManager(this);
    SQLiteDatabase db = dbm.openDatabase(); 
    Cursor cur =  db.rawQuery(sql, null);

    while (cur.moveToNext())
    {
    //String id = cur.getColumnName(1);
    //float la = cur.ge;
    //float id = cur.getColumnName(1);
    int id = cur.getInt(0);
    String name = cur.getString(1);
    String email = cur.getString(2);
    Log.i("latitude", "id:"+id);
    System.out.println("id:"+id);
    System.out.println("name:"+name);
    System.out.println("Email:"+email);
    }
    cur.close();
    db.close();
    } 

}

1 个答案:

答案 0 :(得分:0)

SELECT Columnname FROM Database1.Table,Database2.Table      哪里      Database1.Table.Column(主键)= Database2.Table.Column(主键);