我的表不是在sqlite数据库中创建的

时间:2015-08-07 18:28:12

标签: android sqlite

我创建了一个名为location.db的数据库。在那之后,我试图创建一个表LOCATIONS。当我这样做时,我没有得到任何错误,但似乎根本没有创建表。要验证表的创建我是从命令行使用sqlite3,我已经看到创建表的代码是可以的,但实际上并没有创建任何未知的原因。谢谢你提前。

  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();
        }
    }
}

0 个答案:

没有答案