0

我正在尝试创建一个应用程序来获取您当前的地理位置并将其存储到 SQLite 数据库中。然后,我希望能够检索该位置以查看。我有获取位置的代码,但是在创建数据库后,应用程序崩溃了。

这是我的主要活动代码

public class WhereAmI extends Activity {

SQLiteOpenHelper dbhelper;
SQLiteDatabase database;

double lat;
double lng;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);

  Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);

locationManager.requestLocationUpdates(provider, 2000, 10,
                                       locationListener);

//Open Database
dbhelper = new dbHelper(this);
database = dbhelper.getWritableDatabase();

//ContentValues cv = new ContentValues();
//cv.put(dbHelper.COULMN_LON, lng);
//cv.put(dbHelper.COLUMN_LAT, lat);
//database.insert(dbHelper.TABLE_LOCATION, null, cv);
}

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
  updateWithNewLocation(location);
}

public void onProviderDisabled(String provider){
  updateWithNewLocation(null);
}

public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status, 
                            Bundle extras){ }
};

private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText; 
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null) {
  lat = location.getLatitude();
  lng = location.getLongitude();
  latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
  latLongString = "No location found"; 
}
myLocationText.setText("Your Current Position is:\n" + 
                       latLongString);

}

}


Here is my Database Class

package com.paad.whereami;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class dbHelper extends SQLiteOpenHelper {


public static final String DATABASE_NAME = "geo.db";
public static final int DATABASE_VERSION = 1;

public static final String TABLE_LOCATION = "tablename";
public static final String COLUMN_ID = "locationId";
public static final String COULMN_LON = "longitude";
public static final String COLUMN_LAT = "latitude";

private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_LOCATION + 
        " (" + COLUMN_ID + " INTEGER RIMARY KEY AUTOINVREMENT, " 
        + COULMN_LON + " DOUBLE, " + COLUMN_LAT + " DOUBLE, " + ")";


//Context context;


public dbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_LOCATION);




}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATION);
    onCreate(db);

}

}

这是我得到的 Android 运行时错误。

02-22 06:46:57.543: E/AndroidRuntime(1526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.whereami/com.paad.whereami.WhereAmI}: android.database.sqlite.SQLiteException: near "tablename": syntax error (code 1): , while compiling: tablename
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.os.Looper.loop(Looper.java:137)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-22 06:46:57.543: E/AndroidRuntime(1526): Caused by: android.database.sqlite.SQLiteException: near "tablename": syntax error (code 1): , while compiling: tablename
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.Activity.performCreate(Activity.java:5104)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-22 06:46:57.543: E/AndroidRuntime(1526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
4

2 回答 2

2

这是 onCreate 方法中的一个简单错误,您使用了错误的常量。

将 onCreate 方法更改为

public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
}
于 2013-02-22T06:59:38.747 回答
1

PRIMARY KEY 和 AUTOINCREMENT 中的拼写错误

最后一个 DOUBLE 之后还有一个额外的“,”

改成...

private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_LOCATION + 
        " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
        + COULMN_LON + " DOUBLE, " + COLUMN_LAT + " DOUBLE );";
于 2013-02-22T06:58:50.610 回答