How to make inserts in table creation in SQLite?

1

I would like to know how to do INSERT manuals in the DatabaseHelper class that extends SQLiteOpenHelper.

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String BANCO_DADOS = "Agenda";
private static int VERSAO = 1;


public DatabaseHelper(Context context) {
    super(context, BANCO_DADOS, null, VERSAO);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL( "CREATE TABLE amigo (_id INTEGER PRIMARY KEY," +
                " nome TEXT, telefone TEXT, " +
                " email TEXT, categoria INTEGER);"
    );
}

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

}

}

What I wanted was to do INSERTs within the onCreate method of the DatabaseHelper class just as the table was created, but I do not know how to do it.

    
asked by anonymous 20.04.2017 / 17:32

1 answer

0

You can do the INSERT in the same way you did with CREATE by passing the SQL command to the execSQL function.

It is not recommended to do SQL queries directly from the application. The ideal is to work with Content Providers that act as an abstraction layer between the database and the application, leaving the database more isolated and easier to implement evolutions or data sharing with other applications. >

In addition, if you use lists (RecyclerView) to display data using a Loader to manage it, use of Content Provider is a must.

More details on Content Providers here: link

    
20.04.2017 / 17:45