android-spatialite icon indicating copy to clipboard operation
android-spatialite copied to clipboard

AddGeometryColumn Error (InitSpatialMetadata related)

Open MSBilgin opened this issue 5 years ago • 7 comments

Hi,

I am trying to create spatialite db but no luck. Here is my code to

getWritableDatabase().rawQuery("select InitSpatialMetadata(1)", null);
String sqlCizim = "create table CIZIM(OBJECTID INTEGER PRIMARY KEY AUTOINCREMENT, KATMAN TEXT, VERI TEXT, TARIH TEXT)";
getWritableDatabase().rawQuery(sqlCizim, null);
String sqlCizimGeometri = "select AddGeometryColumn('CIZIM', 'GEOM', 3857,'GEOMETRY', 'XY')";
getWritableDatabase().rawQuery(sqlCizimGeometri, null);

MSBilgin avatar Aug 19 '19 09:08 MSBilgin

I noticed an interesting thing;

Firstly I generated a spatialite db by using QGIS and copied it to the device. Then successfully created a new table and added geometry column from Android app. It works without any problem with pre initialized spatialite db's. I think the problem is about InitSpatialMetadata() sql function. Do you have any advice ?

MSBilgin avatar Aug 19 '19 12:08 MSBilgin

Hi, Creating a new spatialite database requires that you load some extension which do not come with SQLITE by default.

I would suggest you create an empty database using the Spatialite_gui and copy it to your application. With that you can create new tables and add Geometry Columns.

phprad avatar Aug 20 '19 08:08 phprad

Hello, I think I do not get the problem. Could you please provide more info? I've made a test case with your code and do not see anything suspicious for now.

sevar83 avatar Aug 21 '19 10:08 sevar83

Hello, I think I do not get the problem. Could you please provide more info? I've made a test case with your code and do not see anything suspicious for now.

Here is sample code https://gist.github.com/MSBilgin/4174e151216d93225175459c465b3a47

MSBilgin avatar Aug 21 '19 14:08 MSBilgin

Ok, the problem is within InitSpatialMetaData(1) - particularly when the parameter "transaction" is set to 1 or TRUE. It makes the insertions of all spatial coordinate systems to be nested transactions within one single transaction. There is some issue with nesting transactions cause one test covering nested transactions fails for some reason. I have to insvestigate it deeper. But as a workaround I suggest to simply use InitSpatialMetaData(0). It would be slower but that's not more than a few secs and happens only once I guess.

sevar83 avatar Aug 21 '19 19:08 sevar83

I think the Android-friendly way to use Spatialite's functions that has the "transaction" parameter like:

  • InitSpatialMetaData()
  • InitSpatialMetaDataFull()
  • UpgradeGeometryTriggers()
  • ... and about 50 more

is to supply FALSE (0) to the transaction parameter and wrap them with beginTransaction()/endTransaction() like this:

try {
  database.beginTransaction();
  database.rawQuery("select InitSpatialMetaData(0)", null);
  database.setTransactionSuccessful();
finally {
  database.endTransaction();
}

sevar83 avatar Aug 23 '19 09:08 sevar83

I got the same problem,i solve this problem by run cursor. val cursor = database.rawQuery("select InitSpatialMetaData(0)", null); cursor.moveToFirst() cursor.close

xiaohepan avatar Feb 14 '23 12:02 xiaohepan