SQLite for android导致应用程序崩溃

时间:2016-04-16 12:47:54

标签: android sqlite

我想使用我的地方选择器将标记详细信息保存到android中的SQLite数据库。但是当我运行应用程序时,它会崩溃。有人可以告诉我哪里出错了。

我的数据库处理程序

public class HistoryDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1; // version
private static final String DATABASE_NAME = "searches.db"; // file saving on device
public static final String TABLE_HISTORY = "History"; // table name
public static final String COLUMN_ID = "PLACE ID";
public static final String COLUMN_NAME = "NAME";
public static final String COLUMN_ADDRESS = "ADDRESS";


public HistoryDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String query = "CREATE TABLE" + TABLE_HISTORY + "(" +
            COLUMN_ID + " INT PRIMARY KEY AUTO INCREMENT   " +
            COLUMN_NAME + "  TEXT   " +
            COLUMN_ADDRESS + "   TEXT  " + ")";

    db.execSQL(query);
}

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

}

public void addHistory(HistorySQLite history){
    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, history.get_name());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_HISTORY, null, values);
    db.close();
}

// print out database as string
public String printHistory(){
String dbString ="";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_HISTORY + "WHERE 1";

    db.close();
    return dbString;
}
}

构造

public class HistorySQLite {

private int _id;
private String _address;
private String _name;

public HistorySQLite(String _address, String _name, int _id){
        this._address = _address;
        this._name = _name;

}

public HistorySQLite(){

}

public void set_id(int _id) {
    this._id = _id;
}

public void set_address(String _address) {
    this._address = _address;
}

public void set_name(String _name) {
    this._name = _name;
}

public int get_id() {
    return _id;
}

public String get_address() {
    return _address;
}

public String get_name() {
    return _name;
}
}

这是我要求执行意图并添加到数据库

的地方选择器
    Place place = PlacePicker.getPlace(data, this);


        LatLng placeLatLng = place.getLatLng(); // gett lat lng from place
        double placeLat = placeLatLng.latitude;
        double placeLong = placeLatLng.longitude;
        final CharSequence name = place.getName();
        final CharSequence address = place.getAddress();
        final LatLng location = place.getLatLng();
        Marker destination = mMap.addMarker(new MarkerOptions().position(new LatLng(placeLat, placeLong)).title("This is your destination"));

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //Current Location
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location myLocation = locationManager.getLastKnownLocation(provider);

        //Current Location LatLong
        final double currentLat = myLocation.getLatitude();
        final double currentLng = myLocation.getLongitude();

        HistorySQLite history = new HistorySQLite(name.toString(),address.toString(), 1);

        //Directions From Current Location To Destination
        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?" + "saddr=" + currentLat + "," + currentLng + "&daddr=" + placeLat + "," + placeLong));
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");

        startActivity(intent);
        historyDBHandler.addHistory(history);


    }

中调用数据库
historyDBHandler = new HistoryDBHandler(this, null, null , 1); // allows the handler to control the content
   // printDatabase();
}

public void printDatabase(){
   String dbString = historyDBHandler.printHistory();
    textView2.setText(dbString);

}

logcat的

04-16 13:59:38.781 12748-12748/com.may.joe.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.may.joe.myapplication, PID: 12748
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.may.joe.myapplication/com.may.joe.myapplication.History}: android.database.sqlite.SQLiteException: near "TABLEHistory": syntax error (code 1): , while compiling: CREATE TABLEHistory(PLACE ID INT PRIMARY KEY AUTO INCREMENT   NAME  TEXT   ADDRESS   TEXT  )
                                                                       #################################################################
                                                                       Error Code : 1 (SQLITE_ERROR)
                                                                       Caused By : SQL(query) error or missing database.
                                                                        (near "TABLEHistory": syntax error (code 1): , while compiling: CREATE TABLEHistory(PLACE ID INT PRIMARY KEY AUTO INCREMENT   NAME  TEXT   ADDRESS   TEXT  ))
                                                                       #################################################################
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
                                                                           at android.app.ActivityThread.access$1000(ActivityThread.java:198)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:145)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6837)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                        Caused by: android.database.sqlite.SQLiteException: near "TABLEHistory": syntax error (code 1): , while compiling: CREATE TABLEHistory(PLACE ID INT PRIMARY KEY AUTO INCREMENT   NAME  TEXT   ADDRESS   TEXT  )
                                                                       #################################################################
                                                                       Error Code : 1 (SQLITE_ERROR)
                                                                       Caused By : SQL(query) error or missing database.
                                                                        (near "TABLEHistory": syntax error (code 1): , while compiling: CREATE TABLEHistory(PLACE ID INT PRIMARY KEY AUTO INCREMENT   NAME  TEXT   ADDRESS   TEXT  ))
                                                                       #################################################################
                                                                           at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                           at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
                                                                           at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
                                                                           at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                           at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                                                                           at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                           at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1812)
                                                                           at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743)
                                                                           at com.may.joe.myapplication.HistoryDBHandler.onCreate(HistoryDBHandler.java:36)
                                                                           at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                           at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                           at com.may.joe.myapplication.HistoryDBHandler.printHistory(HistoryDBHandler.java:55)
                                                                           at com.may.joe.myapplication.History.printDatabase(History.java:30)
                                                                           at com.may.joe.myapplication.History.onCreate(History.java:26)
                                                                           at android.app.Activity.performCreate(Activity.java:6500)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
                                                                            ... 10 more

2 个答案:

答案 0 :(得分:1)

你有&#34;创建表&#34;语法错误在dbHandler中的onCreate方法中。 试试这个:

public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_HISTORY + "(" +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_NAME + " TEXT, " +
        COLUMN_ADDRESS + " TEXT " + ");";

db.execSQL(query);

}

答案 1 :(得分:0)

创建表语法错误: 更改代码:

 String query = "CREATE TABLE" + TABLE_HISTORY + "(" +
                COLUMN_ID + " INT PRIMARY KEY AUTO INCREMENT   " +
                COLUMN_NAME + "  TEXT   " +
                COLUMN_ADDRESS + "   TEXT  " + ")";

        db.execSQL(query);
    }

    To

    String query = "CREATE TABLE" + TABLE_HISTORY + "(" +
                    + COLUMN_ID + " INTEGER PRIMARY KEY AUTO INCREMENT  ,"
                    + COLUMN_NAME + " TEXT,"
                    + COLUMN_ADDRESS + " TEXT" + ")";
           db.execSQL(query);
    }