我正在制作一个应用程序地图与SQLite连接有标记,但是有一个错误,数据库无法识别,为什么会发生?有谁能够帮我?这是我的代码:
public class MaBase extends SQLiteOpenHelper {
private static final String TABLE_MARK ="marker.db";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_MARK + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONG + " TEXT NOT NULL, " +COL_LAT+" TEXT NOT NULL);";
public MaBase (Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//on créé la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_BDD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE " + TABLE_MARK + ";");
onCreate(db);
}
}
public class MainActivity extends Activity {
private static final String NOM_BDD = "marker.db";
private static final String TABLE_GEOPOINT = "geopoint";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";
static final LatLng BOGOR = new LatLng(-6.604346, 106.796642);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_map);
sauver_point();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
Cursor c = db.query(TABLE_GEOPOINT, new String[] { COL_ID, COL_LONG,
COL_LAT }, null, null, null, null, null, null);
int col = c.getCount(); // col=0 pas de enregistrement qui verifie la
// condition
if (col == 0) {
Toast.makeText(MainActivity.this, "Pas de donnees ",
Toast.LENGTH_LONG).show();
} else {
c.moveToFirst();
while (c.isAfterLast() == false) {
// conversion int to string casting
String id = "" + c.getInt(0);
String longitude = c.getString(1);
String latitude = c.getString(2);
Marker marker = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble(latitude),
Double.parseDouble(longitude)))
.title("Bonjour Tunis")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark2)));
c.moveToNext();
}
}
c.close();
db.close();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(BOGOR, 12.0f));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
void sauver_point() {
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(COL_ID , "1");
values.put(COL_LAT, "36.830722");
values.put(COL_LONG, "10.165672");
db.insert(TABLE_GEOPOINT, null, values);
// creer un autre utilisateur
values = new ContentValues();
values.put(COL_LAT , "36.830922");
values.put(COL_LONG, "10.275572");
db.insert(TABLE_GEOPOINT, null, values);
values = new ContentValues();
values.put(COL_LAT, "36.930522");
values.put(COL_LONG, "10.385572");
db.insert(TABLE_GEOPOINT, null, values);
values = new ContentValues();
values.put(COL_LAT, "36.750422");
values.put(COL_LONG, "10.495572");
db.insert(TABLE_GEOPOINT, null, values);
values.put(COL_LAT, "36.936422");
values.put(COL_LONG, "11.495572");
db.insert(TABLE_GEOPOINT, null, values);
values.put(COL_LAT, "36.990422");
values.put(COL_LONG, "9.995572");
db.insert(TABLE_GEOPOINT, null, values);
db.close();
}
}
02-12 02:56:00.075: E/SQLiteLog(3453): (1) unknown database marker
02-12 02:56:00.075: D/AndroidRuntime(3453): Shutting down VM
02-12 02:56:00.075: W/dalvikvm(3453): threadid=1: thread exiting with uncaught exception (group=0xa6195908)
02-12 02:56:00.075: E/AndroidRuntime(3453): FATAL EXCEPTION: main
02-12 02:56:00.075: E/AndroidRuntime(3453): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bongkorr/com.bongkorr.maps.MainActivity}: android.database.sqlite.SQLiteException: unknown database marker (code 1): , while compiling: CREATE TABLE marker.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, LONGITUDE TEXT NOT NULL, LATITUDE TEXT NOT NULL);
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.os.Looper.loop(Looper.java:137)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-12 02:56:00.075: E/AndroidRuntime(3453): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 02:56:00.075: E/AndroidRuntime(3453): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 02:56:00.075: E/AndroidRuntime(3453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-12 02:56:00.075: E/AndroidRuntime(3453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-12 02:56:00.075: E/AndroidRuntime(3453): at dalvik.system.NativeStart.main(Native Method)
02-12 02:56:00.075: E/AndroidRuntime(3453): Caused by: android.database.sqlite.SQLiteException: unknown database marker (code 1): , while compiling: CREATE TABLE marker.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, LONGITUDE TEXT NOT NULL, LATITUDE TEXT NOT NULL);
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-12 02:56:00.075: E/AndroidRuntime(3453): at com.bongkorr.maps.MaBase.onCreate(MaBase.java:31)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-12 02:56:00.075: E/AndroidRuntime(3453): at com.bongkorr.maps.MainActivity.sauver_point(MainActivity.java:86)
02-12 02:56:00.075: E/AndroidRuntime(3453): at com.bongkorr.maps.MainActivity.onCreate(MainActivity.java:35)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.Activity.performCreate(Activity.java:5104)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-12 02:56:00.075: E/AndroidRuntime(3453): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
答案 0 :(得分:1)
marker.db
作为表名会告诉您在数据库db
中查找表名marker
并且您没有附加数据库marker
。
将表格名称用"marker.db"
等双引号写成,或者更好的是,将表格重命名为更适合您放置的内容,而不是.
。
答案 1 :(得分:0)
Marker.db
应该是数据库的名称,您应该在构造函数中传递它。您的表格名称应为Marker
:
public class MaBase extends SQLiteOpenHelper {
private static final String DB_MARK ="marker.db";
private static final String TABLE_MARK ="marker";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_MARK + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONG + " TEXT NOT NULL, " +COL_LAT+" TEXT NOT NULL);";
public MaBase (Context context, String name, CursorFactory factory, int version) {
super(context, DB_MARK, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//on créé la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_BDD);
}
...