Google지도에서 특정 카페의 위치를 보여주는 애플리케이션을 만들고 있습니다. 내 앱이 카페의 경도와 위도를 데이터베이스에서 가져 와서지도에서 마커를 설정하기를 원합니다.SQLite 데이터베이스에서 데이터를 가져 오는 데 도움이 필요합니다.
SQLite 사용법에 대한 많은 자습서를 읽었지만 데이터베이스를 열 수는 있지만 아무것도 검색하지 못했습니다. 커서를 사용하여 시도했지만 오류가 계속. 나는 이것이 지식의 부족 때문이라고 생각한다. 나는이 웹 사이트를 통해 서핑을했는데 나를 위해 유용한 것을 찾지 못했습니다. 이 튜토리얼 덕분에 지금까지이 기사를 얻었습니다. [http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428] [ 1]
이처럼 내 dbhelper 모양을 내 메인 클래스에서
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class Databasehelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.map.mapguide/databases/";
private static String DB_NAME = "mapDB";
private SQLiteDatabase myDataBase;
private final Context myContext;
public Databasehelper(Context context) {
super(context, DB_NAME, null, 1);
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");
}
}
}
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_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;
}
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 output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
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_READONLY);
}
@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
}
}
이 MapActivity를 확장합니다. 이것은 내지도에서 동작을 제어하는 것입니다. 데이터베이스에서 데이터를 검색하고 메인 클래스에서 위도와 경도를 어떻게 설정합니까?
정말 도움을 주셔서 감사합니다. 나는이 일을 2-3 일 동안 해왔지만 아직도 내가 뭘 잘못하고 있는지 알 수 없다. [1] : http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428
미리 감사드립니다.
바미
답장을 보내 주셔서 감사합니다. 전에이 튜토리얼을 끝냈고 DB에서 데이터를 가져올 수있었습니다. 하지만 지금은 SQLite 데이터베이스 브라우저로 데이터베이스를 만들었습니다. 이제 문제는 데이터를 검색하는 방법을 모른다는 것입니다. – Bami
나는 이해했다. ContentProvider 내에서 그 작업을 수행했습니다. 제 경우에는 공급자의 쿼리 메서드에서 SQLiteQueryBuilder를 사용합니다. – Renaud