2

我正在开发一个应用程序(需要人的位置),它根据消费者 ID 存储位置坐标,我创建了三个字段,即 1)key_Con_ID、2)key_Latitude、3)key_Longitude。

插入代码位于“try”和“catch”中。成功插入后,'try' 部分返回 true,而 'catch' 部分返回 false。

我面临的问题是,在调试时,“try”和“catch”两个部分的代码都被执行了。而且当我查看表格时(在 SD 卡上创建了数据库备份),只有一些消费者的重复条目。 我的问题是,如果插入查询成功,为什么'catch'部分中的代码正在执行?

public class sqldataclass{
public static final String key_Con_ID="Con_ID";
public static final String key_Latitude="Latitude";
public static final String key_Longitude = "Longitude";

private static final String DATABASE_TABLE11 = "Gps_Data";
private static final int DATABASE_VERSION = 1;
private static final String DEBUG_TAG = null;

private DbHelper OurHelper;
private final Context OurContext;
private SQLiteDatabase OurDatabase;
private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_GPS_DATA = "CREATE TABLE " + DATABASE_TABLE11 + " ( "
                + key_Con_ID + " varchar ,"
                + key_Latitude + " double ,"
                + key_Longitude + " double )";
        try
          {db.execSQL(CREATE_GPS_DATA);}
          catch(SQLException e){e.printStackTrace();}
}
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXIST" + DATABASE_NAME);
        onCreate(db);
    }
    public sqldataclass(Context c) {
    OurContext = c;
}

public sqldataclass open() throws SQLException

{
    OurHelper = new DbHelper(OurContext);
    OurDatabase = OurHelper.getWritableDatabase();
    return this;
}
    public void close() {
    OurHelper.close();
}
public boolean insertGpsCords(String consid,double lat,double longi)
{
    open();
    try {
        ContentValues values = new ContentValues();

        values.put(sqldataclass.key_Con_ID, consid);
        values.put(sqldataclass.key_Latitude, lat);
        values.put(sqldataclass.key_Longitude, longi);

        String query1 = "Select " + key_Con_ID + " from " + DATABASE_TABLE11 + " where " + key_Con_ID + " = '" + consid + "'";

        Cursor c1 = OurDatabase.rawQuery(query1, null);
        int getRecordCount = c1.getCount();
        if (getRecordCount == 0) {
            OurDatabase.insert(DATABASE_TABLE11, null, values);
        } else {
                OurDatabase.update(DATABASE_TABLE11, values, null, null);
        }
        close();
        return true;
    }
    catch (Exception e) {
        return false;
    }
}

insertGpsCords 函数在单击活动中的按钮时被调用。位置是通过服务获得的。部分代码在这里: -

b1= (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (s!=null){

                ArrayList<Double> loc = new ArrayList<Double>();

                if (s.getCoordinates().size()!=0) {
                    loc.clear();
                    loc.addAll(s.getCoordinates());

                    Double l = loc.get(0);
                    Double g = loc.get(1);
                    Toast.makeText
                            (Bill_meter_status_faulty.this, "latitude: " + l + "longitude: " + g, Toast.LENGTH_SHORT)
                            .show();
                    sqldataclass sdc = new sqldataclass(Bill_meter_status_faulty.this);
                    sdc.insertGpsCords(consId, l, g);
                }
                else{
                    Toast.makeText(Bill_meter_status_faulty.this,"cannot access location",Toast.LENGTH_SHORT).show();
                }

            }

获取位置没有问题。

4

0 回答 0