无法插入 SQLite 数据库
Not able to INSERT to SQLite database
我正在获取位置的经纬度信息并将它们添加到 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() 方法中,插入失败并被捕获。我的问题是什么?提前致谢。
您的 CREATE TABLE
查询中 lat
和 lon
字段的类型被指定为 DOUBLE
,而在您的 INSERT
查询中您正试图插入 String
(_lat
& _lon
) 值。
我相信,您正在尝试插入 Bundle
中的值,即 _lat
和 _lon
的值。
因此,要解决您的错误,请按以下方式重写您的 INSERT
查询。
database.execSQL("INSERT INTO knm (lat,lon) VALUES (" + _lat + "," + _lon + ")");
您想在 table 中插入变量 _lat
和 _lon
的值,但您的 sql 语句却使用了它们的名称。
这样,SQLite 将名称视为当然不存在的列名,这会引发异常。
推荐的插入行的方法是使用方法 insert()
和 ContentValues
:
ContentValues cv = new ContentValues();
cv.put("lat", _lat);
cv.put("lon", _lon);
int rowid = database.insert("knm", null, cv);
您可以查看 rowid
的值。
如果是 -1
则插入失败。
任何其他值是插入行的 rowid
。
我正在获取位置的经纬度信息并将它们添加到 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() 方法中,插入失败并被捕获。我的问题是什么?提前致谢。
您的 CREATE TABLE
查询中 lat
和 lon
字段的类型被指定为 DOUBLE
,而在您的 INSERT
查询中您正试图插入 String
(_lat
& _lon
) 值。
我相信,您正在尝试插入 Bundle
中的值,即 _lat
和 _lon
的值。
因此,要解决您的错误,请按以下方式重写您的 INSERT
查询。
database.execSQL("INSERT INTO knm (lat,lon) VALUES (" + _lat + "," + _lon + ")");
您想在 table 中插入变量 _lat
和 _lon
的值,但您的 sql 语句却使用了它们的名称。
这样,SQLite 将名称视为当然不存在的列名,这会引发异常。
推荐的插入行的方法是使用方法 insert()
和 ContentValues
:
ContentValues cv = new ContentValues();
cv.put("lat", _lat);
cv.put("lon", _lon);
int rowid = database.insert("knm", null, cv);
您可以查看 rowid
的值。
如果是 -1
则插入失败。
任何其他值是插入行的 rowid
。