mardi 4 août 2015

SQLite DB table not created in Android while DB created

I am working towards creating an android app. In fact I am beginner in android application development while I have good experience of Web development. So I now coding but Android app. and Java is new to me so please ask me if I am not clear of something about android app. that I am not mentioning here.

I am trying to create a DB and then creating my first table in SQLite. It is creating DB but not DB table.

I have created Contaract class.

package com.haafiz.project;

import android.provider.BaseColumns;

public final class ProjectContaract {

    public ProjectContaract(){}

    public static abstract class Site implements BaseColumns{
        public static final String TABLE_NAME = "site";
        public static final String COLUMN_NAME_SITE_NAME = "site_name";
        public static final String COLUMN_NAME_LOGIN = "login_name";
        public static final String COLUMN_NAME_HOST = "host";
        public static final String COLUMN_NAME_PASSWORD = "password";
    }
}

And then DB helper class.

package com.haafiz.project;

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


public class DBHelper extends SQLiteOpenHelper {

    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ",";
    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + ProjectContaract.Site.TABLE_NAME + " (" +
                    projectContaract.Site._ID + " INTEGER PRIMARY KEY," +
                    projectContaract.Site.COLUMN_NAME_SITE_NAME + TEXT_TYPE + COMMA_SEP +
                    projectContaract.Site.COLUMN_NAME_LOGIN + TEXT_TYPE + COMMA_SEP +
                    projectContaract.Site.COLUMN_NAME_HOST + TEXT_TYPE + COMMA_SEP +
                    projectContaract.Site.COLUMN_NAME_PASSWORD + TEXT_TYPE + " )";

    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + ProjectContaract.Site.TABLE_NAME;

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "Project";

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

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}

And here my main activity using that DB Helper as below:

package com.haafiz.project;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        projectDBHelper dbHelper = new projectDBHelper(getApplicationContext());

        // Gets the data repository in read mode
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        // Projection that specifies which columns from the database
        String[] projection = {
                projectContaract.Site._ID,
                projectContaract.Site.COLUMN_NAME_SITE_NAME,
                projectContaract.Site.COLUMN_NAME_LOGIN,
                projectContaract.Site.COLUMN_NAME_HOST,
                projectContaract.Site.COLUMN_NAME_PASSWORD
        };

        // How you want the results sorted in the resulting Cursor
        String sortOrder =
                projectContaract.Site.COLUMN_NAME_SITE_NAME + " DESC";

        Cursor c = db.query(
                projectContaract.Site.TABLE_NAME,  // The table to query
                projection,                               // The columns to return
                null,                                // The columns for the WHERE clause
                null,                            // The values for the WHERE clause
                null,                                     // don't group the rows
                null,                                     // don't filter by row groups
                sortOrder                                 // The sort order
        );

        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

DB is being created but table is not, I check that with adb shell attach with emulator. So please let me know what am I doing wrong due to which table is not being created.

And an important thing is that I am not getting errors related to this in log.

thanks

Aucun commentaire:

Enregistrer un commentaire