Ciao a tutti,
Premetto che ho letto il regolamento ma non sono tanto sicuro di essere nella sezione giusta dato l'argomento.
Devo realizzare un database SQLite per un applicazione Android, seguendo la vostra guida ho realizzato le classi DatadaseHelper e DbAdapter, che ho messo nello stesso package e che vedete di seguito:
codice:
package com.android.forrestrunner.tuttodb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Marcello on 18/01/2015.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "forrest_db.db";
private static final int DATABASE_VERSION = 1;
// Lo statement SQL di creazione del database
private static final String DATABASE_CREATE = "create table allenamenti (_id integer primary key autoincrement, stringaAllenamento text not null);";
// Costruttore
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Questo metodo viene chiamato durante la creazione del database
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
// Questo metodo viene chiamato durante l'upgrade del database, ad esempio quando viene incrementato il numero di versione
@Override
public void onUpgrade( SQLiteDatabase database, int oldVersion, int newVersion ) {
database.execSQL("DROP TABLE IF EXISTS forrest_db");
onCreate(database);
}
}
codice:
package com.android.forrestrunner.tuttodb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
/**
* Created by Marcello on 19/01/2015.
*/
public class DbAdapter {
@SuppressWarnings("unused")
private static final String LOG_TAG = DbAdapter.class.getSimpleName();
private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
// Database fields
private static final String DATABASE_TABLE = "forrest_db";
public static final String ID_ALLENAMENTO = "_id";
public static final String ALLENAMENTO = "name";
public DbAdapter(Context context) {
this.context = context;
}
public DbAdapter open() throws SQLException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
private ContentValues createContentValues(String stringaAllenamento) {
ContentValues values = new ContentValues();
values.put( ALLENAMENTO, stringaAllenamento );
return values;
}
//crea un allenamento
public long creaAllenamento(String stringaAllenamento ) {
ContentValues initialValues = createContentValues(stringaAllenamento);
return database.insertOrThrow(DATABASE_TABLE, null, initialValues);
}
//aggiorna allenamento
public boolean updateAllenamento( long IDallenamento, String stringaAllenamento ) {
ContentValues updateValues = createContentValues(stringaAllenamento);
return database.update(DATABASE_TABLE, updateValues, ID_ALLENAMENTO + "=" + IDallenamento, null) > 0;
}
//elimina allenamento
public boolean deleteAllenamento(long IDallenamento) {
return database.delete(DATABASE_TABLE, ID_ALLENAMENTO + "=" + IDallenamento, null) > 0;
}
//fetch degli allenamenti (non dovrebbe servire)
public Cursor fetchAllAllenamenti() {
return database.query(DATABASE_TABLE, new String[] { ID_ALLENAMENTO, ALLENAMENTO}, null, null, null, null, null);
}
//fetch degli allenamenti mediante filtro
public Cursor fetchAllenamentiByFilter(String filter) {
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
ID_ALLENAMENTO, ALLENAMENTO },
ALLENAMENTO + " like '%"+ filter + "%'", null, null, null, null, null);
return mCursor;
}
}
Ora mi sapreste spiegare come procedere per creare il database ed accedervi?
Ho provato a scrivere il seguente codice nella classe Main dell'applicazione ma non mi vengono riconosciuti i metodi di entrambe le classi 
codice:
public class testDb {
private SQLiteDatabase database;
private DatabaseHelper db;
db.onUpgrade(db);
public long createAllenamento(String stringaAllenamento ) {
ContentValues initialValues = createContentValues(stringaAllenamento);
return database.insertOrThrow(DATABASE_TABLE, null, initialValues);
}
Ringrazio in anticipo chiunque mi dia una mano!!