1. Are you ready for the Galaxy S20? Here is everything we know so far!

Sqlite database.

Discussion in 'Android Development' started by Negrazo1949, Mar 9, 2020.

  1. Negrazo1949

    Negrazo1949 Lurker
    Thread Starter

    Hi folks.
    I have developed a first Android app using Android Studio, now i need to manipulate the database created, but i don't know where it is located nor what extension it has. I have been searching for .db3 , .sqlite , .db to no avail.Any ideas?
    Thank you very much.
    Sincerely, Rafael.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. wseng92

    wseng92 Android Enthusiast

    Can you show us how you create database?
     
  3. Negrazo1949

    Negrazo1949 Lurker
    Thread Starter

    Hi wseng92.
    Thank you for your reply.
    This is the source that created the database:

    package com.academiaandroid.proyectodatos;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.widget.CursorAdapter;
    import android.widget.SimpleCursorAdapter;
    import java.util.ArrayList;
    import java.util.List;
    public class MyOpenHelper extends SQLiteOpenHelper {
    private static final String COMMENTS_TABLE_CREATE = "CREATE TABLE comments(_id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, comment TEXT)";
    private static final String DB_NAME = "comments.sqlite";
    private static final int DB_VERSION = 1;
    private SQLiteDatabase db;
    public MyOpenHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    db=this.getWritableDatabase();
    }
    @override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL(COMMENTS_TABLE_CREATE);
    }
    @override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
    //Insertar un nuevo comentario
    public void insertar(String nombre,String comentario){
    ContentValues cv = new ContentValues();
    cv.put("user", nombre);
    cv.put("comment", comentario);
    db.insert("comments", null, cv);
    }
    //Borrar un comentario a partir de su id
    public void borrar(int id){
    String[] args = new String[]{String.valueOf(id)};
    db.delete("comments", "_id=?", args);
    }
    //Obtener la lista de comentarios en la base de datos
    public ArrayList<Comentario> getComments(){
    //Creamos el cursor
    ArrayList<Comentario> lista=new ArrayList<Comentario>();
    Cursor c = db.rawQuery("select _id, user,comment from comments order by user", null);
    if (c != null && c.getCount()>0) {
    c.moveToFirst();
    do {
    //Asignamos el valor en nuestras variables para crear un nuevo objeto Comentario
    String user = c.getString(c.getColumnIndex("user"));
    String comment = c.getString(c.getColumnIndex("comment"));
    int id=c.getInt(c.getColumnIndex("_id"));
    Comentario com =new Comentario(id,user,comment);
    //Añadimos el comentario a la lista
    lista.add(com);
    } while (c.moveToNext());
    }
    //Cerramos el cursor
    c.close();
    return lista;
    }
    }

    This class, is called from the MainActivity:

    package com.academiaandroid.proyectodatos;
    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.CursorAdapter;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.SpinnerAdapter;
    import android.widget.Switch;
    import android.widget.TextView;
    import java.util.ArrayList;
    public class MainActivity extends Activity implements View.OnClickListener,AdapterView.OnItemSelectedListener {
    //Declaramos los elementos de la interfaz
    private Button btnVer;
    private EditText txtNombre;
    private EditText txtComentario;
    //Declaración del spinner y su Adapter
    private Spinner spinComentarios;
    private ArrayAdapter spinnerAdapter;
    //Lista de comentarios y comentario actual
    private ArrayList<Comentario> lista;
    private Comentario c;
    //Controlador de bases de datos
    private MyOpenHelper db;
    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Inicializamos los elementos de la interfaz
    txtNombre=(EditText) findViewById(R.id.txtNombre);
    txtComentario=(EditText)findViewById(R.id.txtComentario);
    //Los elementos del panel inferior no seran editables
    txtNombre.setEnabled(false);
    txtComentario.setEnabled(false);
    btnVer=(Button)findViewById(R.id.btnVer);
    btnVer.setOnClickListener(this);
    //Iniciamos el controlador de la base de datos
    db=new MyOpenHelper(this);
    //Iniciamos el spinner y la lista de comentarios
    spinComentarios=(Spinner) findViewById(R.id.spinComentarios);
    lista=db.getComments();
    //Creamos el adapter y lo asociamos al spinner
    spinnerAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,lista);
    spinComentarios.setAdapter(spinnerAdapter);
    spinComentarios.setOnItemSelectedListener(this);
    }
    @override
    public void onClick(View v) {
    //Acciones de cada boton
    switch(v.getId()){
    case R.id.btnVer:
    //Si hay algun comentario seleccionado mostramos sus valores en la parte inferior
    if(c!=null) {
    txtNombre.setText(c.getNombre());
    txtComentario.setText(c.getComentario());
    }
    break;
    }
    }
    //Controlador de elemento seleccionado en el spinner
    @override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (parent.getId() == R.id.spinComentarios) {
    //Si hay elementos en la base de datos, establecemos el comentario actual a partir del
    //indice del elemento seleccionado en el spinner
    if(lista.size()>0) {
    c = lista.get(position);
    }
    }
    }
    @override
    public void onNothingSelected(AdapterView<?> parent) {
    }
    }

    So far, all is working fine, but i need to merge data between 2 databases with the same structure and cannot find them.
    I wish you could guide me.

    Thanks a lot.
    Rafael
     
  4. How to see if someone is spying on my through my phone
     
Loading...
Similar Threads - Sqlite database
  1. Castino
    Replies:
    1
    Views:
    360
  2. werner111
    Replies:
    6
    Views:
    1,783
  3. rowlanda38
    Replies:
    0
    Views:
    483
  4. iliji
    Replies:
    1
    Views:
    1,551
  5. dplusk
    Replies:
    0
    Views:
    1,054
  6. selva_0707
    Replies:
    0
    Views:
    1,426
  7. manjula1
    Replies:
    0
    Views:
    723
  8. GabeTheGreat73
    Replies:
    0
    Views:
    577
  9. Dilip Birajadar
    Replies:
    4
    Views:
    887
  10. Praful Wadkar
    Replies:
    1
    Views:
    499

Share This Page

Loading...