Home > Software engineering >  How to insert Latitude and Longitude inside SQLite database - React Native
How to insert Latitude and Longitude inside SQLite database - React Native

Time:12-09

I am not able to save the Latitude and Longitude values into the SQLite database. What kind of type of data they are? I tried to insert them as REAL, BLOB, NUMERIC, and TEXT but they are not saved correctly. This is my code:

const createTable = () => {
        db.transaction((tx) => {
            tx.executeSql(
                "CREATE TABLE IF NOT EXISTS "
                  "Users "
                  "(ID INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT, Description TEXT, Latitude TEXT);"
            )
        })
    }
  //in the following function I want to insert Latitude into Users and display it in the console.log
  

    const setData = async () => {
          try {
            dispatch(setTitle(title));
            Geolocation.getCurrentPosition(info => dispatch(setLat(info.coords.latitude)));
    
            console.log('latitudine:',lat); // i have my current latitude
              await db.transaction(async (tx) => {
                await tx.executeSql(
                    "INSERT INTO Users (Title, Latitude) VALUES (?,?)",
                    [title, lat]
                );
            }); 
            await db.transaction(async (tx) => { 
              await tx.executeSql(
                "SELECT Title, Latitude FROM Users",
                    [],
                    (tx, results) => {
                         len = results.rows.length; 
                        if (len > 0) {
                          for (let i=0;i<len;i  ){
                            var userTitle = results.rows.item(i).Title;
                            var userLatitude = results.rows.item(i).Latitude; 
                            console.log('userTitle: ',userTitle); // no display
                            console.log('Latitude from database: ',userLatitude); //no display
                          }
                        }
                    }
                )
            });
          } catch (error) {
              console.log(error);
          }
         }

If I don't add the Latitude column in the database, the function works correctly and the userTitle is displayed correctly. I think the problem comes from the Latitude's column type but I am not sure of this. Someone can help me please? Very thanks!

Edit: if I have only 3 columns (ID, Title, Latitude) and latitude has values saved as a string I have no problem (thanks Hitesh Prajapati!). But... if I add a column in the database (for example Longitude), I have the problem again. In particular when I have to insert the new value inside the Longitude column. How can I solve this? Thanks

CodePudding user response:

As suggested by Hitesh Prajapati in a comment on the question, the latitude would need to be converted to a string before being saved in the SQLite db - based on the type defined for its column.

From the docs, getCurrentPosition returns an object whose coords property is a GeolocationCoordinates object with its longitude and latitude fields as the double type.

So, if you define the column for latitude as Latitude TEXT, then you need to convert the latitude to a string. The same would be required for the longitude column if the column type is set to TEXT:

...
// This assumes lat is assigned the value from info.coords.latitude
// And longitude is assigned the value from info.coords.longitude
await tx.executeSql(
    "INSERT INTO Users (Title, Latitude, Longitude) VALUES (?,?)",
    [title, lat.toString(), longitude.toString()]
);
...
  • Related