我正在开发一个应用程序(需要人的位置),它存储了针对consumerIds的位置坐标,我创建了三个字段,即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();
}
}
获取位置我没有问题。