通过数据库sqlite android更改纬度和经度值

时间:2013-07-12 14:41:31

标签: android sqlite coordinates

我有一个带有tre列的sqlite数据库: id,纬度,经度。 纬度和经度是双倍的,像43.98722。 当我在我的Android设备中复制这个数据库时,经度和纬度的值变为4.398722e + 07 所以这可能不适用于我的应用程序。 我怎么解决这个问题? 也许是我的dbhelper通过InputStream和OutputStream复制extern数据库导致这个错误?

2 个答案:

答案 0 :(得分:0)

public class MyOpenHelper extends SQLiteOpenHelper{


private static String DB_PATH = "/data/data/pack.pullman/databases/";

private static String DB_NAME = "orari";

private SQLiteDatabase myDataBase; 

private final Context myContext;


public MyOpenHelper(Context context) {
    super(context, DB_NAME, null, 1);
    if(android.os.Build.VERSION.SDK_INT >= 4.2){
        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";         
    } else {
       DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }
    this.myContext = context; 
}   

public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){

    }else{

        this.getReadableDatabase();

        try {

            copyDataBase();


        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}


private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

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

    }catch(SQLiteException e){


    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}


private void copyDataBase() throws IOException{

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    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 database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);


}

@Override
public synchronized void close() {

        if(myDataBase != null)
            myDataBase.close();

        super.close();

}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

    }

答案 1 :(得分:0)

    public List<Coordinate> vediCoordinate(){
    SQLiteDatabase db=oh.getReadableDatabase();
    String c[]=new String[]{"lat","long","via"};
    cu=db.query("linee", c, null, null, null, null, null);
    int rows=cu.getCount();
    LinkedList<Coordinate> risultato=new LinkedList<Coordinate>();
    cu.moveToFirst();
    for(int i=0;i<rows;i++){
        risultato.add(i,new Coordinate(cu.getDouble(cu.getColumnIndex("lat")),cu.getDouble(cu.getColumnIndex("long")),cu.getString(cu.getColumnIndex("via"))));
        cu.moveToNext();
    }
    cu.close();
    return risultato;   
}