jeudi 5 novembre 2015

SQlite command not showing tables

I have set up Android adb tools on Mac.

So I followed that tutorial to get used to it.

My db is called: "MaschinenReview.db" To select the db I entered the following command:

sqlite3 MaschinenReview.db

and then to show the tables I typed:

.tables

But I do not get any output.

See the screenshot. Perhaps it shows what I mean.

What I finally want to do is to select the table I have created inside the database via an Android application and look what data is inside.

My DatabaseHandler Class:

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "MaschinenReview";

    private static final String TABLE_MASCHINEN = "maschinen";

    private static final String KEY_ID = "id";
    private static final String KEY_ANLAGE = "anlage";
    private static final String KEY_MASCHINE = "maschine";
    private static final String KEY_TYP = "typ";
    private static final String KEY_ARRIVALATSITE = "arrivalatsite";
    private static final String KEY_STARTERECTION = "starterection";
    private static final String KEY_POSITIONING = "positioning";
    private static final String KEY_INSTALLREMAININGPARRT = "installremainingpart";
    private static final String KEY_ALIGNMENT = "alignment";
    private static final String KEY_GROUTING = "grouting";
    private static final String KEY_FUNCTIONTEST = "functiontest";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_MASCHINEN_TABLE = "CREATE TABLE " + TABLE_MASCHINEN + "(" + KEY_ID + " INT PRIMARY KEY, " + KEY_ANLAGE + " INT, " + KEY_MASCHINE + " INT, " + KEY_TYP + " INT, " + KEY_ARRIVALATSITE + " BOOLEAN, " + KEY_STARTERECTION + " Boolean, " + KEY_POSITIONING + " BOOLEAN, " + KEY_INSTALLREMAININGPARRT + " INT, " + KEY_ALIGNMENT + " BOOLEAN, " + KEY_GROUTING + " INT, " + KEY_FUNCTIONTEST + " BOOLEAN)";
        db.execSQL(CREATE_MASCHINEN_TABLE);
    }

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

        onCreate(db);
    }

    public Maschine getMaschine(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_MASCHINEN,
               null, null, null, null, null, null);

        cursor.moveToFirst();

        // Maschine maschine = new Maschine(Integer.parseInt(cursor.getString(0)),
          //       Integer.parseInt(cursor.getString(1), Integer.parseInt(cursor.getString(2), Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)),
            //             (cursor.getInt(6) == 1), Integer.parseInt(cursor.getString(7)), Integer.parseInt(cursor.getString(8)), Integer.parseInt(cursor.getString(9)), Integer.parseInt(cursor.getString(10)))//;

        Maschine maschine = new Maschine();


        // TEST IF DATABASE RETRIEVES CORRECT VALUES
        boolean value1 = Boolean.parseBoolean(cursor.getString(1));
        boolean value2 = Boolean.parseBoolean(cursor.getString(2));
        boolean value3 = Boolean.parseBoolean(cursor.getString(3));
        boolean value4 = Boolean.parseBoolean(cursor.getString(4));
        boolean value5 = Boolean.parseBoolean(cursor.getString(5));

        return maschine;
    }

    public void addMaschine(int anlage, int maschine, int typ, boolean arrivalAtStart, boolean startErection, boolean positioning, int installRemainingPart, boolean alignment, int grouting, Boolean functionTest) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ANLAGE, anlage);
        values.put(KEY_MASCHINE, maschine);
        values.put(KEY_TYP, typ);
        values.put(KEY_ARRIVALATSITE, arrivalAtStart);
        values.put(KEY_STARTERECTION, startErection);
        values.put(KEY_POSITIONING, positioning);
        values.put(KEY_INSTALLREMAININGPARRT, installRemainingPart);
        values.put(KEY_ALIGNMENT, alignment);
        values.put(KEY_GROUTING, grouting);
        values.put(KEY_FUNCTIONTEST, functionTest);

        db.insert(TABLE_MASCHINEN, null, values);
        db.close();
    }
}

And I instantiate the db like that:

 DatabaseHandler db = new DatabaseHandler(getActivity());

        db.addMaschine(1, 1, 1, true, false, true, 1, false, 4, true);
        db.addMaschine(0, 1, 0, false, true, false, 1, false, 4, false);
        db.addMaschine(0, 1, 1, false, true, false, 0, false, 4, true);

        Maschine mMaschine = new Maschine();
        mMaschine = db.getMaschine(0);

I have figured out that the App does not get trough the onCreate Method of the DatabaseHandler and I do not understand why. But on the other hand I should get an error message when I add some data to a table that does not exist. Or am I wrong?

enter image description here

Aucun commentaire:

Enregistrer un commentaire