public void consulta(View v) {
GestorSQLite admin = new GestorSQLite(getApplicationContext(), "preguntas", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
int identificador=1;
Cursor fila = bd.rawQuery( "select pregunta from preguntas where identificador=" + identificador, null);
if (fila.moveToFirst()) {
pregunta.setText(fila.getString(0));
}
else
Toast.makeText(getApplicationContext(), "No hay alumno con ese numero", Toast.LENGTH_SHORT).show();
bd.close();
}
public class GestorSQLite extends SQLiteOpenHelper {
public GestorSQLite(Context context, String nombre, SQLiteDatabase.CursorFactory factory, int version) {
super(context, nombre, factory, version);
}
public Context mContext;
@Override
public void onCreate(SQLiteDatabase db) {
// Sentencias de creación de base de datos
// db.execSQL("create table preguntas( identificador integer primary key,pregunta text)");
InputStream is = null;
try {
is = mContext.getAssets().open("prueba.sql");
if (is != null) {
db.beginTransaction();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (!TextUtils.isEmpty(line)) {
db.execSQL(line);
line = reader.readLine();
}
db.setTransactionSuccessful();
}
} catch (Exception ex) {
// Muestra log
} finally {
db.endTransaction();
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Muestra log
}
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int versAnte, int versNue) {
//db.execSQL("drop table if exists lista");
//db.execSQL("create table lista(matricula integer primary key, nombre text)");
}
}
public void consulta(View v) {
GestorSQLite admin = new GestorSQLite(getApplicationContext(), "preguntas", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
int identificador=1;
Cursor fila = bd.rawQuery( "select pregunta from preguntas where identificador=" + identificador, null);
if (fila.moveToFirst()) {
pregunta.setText(fila.getString(0));
}
else
Toast.makeText(getApplicationContext(), "No hay alumno con ese numero", Toast.LENGTH_SHORT).show();
bd.close();
}
public class GestorSQLite extends SQLiteOpenHelper {
public GestorSQLite(Context context, String nombre, SQLiteDatabase.CursorFactory factory, int version) {
super(context, nombre, factory, version);
}
public Context mContext;
@Override
public void onCreate(SQLiteDatabase db) {
// Sentencias de creación de base de datos
// db.execSQL("create table preguntas( identificador integer primary key,pregunta text)");
InputStream is = null;
try {
is = mContext.getAssets().open("prueba.sql");
if (is != null) {
db.beginTransaction();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (!TextUtils.isEmpty(line)) {
db.execSQL(line);
line = reader.readLine();
}
db.setTransactionSuccessful();
}
} catch (Exception ex) {
// Muestra log
} finally {
db.endTransaction();
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Muestra log
}
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int versAnte, int versNue) {
//db.execSQL("drop table if exists lista");
//db.execSQL("create table lista(matricula integer primary key, nombre text)");
}
}