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

PRIMARYKEYと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 に答える