为什么在Android SQLite中无法识别数据库? [英] Why the database can not be known in Android SQLite?

查看:114
本文介绍了为什么在Android SQLite中无法识别数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个与带有标记的SQLite连接的应用程序Maps,但是存在无法识别数据库的错误,为什么会发生?有谁能够帮助我?这是我的代码:

I'm making an application Maps connected with SQLite which there are markers, but there is an error that the database can not be recognized, why did it happen? Can anybody help me? This my code :

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);
    }

}

MainActivity.java

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();

    }

}

LogCat

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)

推荐答案

Marker.db 应该是数据库的名称,并且应该在构造函数中传递它.表的名称应简单地为 Marker :

Marker.db should be the name of your database and you should pass it in your constructor. The name of your table should be simply 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);

}

...

这篇关于为什么在Android SQLite中无法识别数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆