开发者

trying to get coords from database but nothing happens

as you can see from the code below im just trying to get the coords pushed out to the logcat. however i never see the message or the coords. it shows me the sql string then finishes. so i believe the try is failing but even the catch statement doesnt return anything.

Button ButtonMap = (Button) findViewById(R.id.ButMap);
   ButtonMap.setOnClickListener(new View.OnClickListener() {

          publ开发者_如何学Pythonic void onClick(View v) {
           try {
         /* Query for some results with Selection and Projection. */
            String sql = "SELECT _id,Longitude,Latitude FROM " + MY_DATABASE_TABLE + " WHERE _id = "+ID;
          Cursor c = db.rawQuery(sql,null);
           //db.query(MY_DATABASE_TABLE, new String[] {"_id", "Longitude", "Latitude"}, 
                      // "_id = " + "'%"+ID+"%'", null, null, null, null);
                  Log.e("SQL Select", sql);


         /* Get the indices of the Columns we will need */
         int longitude, latitude ;
            longitude = c.getColumnIndex("Longitude");
            latitude = c.getColumnIndex("Latitude");

         /* Check if our result was valid. */
          if (c.moveToFirst()) {
           int i = 0;
           /* Loop through all Results */ 
           do {
            i++;
            /* Retrieve the values of the Entry
             * the Cursor is pointing to. */
             double lat = c.getDouble(latitude);
              double lon = c.getDouble(longitude);            
                         Log.e("GPS", "location for this record is: lat="+lat+", lon="+lon);

           } while (c.moveToNext());
          }

        } catch (Throwable t) {
                  Log.e("GPS", t.toString());
       }
           finally {
         if (db != null)
          db.close();
        }

            Intent intent = new Intent();
                 setResult(RESULT_OK, intent);
                 finish();

       }

      });
 }


been to long to know exactly what fixed it but here is the answer code:

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class SqlLiteFishLoggerDao extends SQLiteOpenHelper implements
        FishLoggerDao {

    private static final String DB_NAME = "fishingLog";

    private static final String TABLE_NAME = "LogEntries";

    private static final String DELETE_LOG_ENTRY_SQL = "DELETE FROM LogEntries WHERE _id = ?;";

    private static final String FIND_LOG_ENTRY_SQL = "SELECT * FROM LogEntries WHERE _id = ?";

    private static final String FIND_ALL_ENTRIES_SQL = "SELECT * FROM LogEntries";

    private static final String[] NO_ARGS = {};

    private Context context;

    private SQLiteDatabase DB;

    public SqlLiteFishLoggerDao(Context context) {
        super(context, DB_NAME, null, 1);
        this.context = context;
    }

    @Override
    public void deleteLogEntry(String id) {
        DB = getWritableDatabase();
        DB.execSQL(DELETE_LOG_ENTRY_SQL, new Object[] { id });
        DB.close();
    }

    @Override
    public LogEntry findEntry(String id) {
        DB = getReadableDatabase();
        Cursor cursor = DB.rawQuery(FIND_LOG_ENTRY_SQL,
                new String[] { id });
        if (!cursor.moveToFirst()) {
            return null;
        }
        LogEntry entry = new LogEntry();
        entry.setId(cursor.getString(cursor.getColumnIndex("_id")));
        entry.setEntryDate(cursor.getString(cursor.getColumnIndex("CreateDate")));
        entry.setSpecies(cursor.getString(cursor.getColumnIndex("Species")));
        entry.setSizeOrWeight(cursor.getString(cursor.getColumnIndex("SizeOrWeight")));
        entry.setLatitude(cursor.getDouble(cursor.getColumnIndex("Latitude")));
        entry.setLongitude(cursor.getDouble(cursor.getColumnIndex("Longitude")));
        entry.setPictureUrl(cursor.getString(cursor.getColumnIndex("PictureURL")));

        cursor.close();
        DB.close();
        return entry;

    }

    @Override
    public void insertLogEntry(LogEntry entry) {
        DB = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("Latitude", entry.getLatitude());
        values.put("Longitude", entry.getLongitude());
        values.put("PictureURL", entry.getPictureUrl());
        values.put("SizeOrWeight", entry.getSizeOrWeight());
        values.put("CreateDate", entry.getEntryDate());
        values.put("Species", entry.getSpecies());
        DB.insertOrThrow("LogEntries", null, values);
        DB.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String s;
        try {
            InputStream in = context.getResources().openRawResource(R.raw.sql);
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(in, null);
            NodeList statements = doc.getElementsByTagName("statement");
            for (int i = 0; i < statements.getLength(); i++) {
                s = statements.item(i).getChildNodes().item(0).getNodeValue();
                db.execSQL(s);
            }
            Log.e("DB", "DB Created Successfully");
        } catch (Throwable t) {
            Log.e("DB error: ",t.toString());
        }
    }

    @Override
    public List<LogEntry> findAllEntries() {
        DB = getReadableDatabase();

        List<LogEntry> entries = new ArrayList<LogEntry>();

        Cursor cursor = DB.rawQuery(FIND_ALL_ENTRIES_SQL,
                NO_ARGS);

        if (cursor.moveToFirst()) {
            do {
                LogEntry entry = new LogEntry();
                entry.setId(cursor.getString(cursor.getColumnIndex("_id")));
                entry.setEntryDate(cursor.getString(cursor.getColumnIndex("CreateDate")));
                entry.setSpecies(cursor.getString(cursor.getColumnIndex("Species")));
                entry.setSizeOrWeight(cursor.getString(cursor.getColumnIndex("SizeOrWeight")));
                entry.setLatitude(cursor.getDouble(cursor.getColumnIndex("Latitude")));
                entry.setLongitude(cursor.getDouble(cursor.getColumnIndex("Longitude")));
                entry.setPictureUrl(cursor.getString(cursor.getColumnIndex("PictureURL")));

                if (entry.getSpecies() == null) {
                    entry.setSpecies("Not Entered");
                }

                if (entry.getSizeOrWeight() == null) {
                    entry.setSizeOrWeight("Not entered");
                }

                entries.add(entry);
            } while (cursor.moveToNext());
        }
        cursor.close();
        DB.close();
        return entries;
    }

    @Override
    public void onUpgrade(SQLiteDatabase DB, int oldVersion, int newVersion) {
        DB = getWritableDatabase();
        DB.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(getWritableDatabase());
        DB.close();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜