I have an app to store location and its latitude and logitude values with sqlite. There are two tables called locations and latlong. I need to add some locations to the first table and need to add its latitude and longitude values to the second table. I have created database and location adding codes. Now I am getting error while adding location to the location database. My database class is given below
public class SQLiteDatabaseData extends SQLiteOpenHelper
{
public static final String DATABASE_NAME ="locationdata.db";
public static final String TABLE_LOC = "locations";
public static final String COLUMN_ID ="id";
public static final String COLUMN_PLACE="place";
public static final String COLUMN_LOCATION="location";
public static final String TABLE_LATLNG="latlong";
public static final String COLUMN_LATLNG_ID=COLUMN_ID;
public static final String COLUMN_LATITUDE="latitude";
public static final String COLUMN_LONGITUDE="longitude";
public static final String COLUMN_UID="loc_id";
private static final int DATABASE_VERSION = 1;
private static final String SQL_CREATE_TABLE_LOCATIONS = "CREATE TABLE IF NOT EXISTS " +TABLE_LOC + "("
+COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+COLUMN_PLACE + " TEXT NOT NULL, "
+COLUMN_LOCATION+ " TEXT NOT NULL" +");";
private static final String SQL_CREATE_TABLE_LATLNG= "CREATE TABLE IF NOT EXISTS " +TABLE_LATLNG + "("
+COLUMN_LATLNG_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+COLUMN_LATITUDE + " TEXT NOT NULL, "
+COLUMN_LONGITUDE + " TEXT NOT NULL, "
+COLUMN_UID + " INTEGER NOT NULL "
+");";
public SQLiteDatabaseData(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE_TABLE_LOCATIONS);
db.execSQL(SQL_CREATE_TABLE_LATLNG);
onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " +TABLE_LOC);
db.execSQL("DROP TABLE IF EXISTS "+TABLE_LATLNG);
onCreate(db);
}
public SQLiteDatabaseData(Context context, String name, CursorFactory factory,int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
}
This is my locationdata class
public class LocationDAO {
public static final String TAG = "LocationDAO";
// Database fields
private SQLiteDatabase mDatabase;
private SQLiteDatabaseData mDbHelper;
private Context mContext;
private String[] mAllColumns = { SQLiteDatabaseData.COLUMN_ID,
SQLiteDatabaseData.COLUMN_PLACE, SQLiteDatabaseData.COLUMN_LOCATION};
public LocationDAO(Context context) {
this.mContext = context;
mDbHelper = new SQLiteDatabaseData(context);
// open the database
try {
open();
} catch (SQLException e) {
Log.e(TAG, "SQLException on openning database " + e.getMessage());
e.printStackTrace();
}
}
public void open() throws SQLException {
mDatabase = mDbHelper.getWritableDatabase();
}
public void close() {
mDbHelper.close();
}
public Locations createLocation(String place, String location)
{
ContentValues values = new ContentValues();
values.put(SQLiteDatabaseData.COLUMN_PLACE, place);
values.put(SQLiteDatabaseData.COLUMN_LOCATION, location);
long insertId = mDatabase.insert(SQLiteDatabaseData.TABLE_LOC, null,values);
Cursor cursor = mDatabase.query(SQLiteDatabaseData.TABLE_LOC, mAllColumns,
SQLiteDatabaseData.COLUMN_ID + " = " + insertId, null, null,null,null);
cursor.moveToFirst();
Locations newLocations = cursorToLocation(cursor);
cursor.close();
return newLocations;
}
public void deleteLocation(Locations location) {
long id = location.getId();
// delete all employees of this company
LatLongDAO latlongDao = new LatLongDAO(mContext);
List<LatLong> listLatLong = latlongDao.getLocationsOfLoc(id);
if (listLatLong != null && !listLatLong.isEmpty()) {
for (LatLong le: listLatLong)
{
latlongDao.deleteLocation(le);
}
}
System.out.println("the deleted location has the id: " + id);
mDatabase.delete(SQLiteDatabaseData.TABLE_LOC, SQLiteDatabaseData.COLUMN_ID
+ " = " + id, null);
}
public List<Locations> getAllLocations() {
List<Locations> listLocations = new ArrayList<Locations>();
Cursor cursor = mDatabase.query(SQLiteDatabaseData.TABLE_LOC, mAllColumns,
null, null, null, null, null);
String query="SELECT * FROM " + SQLiteDatabaseData.TABLE_LOC;
//mDatabase=mDbHelper.getReadableDatabase();
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Locations locations = cursorToLocation(cursor);
listLocations.add(locations);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
}
return listLocations;
}
public Locations getCompanyById(long id) {
Cursor cursor = mDatabase.query(SQLiteDatabaseData.TABLE_LOC, mAllColumns,
SQLiteDatabaseData.COLUMN_ID + " = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
Locations locations = cursorToLocation(cursor);
return locations;
}
protected Locations cursorToLocation(Cursor cursor)
{
Locations location = new Locations();
location.setId(cursor.getLong(0));
location.setPlace(cursor.getString(1));
location.setLocation(cursor.getString(2));
return location;
}
This is the location adding class
public class AddLocations extends Activity implements OnClickListener, android.view.View.OnClickListener
{
public static final String TAG = "AddLocations";
EditText edtLocation;
EditText edtPlace;
Button btnGet;
Button btnSave;
private LocationDAO locDataDao;
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.track);
initViews();
this.locDataDao=new LocationDAO(this);
}
private void initViews()
{
this.edtPlace=(EditText)findViewById(R.id.editPlace);
this.edtLocation=(EditText)findViewById(R.id.editLoc);
this.btnGet=(Button)findViewById(R.id.buttonGet);
this.btnSave=(Button)findViewById(R.id.buttonSave);
this.btnSave.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.buttonSave:
Editable place=edtPlace.getText();
Editable location=edtLocation.getText();
if (!TextUtils.isEmpty(place) && !TextUtils.isEmpty(location))
{
Locations createdLocations=locDataDao.createLocation(place.toString(), location.toString());
Log.e(TAG, "added company : "+ createdLocations.getId());
Intent intent = new Intent();
intent.putExtra(ListLocationsActivity.EXTRA_ADDED_COMPANY, createdLocations);
setResult(RESULT_OK, intent);
Toast.makeText(this, "Location Added..", Toast.LENGTH_LONG).show();
finish();
}
else
{
Toast.makeText(this, "Empty location", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
locDataDao.close();
}
}
Like the same way created a class to read latlong. When clicking the buttonSave my app gets closed. My logcat is given below. Please help me to resolve this..
06-08 00:48:06.720: E/SQLiteConnection(14268): sqlite3_close(0xb7c69dc0) failed: 5
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): Failed to close connection, its fate is now in the hands of the merciful GC: SQLiteConnection: /data/data/com.example.databseexample/databases/locationdata.db (0)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): android.database.sqlite.SQLiteDatabaseLockedException: unable to close due to unfinalised statements (code 5): Count not close db.
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteConnection.nativeClose(Native Method)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteConnection.dispose(SQLiteConnection.java:240)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteConnection.close(SQLiteConnection.java:205)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteConnectionPool.closeConnectionAndLogExceptionsLocked(SQLiteConnectionPool.java:528)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteConnectionPool.closeAvailableConnectionsAndLogExceptionsLocked(SQLiteConnectionPool.java:501)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteConnectionPool.dispose(SQLiteConnectionPool.java:226)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteConnectionPool.close(SQLiteConnectionPool.java:205)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteDatabase.dispose(SQLiteDatabase.java:295)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:272)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:74)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteClosable.close(SQLiteClosable.java:106)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:278)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at com.example.databseexample.LocationDAO.open(LocationDAO.java:38)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at com.example.databseexample.LocationDAO.<init>(LocationDAO.java:30)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at com.example.databseexample.ListLocationsActivity.onCreate(ListLocationsActivity.java:44)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.app.Activity.performCreate(Activity.java:5231)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.os.Handler.dispatchMessage(Handler.java:102)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.os.Looper.loop(Looper.java:136)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at java.lang.reflect.Method.invoke(Method.java:515)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-08 00:48:06.740: E/SQLiteConnectionPool(14268): at dalvik.system.NativeStart.main(Native Method)
06-08 00:48:06.890: E/SQLiteConnection(14268): sqlite3_close(0xb7c69dc0) failed: 5
06-08 00:48:06.890: E/System(14268): Uncaught exception thrown by finalizer
06-08 00:48:06.900: E/System(14268): android.database.sqlite.SQLiteDatabaseLockedException: unable to close due to unfinalised statements (code 5): Count not close db.
06-08 00:48:06.900: E/System(14268): at android.database.sqlite.SQLiteConnection.nativeClose(Native Method)
06-08 00:48:06.900: E/System(14268): at android.database.sqlite.SQLiteConnection.dispose(SQLiteConnection.java:240)
06-08 00:48:06.900: E/System(14268): at android.database.sqlite.SQLiteConnection.finalize(SQLiteConnection.java:180)
06-08 00:48:06.900: E/System(14268): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
06-08 00:48:06.900: E/System(14268): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
06-08 00:48:06.900: E/System(14268): at java.lang.Thread.run(Thread.java:841)
06-08 00:48:07.160: E/AndroidRuntime(14268): FATAL EXCEPTION: main
06-08 00:48:07.160: E/AndroidRuntime(14268): Process: com.example.databseexample, PID: 14268
06-08 00:48:07.160: E/AndroidRuntime(14268): java.lang.StackOverflowError
06-08 00:48:07.160: E/AndroidRuntime(14268): at java.lang.CaseMapper.toUpperCase(CaseMapper.java:155)
06-08 00:48:07.160: E/AndroidRuntime(14268): at java.lang.String.toUpperCase(String.java:1548)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.DatabaseUtils.getSqlStatementType(DatabaseUtils.java:1379)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:892)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
06-08 00:48:07.160: E/AndroidRuntime(14268): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:46)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:48)
06-08 00:48:07.160: E/AndroidRuntime(14268): at com.example.databseexample.SQLiteDatabaseData.onCreate(SQLiteDatabaseData.java:
06-08 01:00:54.900: E/SQLiteConnection(17819): sqlite3_close(0xb7c69e10) failed: 5
06-08 01:00:54.960: E/SQLiteConnectionPool(17819): Failed to close connection, its fate is now in the hands of the merciful GC: SQLiteConnection: /data/data/com.example.databseexample/databases/locationdata.db (0)
06-08 01:00:54.960: E/SQLiteConnectionPool(17819): android.database.sqlite.SQLiteDatabaseLockedException: unable to close due to unfinalised statements (code 5): Count not close db.
Aucun commentaire:
Enregistrer un commentaire