0

I have created a Database called location.db. After that, i tried to create a table LOCATIONS. When I do this, i don't get any error , but the table seems not be created at all. To verify the creation of table i´m using sqlite3 from command line, where i have seen that the code to create the table is ok but for any unknown reason is not in fact created. Thanks in advanced.

  package com.carlos.googlemapstest.db;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

/**
 * Created by Carlos on 07/08/2015.
 */
public class LocationsHelper extends SQLiteOpenHelper {
    //base de datos
    private static final String DATABASE_NAME="location.db";
    //tabla
    private static final String TABLE_NAME="LOCATIONS";
    //columnas
    private static final String UID="_id";
    private static final String NOMBRE="Nombre";
    private static final String LATITUD="Latitud";
    private static final String LONGITUD="Longitud";
    private static final String INFO="Info";
    //database version
    private static final int DATABASE_VERSION=1;
    //create database query
    private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
            +NOMBRE+" VARCHAR(255),"
            +LATITUD+" REAL,"
            +LONGITUD+" REAL,"
            +INFO+" VARCHAR(255)"+")";
    //private static final String CREATE_TABLE1="CREATE TABLE PRUEBA (nombre TEXT);";
    private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;
    private Context context;

    public LocationsHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context=context;
        Toast.makeText(context,"Constructor called",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Toast.makeText(context,"onCreate called",Toast.LENGTH_SHORT).show();
        try {

            db.execSQL(CREATE_TABLE);


        } catch (SQLException e) {
            Toast.makeText(context,"Error onCreate:"+e,Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Toast.makeText(context,"onUpgraded called",Toast.LENGTH_SHORT).show();
        try {
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            Toast.makeText(context,"Error onUpgrade:"+e,Toast.LENGTH_SHORT).show();
        }
    }
}
Carlos Hernández Gil
  • 1,853
  • 2
  • 22
  • 30
  • Have you uninstalled the app on the device? Maybe you ran an older version of you LocationsHelper before and now you never hit onCreate. Have you verified onCreate() is called? – fweigl Aug 07 '15 at 18:34
  • Try to uninstall your app and ther run it again. Also you can try to increase the database version number. – PetrS Aug 07 '15 at 18:38
  • Can you post some output? More information is needed. – CantThinkOfAnything Aug 07 '15 at 18:41
  • @PSglass wouldnt it be better to delete the database instead of upgrading it? – CantThinkOfAnything Aug 07 '15 at 18:41
  • @PSglass putting context.deleteDatabase(String databaseName); inside the constructor would ensure that a fresh database is created everytime. – CantThinkOfAnything Aug 07 '15 at 18:43
  • when i uninstall the app the database is deleted also. I have rerun my app again and now it work great. Thank guys – Carlos Hernández Gil Aug 07 '15 at 18:49
  • 1
    @CantThinkOfAnything I think that the best check is reinstall the app. In case that reinstall did not help, there is a problem in code. Increase the DB version is faster way, but I know that there is a better solution. I suggested it only for check if code is OK. – PetrS Aug 07 '15 at 19:02

0 Answers0