My SQLite database is not generated

1

I am making a music player for android and I am developing it in android studio . I get to show and play the songs on my device, but when inserting the "paths" of those songs in the database, I can not even get this bbdd created.

I know that this database is not created, because the "data" directory of my application is empty.

This would be the code of my DBHelper:

public class DBHelper extends SQLiteOpenHelper{

public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){ // constructor
    super(context, name, factory, version); // conecta con la base de datos
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE 'canciones'(" +
            "id_cancion INT(10) NOT NULL AUTO_INCREMENT,"  +
            "path VARCHAR(255) NOT NULL," +
            "PRIMARY KEY ('id_cancion')" +
            ");"
    );

    db.execSQL("CREATE TABLE 'lista'(" +
            "id_cancion INT(10) NOT NULL AUTO_INCREMENT,"  +
            "path VARCHAR(255) NOT NULL," +
            "PRIMARY KEY ('id_cancion')" +
            ");"
    );
}

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

   db.execSQL("CREATE TABLE 'canciones'(" +
           "id_cancion INT(10) NOT NULL AUTO_INCREMENT,"  +
            "path VARCHAR(255) NOT NULL," +
            "PRIMARY KEY ('id_cancion')" +
            ");"
    );

    db.execSQL("CREATE TABLE 'lista'(" +
            "id_cancion INT(10) NOT NULL AUTO_INCREMENT,"  +
            "path VARCHAR(255) NOT NULL," +
            "PRIMARY KEY ('id_cancion')" +
            ");"
    );
}
}

and there is the class that DBHelper instantiates to create the database and make the corresponding insert:

public class MainActivity extends ListActivity{

private MediaCursorAdapter mediaAdapter = null;
private String currentFile = "";
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //PREPARAMOS LA BASE DE DATOS PARA PODER HACER INSERTS
    DBHelper helper = new DBHelper(this, "Songs", null, 1);
    db = helper.getWritableDatabase();

    Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

    if (null != cursor) {
        cursor.moveToFirst();
        mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
        setListAdapter(mediaAdapter);
    }


}//FIN DEL ONCREATE

private class MediaCursorAdapter extends SimpleCursorAdapter {

    public MediaCursorAdapter(Context context, int layout, Cursor c) {
        super(context, layout, c,
                new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
                new int[]{R.id.displayname, R.id.title, R.id.duration});
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView name = (TextView) view.findViewById(R.id.displayname);
        TextView duration = (TextView) view.findViewById(R.id.duration);

        ContentValues datos = new ContentValues();
        datos.put("path", cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));
        db.insert("canciones", null, datos);

        /*
        * db.execSQL("INSERT INTO 'canciones' ('path') VALUES" +
            "('"+cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME))+"');");
        * */

        name.setText(cursor.getString(
                cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));

        title.setText(cursor.getString(
                cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));

        long durationInMs = Long.parseLong(cursor.getString(
                cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));

        double durationInMin = ((double) durationInMs / 1000.0) / 60.0;

        durationInMin = new BigDecimal(Double.toString(durationInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue();

        duration.setText("" + durationInMin);

        view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.listitem, parent, false);

        bindView(v, context, cursor);
        return v;
    }
}// FIN MEDIACURSOR_ADAPTER

@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
    super.onListItemClick(list, view, position, id);

    currentFile = (String) view.getTag();

    Intent intent = new Intent(getApplicationContext(), Song.class); // fem un intent a Song
    Bundle bundle = new Bundle();
    bundle.putString("cancion_actual", currentFile);
    intent.putExtras(bundle);
    startActivity(intent);
    //finish();
}// FIN DEL ONLIST_ITEM_CLICK

}
    
asked by Charlio 30.03.2017 в 13:02
source

1 answer

0

There are several aspects to review.

At first you could do something like this:

public class DBHelper extends SQLiteOpenHelper{

// SQL statement to create the Songs and List table

 String sqlCreateCanciones = "CREATE TABLE Canciones(id_cancion INT(10) NOT NULL AUTO_INCREMENT, path VARCHAR(255) NOT NULL)";

     String sqlCreateLista = "CREATE TABLE Listas(id_lista INT(10) NOT NULL AUTO_INCREMENT, path VARCHAR(255) NOT NULL)";

// Constructor

public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){ // constructor
    super(context, name, factory, version); // conecta con la base de datos
}

// Tables are created

 @Override
        public void onCreate(SQLiteDatabase db) {
            //Se ejecuta la sentencia SQL de creación de la tabla
            db.execSQL(sqlCreateCanciones );
            db.execSQL(sqlCreateLista);
        }

P.d. If it does not work out or you have more questions, I'll gladly help you! :)

I'll leave you links with more info.

Databases on Android (I ): First steps

A very good and powerful REALM option

    
answered by 06.04.2017 в 16:24