BDs

Bases de datos
-
Información guardada en tablas
-
Columnas, instancias de objetos ->filas
-
SQL
-
SQLite Database
-
.query(args)
-
.rawQuery(consulta SQL, null)
SQL
//Crear:
CREATE TABLE intents (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, number INTEGER)
//Seleccionar:
SELECT id _id, user, number FROM intents ORDER BY number DESC
//Insertar:
INSERT INTO intents (user, number) VALUES (’usuario1’, ‘5’)
//Eliminar:
DELETE FROM intents WHERE user=’user1’
SQLite Open Helper
public class IntentsOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "project";
private static final String STATISTICS_TABLE_NAME = "intents";
private static final String STATISTICS_TABLE_CREATE =
"CREATE TABLE " + STATISTICS_TABLE_NAME +
" (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, number INTEGER)";
IntentsOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(STATISTICS_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + STATISTICS_TABLE_NAME);
db.execSQL(STATISTICS_TABLE_CREATE);
}
}
Uso del Open Helper
IntentsOpenHelper ioh = new IntentsOpenHelper(this);
SQLiteDatabase db = ioh.getReadableDatabase();
SQLiteDatabase db = ioh.getWritableDatabase();
if (db != null) {
db.execSQL("INSERT INTO ...", null);
db.execSQL("DELETE FROM ...", null);
cursor = db.rawQuery("SELECT id ...", null);
db.close();
}
DB
By marcos_perez
DB
- 806