Android无法打开/创建SQLiteDatabase

时间:2014-03-30 22:38:20

标签: java android sqlite

您好,我在从另一个类调用时打开/创建SQLiteDatabase时遇到问题。我现在是学生,对我来说这是一个新手,所以请残酷地清楚任何答案,以便我可以跟随。

Route.java下的Testing()调用数据库类来设置数据库

DatabaseManager db = new DatabaseManager(Route.this, "dbSkyTrain.dbf", null, 1);

当DatabaseManager尝试运行这行代码时崩溃。

非常感谢您提供任何帮助。我很确定我只是把它设置错了。

Route.java:

public void Testing(){

        //Station Variables
        int StnID;           //Unique ID, Format: ##
        String StnLine;      //Format: XXX = Mil, Exp, Eve, Can or YVR
        int StnLineIndex;    //Format: ##
        int StnZone;         //1, 2, 3
        int StnTimePrev;     //Time in min to this station from previous station
        int StnTimeNext;     //Time in min from this station to the next station
        String StnSwitch;    //Is this a switching station T or F
        String StnName;      //Name

        //Path Variables
        int PathStartIndex;  //Start station index
        int PathEndIndex;    //Destination or Switching station index
        String PathLine;     //Line that the train is currently on
        String PathSwitch;   //Name of switching station or "False" if end index is destination

        DatabaseManager db = new DatabaseManager(Route.this, "dbSkyTrain.dbf", null, 1);
        Station station = new Station();
        Path path = new Path();


        //Get Station usage
        station = db.getStation("Lougheed");
        StnID = station.ID();
        StnLine = station.Line();
        StnLineIndex = station.LineIndex();
        StnZone = station.Zone();
        StnTimePrev = station.TimeNext();
        StnTimeNext = station.TimePrev();
        StnSwitch = station.Switch();
        StnName = station.Name();

        //alternative get station method
        station = db.getStation("Mil", 10);
        StnID = station.ID();
        StnLine = station.Line();
        StnLineIndex = station.LineIndex();
        StnZone = station.Zone();
        StnTimePrev = station.TimeNext();
        StnTimeNext = station.TimePrev();
        StnSwitch = station.Switch();
        StnName = station.Name();
        db.close();


        //Path usage - returns endIndex of destination station or switching station
        path = getPath("Lougheed", "YVR-Airport");
        PathStartIndex = path.StartIndex();
        PathEndIndex = path.EndIndex();
        PathLine = path.Line();
        PathSwitch = path.SwitchStn();

        Toast.makeText(Route.this, PathLine + " " + PathStartIndex + " " + PathEndIndex + " " + PathSwitch , Toast.LENGTH_LONG).show();




    }

    //Get Route instructions//////////////////////////////////////////////////////////////////////////////////////
    public Path getPath(String StartStation, String EndStation){

        String strSQL;
        String strLine, strLineStart, strLineEnd;
        int intIndexStart, intIndexEnd;
        String strSwitch;

        //Table Station Variables
        String tbl_Stations = "tblStations";

        String key_StnID = "StnID";
        String key_StnName = "StnName";
        String key_StnLine = "StnLine";
        String key_StnLineIndex = "StnLineIndex";
        String key_StnZone = "StnZone";
        String key_StnTimePrev = "StnTimePrev";
        String key_StnTimeNext = "StnTimeNext";
        String key_StnSwitch = "StnSwitch";



        DatabaseManager dbManager = new DatabaseManager(Route.this, "dbSkyTrain.dbf", null, 1);
        SQLiteDatabase db = dbManager.getWritableDatabase();

        //First Station
        strSQL = "SELECT * FROM " + tbl_Stations + " WHERE " + key_StnName + "=" + StartStation;
        Cursor c = db.rawQuery(strSQL, null);
        c.moveToFirst();

        Station station = new Station(c.getString(0), c.getString(1), c.getString(2), c.getString(3), c.getString(4), c.getString(5), c.getString(6), c.getString(7));

        strLineStart = station.Line();
        intIndexStart = station.LineIndex();


        //Second Station
        strSQL = "SELECT * FROM " + tbl_Stations + " WHERE " + key_StnName + "=" + EndStation;
        c = db.rawQuery(strSQL, null);
        c.moveToFirst();

        station = new Station(c.getString(0), c.getString(1), c.getString(2), c.getString(3), c.getString(4), c.getString(5), c.getString(6), c.getString(7));

        strLineEnd = station.Line();
        intIndexEnd = station.LineIndex();

        db.close();

DatabaseManager.java

package com.example.skytrainfinalproject;

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

public class DatabaseManager extends SQLiteOpenHelper{

    //Database variables
    String db_Name = "dbSkyTrain.dbf";

    //Table Station Variables
    String tbl_Stations = "tblStations";

    String key_StnID = "StnID";
    String key_StnName = "StnName";
    String key_StnLine = "StnLine";
    String key_StnLineIndex = "StnLineIndex";
    String key_StnZone = "StnZone";
    String key_StnTimePrev = "StnTimePrev";
    String key_StnTimeNext = "StnTimeNext";
    String key_StnSwitch = "StnSwitch";

    //Table Options Variables
    String tbl_Options = "tblOptions";

    String key_OptID = "OptID";
    String key_OptName = "OptName";
    String key_OptValue = "OptValue";

    //Table GPS Variables
    String tbl_Gps = "tblGps";

    String key_GpsID = "GpsID";
    String key_GpsName = "GpsName";
    String key_Gpslongitude = "Gpslongitude";
    String key_Gpslatitude = "Gpslatitude";


    //Constructor//////////////////////////////////////////////////////////////////////////////////////
    public DatabaseManager(Context context, String dbName, CursorFactory factory,
            int version) {
        super(context, dbName, factory, version);


    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        //Drop tables -comment out tables to save
        db.execSQL("DROP TABLE IF EXISTS " + tbl_Stations);
        db.execSQL("DROP TABLE IF EXISTS " + tbl_Options);
        //db.execSQL("DROP TABLE IF EXISTS " + tbl_Gps);

        //Recreate tables
        CreateTblStations();
        CreateTblOptions();
        //CreateTblGps();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

    //Create GPS Table//////////////////////////////////////////////////////////////////////////////////////
    public void CreateTblGps(){

    }

    //Add GPS//////////////////////////////////////////////////////////////////////////////////////
    public void addGps(){

    }

    //Get GPS//////////////////////////////////////////////////////////////////////////////////////
    public void getGps(){

    }

    //Create Options Table//////////////////////////////////////////////////////////////////////////////////////
    public void CreateTblOptions(){

        SQLiteDatabase db = getWritableDatabase();

        //Table Values
        String Create_tblOptions = "CREATE TABLE IF NOT EXISTS " + tbl_Options + "(" + 
                                    key_OptID + " INTEGER PRIMARY KEY," + 
                                    key_OptName +" TEXT," + 
                                    key_OptValue + " TEXT)";

        //Create Table
        db.execSQL(Create_tblOptions); 


        addOption(db, "01", "TicketDefault", "Adult");

        addOption(db, "02", "TicketA1", "2.75");
        addOption(db, "03", "TicketA2", "4.00");
        addOption(db, "04", "TicketA3", "5.50");

        addOption(db, "05", "TicketC1", "1.75");
        addOption(db, "06", "TicketC2", "2.75");
        addOption(db, "07", "TicketC3", "3.75");

        addOption(db, "08", "AfterHours", "6:30");
        addOption(db, "09", "YVRCharge", "5.00");

        db.close();     
    }

    //Add Option//////////////////////////////////////////////////////////////////////////////////////
    public void addOption(SQLiteDatabase db, String ID, String Name, String Value){

        String insertRow;


        insertRow = "INSERT INTO " + tbl_Options + " values ('" + Integer.parseInt(ID) + "', '" + Name + "', '" + Value + "');";
        db.execSQL(insertRow);

    }

    //Get Option//////////////////////////////////////////////////////////////////////////////////////
    public Option getOption(int ID){

        String strSQL;

        SQLiteDatabase db = getReadableDatabase();

        strSQL = "SELECT * FROM tblOptions WHERE OptID=" + String.valueOf(ID);
        Cursor c = db.rawQuery(strSQL, null);
        c.moveToFirst();

        Option option = new Option(c.getString(0), c.getString(1), c.getString(2));

        c.close();
        db.close();

        return option;
    }

    //Create Station Table//////////////////////////////////////////////////////////////////////////////////////
    public void CreateTblStations(){

        SQLiteDatabase db = getWritableDatabase();

        //Table Values
        String Create_tblStations = "CREATE TABLE IF NOT EXISTS " + tbl_Stations + "(" + 
                                    key_StnID + " INTEGER PRIMARY KEY," + 
                                    key_StnLine +" TEXT," + 
                                    key_StnLineIndex + " INTEGER," + 
                                    key_StnZone + " INTEGER," +
                                    key_StnTimePrev + " INTEGER," + 
                                    key_StnTimeNext + " INTEGER," +
                                    key_StnSwitch + " TEXT," + 
                                    key_StnName + " TEXT)"; 

        //Create Table
        db.execSQL(Create_tblStations); 



        //Fill Table

        //Format (StnID, LineIndex, Line, Zone, TimePrev, TimeNext, Switch, Name)           
        addStation(db, "01", "19", "Exp", "3", "2", "3", "F", "King George");
        addStation(db, "02", "18", "Exp", "3", "3", "2", "F", "Surrey Central");
        addStation(db, "03", "17", "Exp", "3", "2", "3", "F", "Gateway");
        addStation(db, "04", "16", "Exp", "3", "3", "2", "F", "Scott Road");

        addStation(db, "05", "28", "Mil", "1", "1", "0", "F", "VCC-Clark");
        addStation(db, "06", "27", "Mil", "1", "3", "1", "T", "Commercial");
        addStation(db, "07", "26", "Mil", "1", "1", "3", "F", "Renfrew");
        addStation(db, "08", "25", "Mil", "1", "3", "1", "F", "Rupert");
        addStation(db, "09", "24", "Mil", "2", "1", "3", "F", "Gilmore");
        addStation(db, "10", "23", "Mil", "2", "2", "1", "F", "Brentwood");
        addStation(db, "11", "22", "Mil", "2", "2", "2", "F", "Holdom");
        addStation(db, "12", "21", "Mil", "2", "2", "2", "F", "Sperling/Burnaby Lake");
        addStation(db, "13", "20", "Mil", "2", "3", "2", "F", "Lake City");
        addStation(db, "14", "19", "Mil", "2", "2", "3", "F", "Production Way/University");
        addStation(db, "15", "18", "Mil", "2", "3", "2", "T", "Lougheed");
        addStation(db, "16", "17", "Mil", "2", "2", "3", "F", "Braid");
        addStation(db, "17", "16", "Mil", "2", "3", "2", "F", "Sapperton");

        addStation(db, "18", "15", "Mil-Exp", "2", "1", "3", "T", "Columbia");
        addStation(db, "19", "14", "Mil-Exp", "2", "4", "1", "F", "New Westminster");
        addStation(db, "20", "13", "Mil-Exp", "2", "2", "4", "F", "22nd St");
        addStation(db, "21", "12", "Mil-Exp", "2", "3", "2", "F", "Edmonds");
        addStation(db, "22", "11", "Mil-Exp", "2", "2", "3", "F", "Royal Oak");
        addStation(db, "23", "10", "Mil-Exp", "2", "1", "2", "F", "Metrotown");
        addStation(db, "24", "09", "Mil-Exp", "1", "2", "1", "F", "Patterson");
        addStation(db, "25", "08", "Mil-Exp", "1", "2", "2", "F", "Joyce");
        addStation(db, "26", "07", "Mil-Exp", "1", "1", "2", "F", "29th Ave");
        addStation(db, "27", "06", "Mil-Exp", "1", "3", "1", "F", "Nanaimo");
        addStation(db, "28", "05", "Mil-Exp", "1", "3", "3", "T", "Commercial");
        addStation(db, "29", "04", "Mil-Exp", "1", "2", "3", "F", "Main");
        addStation(db, "30", "03", "Mil-Exp", "1", "1", "2", "F", "Stadium");
        addStation(db, "31", "02", "Mil-Exp", "1", "1", "1", "F", "Granville");
        addStation(db, "32", "01", "Mil-Exp", "1", "2", "1", "F", "Burrard");

        addStation(db, "33", "00", "Mil-Exp-Can-YVR", "1", "0", "2", "T", "Waterfront");

        addStation(db, "34", "01", "Can-YVR", "1", "2", "2", "F", "Vancouver City Center");
        addStation(db, "35", "02", "Can-YVR", "1", "2", "2", "F", "Yaletown-Roundhouse");
        addStation(db, "36", "03", "Can-YVR", "1", "2", "1", "F", "Olympic Village");
        addStation(db, "37", "04", "Can-YVR", "1", "1", "2", "F", "Broadway-City Hall");
        addStation(db, "38", "05", "Can-YVR", "1", "2", "3", "F", "King Edward");
        addStation(db, "39", "06", "Can-YVR", "1", "3", "2", "F", "Oakridge-41st");
        addStation(db, "40", "07", "Can-YVR", "1", "2", "3", "F", "Langara-49th");
        addStation(db, "41", "08", "Can-YVR", "1", "3", "2", "F", "Marine Drive");
        addStation(db, "42", "09", "Can-YVR", "2", "2", "9", "T", "Bridgeport");

        addStation(db, "43", "10", "Can", "2", "2", "2", "F", "Aberdeen");
        addStation(db, "44", "11", "Can", "2", "2", "2", "F", "Lansdowne");
        addStation(db, "45", "12", "Can", "2", "2", "0", "F", "Brighouse");

        addStation(db, "46", "10", "YVR", "2", "9", "2", "F", "Templeton");
        addStation(db, "47", "11", "YVR", "2", "2", "2", "F", "Sea Island Center");
        addStation(db, "48", "12", "YVR", "2", "2", "0", "F", "YVR-Airport");

        db.close();

    }

    //Add Station//////////////////////////////////////////////////////////////////////////////////////
    public void addStation(SQLiteDatabase db, String ID, String LineIndex, String Line, String Zone, String TimePrev, String TimeNext, String Switch, String Name){

        String insertRow;


        insertRow = "INSERT INTO " + tbl_Stations + " values ('" + Integer.parseInt(ID) + "', '" + Integer.parseInt(LineIndex) + "', '" + Line + "', '" + Integer.parseInt(Zone) + "', '" + Integer.parseInt(TimePrev) + "', '" + Integer.parseInt(TimeNext) + "', '" + Switch + "', '" + Name + "');";
        db.execSQL(insertRow);


    }

    //Get Station//////////////////////////////////////////////////////////////////////////////////////
    public Station getStation(String strStnName){

        String strSQL;

        SQLiteDatabase db = getReadableDatabase();

        strSQL = "SELECT * FROM " + tbl_Stations + " WHERE " + key_StnName + "=" + strStnName;
        Cursor c = db.rawQuery(strSQL, null);
        c.moveToFirst();

        Station station = new Station(c.getString(0), c.getString(1), c.getString(2), c.getString(3), c.getString(4), c.getString(5), c.getString(6), c.getString(7));

        c.close();
        db.close();

        return station;

    }

    public Station getStation(String strLine, int intLineIndex){

        String strSQL;

        SQLiteDatabase db = getReadableDatabase();

        strSQL = "SELECT * FROM " + tbl_Stations + " WHERE " + key_StnLine + "=" + strLine + " AND " + key_StnLineIndex + "=" + String.valueOf(intLineIndex);
        Cursor c = db.rawQuery(strSQL, null);
        c.moveToFirst();

        Station station = new Station(c.getString(0), c.getString(1), c.getString(2), c.getString(3), c.getString(4), c.getString(5), c.getString(6), c.getString(7));

        c.close();
        db.close();

        return station;

    }





}

错误记录

03-30 18:23:32.736: W/ApplicationPackageManager(27014): getCSCPackageItemText()
03-30 18:23:35.038: D/AndroidRuntime(27014): Shutting down VM
03-30 18:23:35.038: W/dalvikvm(27014): threadid=1: thread exiting with uncaught exception (group=0x4189cda0)
03-30 18:23:35.048: E/AndroidRuntime(27014): FATAL EXCEPTION: main
03-30 18:23:35.048: E/AndroidRuntime(27014): Process: com.example.skytrainfinalproject, PID: 27014
03-30 18:23:35.048: E/AndroidRuntime(27014): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.skytrainfinalproject/com.example.skytrainfinalproject.Route}: java.lang.IllegalStateException: getDatabase called recursively
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.os.Looper.loop(Looper.java:157)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.ActivityThread.main(ActivityThread.java:5356)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at java.lang.reflect.Method.invoke(Method.java:515)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at dalvik.system.NativeStart.main(Native Method)
03-30 18:23:35.048: E/AndroidRuntime(27014): Caused by: java.lang.IllegalStateException: getDatabase called recursively
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at com.example.skytrainfinalproject.DatabaseManager.CreateTblStations(DatabaseManager.java:152)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at com.example.skytrainfinalproject.DatabaseManager.onCreate(DatabaseManager.java:61)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at com.example.skytrainfinalproject.DatabaseManager.getStation(DatabaseManager.java:249)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at com.example.skytrainfinalproject.Route.Testing(Route.java:62)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at com.example.skytrainfinalproject.Route.onCreate(Route.java:28)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.Activity.performCreate(Activity.java:5426)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-30 18:23:35.048: E/AndroidRuntime(27014):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
03-30 18:23:35.048: E/AndroidRuntime(27014):    ... 11 more
03-30 18:23:37.160: I/Process(27014): Sending signal. PID: 27014 SIG: 9

1 个答案:

答案 0 :(得分:0)

如果您使用的是SQLiteOpenHelper,那么您很可能不想调用SQLiteDatabase.openOrCreateDatabase(...)。辅助类为SQLiteDatabase提供了一个抽象层。您的构造函数应如下所示:

public DatabaseManager(Context context, String dbName, CursorFactory factory,
        int version) {
    super(context, dbName, factory, version);
}

然后您可以像这样使用您的数据库:

DatabaseManager dbManager = new DatabaseManager(context, "dbSkyTrain.dbf", null, 1);
SQLiteDatabase db = dbManager.getWritableDatabase();
// Do database operations
db.close();

将根据需要调用onCreate方法。