Ich möchte meine Platzwähler verwenden, um die Markierungsdetails in einer SQLite-Datenbank in Android zu speichern. Aber wenn ich die App starte, stürzt sie ab. Kann mir jemand sagen, wo ich falsch liege? SQLite für Android verursacht Absturz der App
mein DB-Handler
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;
}
}
Konstrukteurs
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;
}
}
Dies ist der Ort, Picker, in dem gefragt im eine Absicht auszuführen, und auch zur Datenbank
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);
}
Aufruf der hinzufügen Datenbank in
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
Können Sie die logcat des Absturzes posten? – NSimon
Warum verwenden Sie kein ORM? Es ist schmerzhaft, direkt mit dem SQlite zu arbeiten. – jonathanrz
@Joseph Jay Mai ..können Sie zeigen Ihre Fehlerprotokoll .. –