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

Sqlite database doesnt run...need help

Discussion in 'Android Development' started by javiete, Aug 1, 2017.

  1. javiete

    javiete Lurker
    Thread Starter

    Hi everybody!

    I have made a app and I am using a Sqlite app. At first it runs properly but now when I am programming some issues in the app and I have to use a sqlite instruction the app breaks. For this reason I have returned to the first steps with a sqlite database, however the app continues breaking .

    I have check the code so many times but i dont know what happend...i cant undestand nothing

    ***DATABASE CODE***


    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;
    import android.widget.Toast;

    public class accesobd extends SQLiteOpenHelper {

    public accesobd(Context context, String name, CursorFactory factory,
    int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
    }

    @override
    public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE PERSONAS (_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
    + "dni INTEGER, nie INTEGER, nombre TEXT, apellido text, ano INTEGER);"
    );

    db.execSQL("CREATE TABLE PERSONAS1 (_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
    + "registro INTEGER, nregistros INTEGER, dnin INTEGER, nombre TEXT, apellido text, fecha INTEGER, nacionalidad TEXT, domicilio TEXT, estado TEXT, ordinal INTEGER);"
    );

    }


    @override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // db.execSQL("DROP TABLE IF EXISTS PERSONA");

    }
    public boolean insertarRegistro (String dni, String nie, String nombre, String apellido, String ano) {
    SQLiteDatabase sql = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("dni", dni);
    cv.put("nie", nie);
    cv.put("nombre", nombre);
    cv.put("apellido", apellido);
    cv.put("ano", ano);
    if (sql.insert("PERSONAS", null, cv) != -1) return true;
    else return false;
    }


    public String [] leerRegistro (String dni) {
    SQLiteDatabase sql = this.getReadableDatabase();
    Cursor c = sql.query(true, "PERSONAS", new String[] {"nombre", "apellido" ,"ano"},"dni='"+dni+"'",null,null,null,null,null);
    String [] aux = {"0", "0" ,"0"};


    if (c.moveToFirst()) {
    //Recorremos el cursor hasta que no haya más registros
    do {
    aux[0]= c.getString(0); //nombre
    aux[1] = c.getString(1); //apellido
    aux[2]= c.getString(2); //ano
    } while(c.moveToNext());

    }
    return aux;

    }


    public boolean insertarRegistrobd1 (String registro, String nregistro, String dnin, String nombre, String apellido, String fecha,


    String nacional, String domicilio, String estado, String ordinal) {
    SQLiteDatabase sql = this.getWritableDatabase();
    ContentValues cv = new ContentValues();


    cv.put("registro", registro);
    cv.put("nregistro", nregistro);
    cv.put("dnin", dnin);
    cv.put("nombre", nombre);
    cv.put("apellido", apellido);
    cv.put("fecha", fecha);
    cv.put("nacional", nacional);
    cv.put("domicilio", domicilio);
    cv.put("estado", estado);
    cv.put("ordinal", ordinal);

    if (sql.insert("PERSONAS1", null, cv) != -1) return true;
    else return false;
    }




    public String [] leerRegistro1bd1 (String nombre) {
    SQLiteDatabase sql = this.getReadableDatabase();
    Cursor c = sql.query(true, "PERSONAS1", new String[] {"registro","nregistro", "apellido" ,
    "fecha", "nacional", "domicilio", "estado", "ordinal, dnin"},
    "nombre='"+nombre+"'",null,null,null,null,null);


    String [] aux = {"0", "0" ,"0","0", "0" ,"0","0", "0", "0" };

    StringBuilder aux = new StringBuilder();
    if (c.moveToFirst()) {

    do {
    aux[0]= c.getString(0); //registro
    aux[1] = c.getString(1); //nregistro
    aux[2]= c.getString(3); //apellido
    aux[3] = c.getString(4); //fecha
    aux[4]= c.getString(5); //nacional
    aux[5]= c.getString(6); //domicilio
    aux[6] = c.getString(7); //estado
    aux[7]= c.getString(8); //ordinal
    aux[8]= c.getString(8); //dnin

    } while(c.moveToNext());

    return aux;
    }


    ///MAIN ACTIVITY
    public class Consulta1 extends Activity implements OnClickListener {
    public accesobd bd;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.consulta1);
    bd = new accesobd(this, "BBDD_clase", null, 1);

    boolean resultado = bd.insertarRegistro("0", "0","0","0","0");
    resultado = bd.insertarRegistrobd1("0", "0","0","0","0", "0", "0","0","0","0");

    }

    The most strange thing is that the app breaks (I have tested it in debug mode) only with the instruction bd.insertarRegistrobd1, and with the instruction bd.insertarRegistro it runs properly.

    Thanks a lot

     


  2. Best Answer:
    Post #5 by Deleted User, Aug 2, 2017 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

    Please explain how the "app breaks".
    If it crashed, then please include the stack trace from the Logcat output.
     
  4. javiete

    javiete Lurker
    Thread Starter

    Hi LV426,

    This is the trace from the Logacat output

    08-01 23:34:01.760: I/Database(362): sqlite returned: error code = 1, msg = table PERSONAS1 has no column named nregistro
    08-01 23:34:01.820: E/Database(362): Error inserting apellido=0 nombre=0 nregistro=0 estado=0 fecha=0 domicilio=0 registro=0 ordinal=0 dnin=0 nacional=0
    08-01 23:34:01.820: E/Database(362): android.database.sqlite.SQLiteException: table PERSONAS1 has no column named nregistro: , while compiling: INSERT INTO PERSONAS1(apellido, nombre, nregistro, estado, fecha, domicilio, registro, ordinal, dnin, nacional) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
    08-01 23:34:01.820: E/Database(362): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
    08-01 23:34:01.820: E/Database(362): at es.masterd.e07_1.accesobd.insertarRegistrobd1(accesobd.java:149)

    Thanks
     
  5. javiete

    javiete Lurker
    Thread Starter

    log file
     

    Attached Files:

    • log.txt
      File size:
      1.6 KB
      Views:
      174
  6. Deleted User

    Deleted User Guest

    Your database table doesn't have a column you used in your query. The error could not be any clearer -

    Code (Text):
    1.  
    2. sqlite returned: error code = 1, msg = table PERSONAS1 has no column named nregistro
    3.  
    In fact the column name is "nregistros", as you used in the table creation statement below.

    Code (Text):
    1.  
    2. db.execSQL("CREATE TABLE PERSONAS1 (_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
    3. + "registro INTEGER, nregistros INTEGER, dnin INTEGER, nombre TEXT, apellido text, fecha INTEGER, nacionalidad TEXT, domicilio TEXT, estado TEXT, ordinal INTEGER);"
    4.  
     
  7. javiete

    javiete Lurker
    Thread Starter

    This was exactly the error and i didn't realise what happened... ( i could check the code hundreds times)

    Thanks!!
     
    sweetndreemy73 likes this.
Loading...

Share This Page

Loading...