Need help getting data out of SQLite database
I'm creating an application that shows the location of certain cafes on google maps. I want my app to get the longitude and latitude of the cafes out of a database and use them to set markers on my map.
I've gone through so many tutorials on how to use SQLite and I've only managed to open my database but not to retrieve anything. I've tried using cursor and such but keep getting errors. I believe this is because of the lack of knowledge. I've surfed through this website and couldn't find anything useful for me. I got this far thanks to this tutorial: [http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428][1]
This how my dbhelper looks like:
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 fin开发者_Python百科al 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
}
}
In my main class extends MapActivity. This is were i control the actions on my map. How do i retrieve data from my database and set the longitude and latitude in my main class.
I really appreciate the help. I've been on this for about 2-3 days and still can't figure out what I am doing wrong. [1]: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428
Thanks a lot in advance!
Bami
This devx tutorial may help you. Proceed step by step to understand how it works using Log without restriction. Both reigndesign and devx tutorials helped me a lot and now I have an externally initialized multi-tables database wrapped in a ContentProvider and it works like a charm.
First try to get all your data and use the cursor with a simple ListActivity. If this does not work and as long as you don't really understand how, it's pointless to add more complexity.
My two cents.
−−− Update:
Adapted from my ContentProvider class, try
CustomHelper dbHelper = new CustomHelper(context);
try {
dbHelper.createDatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myDB = dbHelper.getWritableDatabase();
try {
dbHelper.openDatabase();
}catch(SQLException sqle){
throw sqle;
}
then query on myDB
Cursor c = sqlBuilder.query(
ofopsDB,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
You could then control the result on logging c.getCount()
and test the content of the first element with c.moveToFirst()
.
精彩评论