Per prima cosa si deve scrivere una classe di helper in cui sono identificate la struttura del Db e le operazioni da eseguire...per semplicita' viene utilizzata la struttura gia' vista nello script Python
-----------------------------------------------------------------------
package com.example.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class database {
SQLiteDatabase mDb;
DbHelper mDbHelper;
Context mContext;
private static final String DB_NAME="users";//nome del db
private static final int DB_VERSION=1; //numero del db
public database(Context ctx){
mContext=ctx;
mDbHelper=new DbHelper(ctx, DB_NAME, null, DB_VERSION);
}
public void open(){
mDb=mDbHelper.getWritableDatabase();
}
public void close(){
mDb.close();
}
public void insertUtente(String name,String password, String email){ // genera l'INSERT
ContentValues cv=new ContentValues();
cv.put(UsersMetaData.USERNAME_KEY, name);
cv.put(UsersMetaData.PASSWORD_KEY, password);
cv.put(UsersMetaData.EMAIL_KEY,email);
mDb.insert(UsersMetaData.USERS_TABLE, null, cv);
}
public void deleteUtente(String id) {
mDb.delete(UsersMetaData.USERS_TABLE, UsersMetaData.ID + "=" + id, null);
}
public Cursor fetchUtenti(){ //genera la SELECT
return mDb.query(UsersMetaData.USERS_TABLE, null,null,null,null,null,null);
}
public class UsersMetaData { // variabili
static final String USERS_TABLE = "users";
static final String ID = "_id";
static final String USERNAME_KEY = "username";
static final String PASSWORD_KEY = "password";
static final String EMAIL_KEY = "email";
}
private static final String USERS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " //CREATE TABLE
+ UsersMetaData.USERS_TABLE + " ("
+ UsersMetaData.ID+ " integer primary key autoincrement, "
+ UsersMetaData.USERNAME_KEY + " text not null, "
+ UsersMetaData.PASSWORD_KEY + " text not null, "
+ UsersMetaData.EMAIL_KEY + " text not null);";
private class DbHelper extends SQLiteOpenHelper { //helper
public DbHelper(Context context, String name, CursorFactory factory,int version) {
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase _db) { //solo quando il db viene creato, creiamo la tabella
_db.execSQL(USERS_TABLE_CREATE);
}
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
}
}
}
---------------------------------------------------------
a questo punto si puo' procedere utilizzando la classe helper
il codice e' sostanzialmente autoesplicativo
----------------------------------------------------------
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database utenti =new database(getApplicationContext());
utenti.open();
utenti.insertUtente("luca", "password", "lucainnoc@gmail.com");
utenti.insertUtente("chiara", "password", "chiara@gmail.com");
Cursor c=utenti.fetchUtenti(); // query
int usernameCol=c.getColumnIndex(UsersMetaData.USERNAME_KEY); //indici delle colonne
int passwordCol=c.getColumnIndex(UsersMetaData.PASSWORD_KEY);
int mailCol=c.getColumnIndex(UsersMetaData.EMAIL_KEY);
if(c.moveToFirst()){
do {
Log.d("select",c.getString(usernameCol)+" "+c.getString(passwordCol)+" "+c.getString(mailCol));
} while (c.moveToNext());//iteriamo al prossimo elemento
}
utenti.deleteUtente("1");
if(c.moveToFirst()){
do {
Log.d("select",c.getString(usernameCol)+" "+c.getString(passwordCol)+" "+c.getString(mailCol));
} while (c.moveToNext());//iteriamo al prossimo elemento
}
utenti.close();
Nessun commento:
Posta un commento