我正在获取一个位置的纬度和经度信息并将它们添加到 SQLite。看起来我创建数据库没有问题,但我的插入不起作用。这是我的代码:
private GoogleMap mMap;
Button button;
double _lat,_lon;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
intent = getIntent();
_lat = intent.getDoubleExtra("yenie",0);
_lon = intent.getDoubleExtra("yenib",0);
button = findViewById(R.id.kaydet);
SQLiteDatabase database = this.openOrCreateDatabase("Konumlar",MODE_PRIVATE,null);
database.execSQL("CREATE TABLE IF NOT EXISTS knm (lat DOUBLE,lon DOUBLE)");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
database.execSQL("INSERT INTO knm (lat,lon) VALUES (_lat,_lon)");
Toast.makeText(getApplicationContext(),"sa",Toast.LENGTH_LONG);
}
catch (Exception e){
}
}
});
}
我通过调试了解到,在 onClick() 方法中,插入失败并被捕获。我的问题可能是什么?提前致谢。
答案 0 :(得分:2)
您想在表中插入变量 RUN mkdir -p /android_tools \
&& curl https://storage.googleapis.com/git-repo-downloads/repo > /android_tools/repo \
&& chmod a+x /android_tools/repo
和 _lat
的值,但是您的 sql 语句使用了它们的名称。
通过这种方式,SQLite 将名称视为列名,而列名当然不存在,这会引发异常。
推荐的插入行的方法是使用 _lon
和 insert()
方法:
ContentValues
您可以查看 ContentValues cv = new ContentValues();
cv.put("lat", _lat);
cv.put("lon", _lon);
int rowid = database.insert("knm", null, cv);
的值。
如果是 rowid
,则插入失败。
任何其他值都是插入行的 -1
。
答案 1 :(得分:1)
lat
查询中的 lon
和 CREATE TABLE
字段类型指定为 DOUBLE
,而在 INSERT
查询中,您尝试插入 { {1}}(String
& _lat
) 值。
我相信,您正在尝试插入 _lon
中的值,即 Bundle
和 _lat
的值。
因此,要解决您的错误,请按如下方式重写您的 _lon
查询。
INSERT