Error while performing an SQLITE table creation

Asked

Viewed 385 times

0

I’m trying the following mistake:

android.database.sqlite.SQLiteException: no such column: np (code 1): , while compiling: SELECT _id, nome, email, np, tipoFunc FROM usuarios ORDER BY nome ASC
                                                                                                   at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                                   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                                   at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                                   at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                                   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                                   at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                                   at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                                                   at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                                                                                                   at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                                                                                                   at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                                                                                                   at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                                                                                                   at com.example.gerdaumanagement.gerdaumanagement.db_funcao.buscar(db_funcao.java:61)
                                                                                                   at com.example.gerdaumanagement.gerdaumanagement.usuario.onCreateView(usuario.java:40)
                                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                                   at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                                   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

And table creation code is:

  @Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table usuarios(_id integer primary key autoincrement, nome text not null, email text not null, np integer not null, tipoFunc text not null);");
}

And here I am running a function to fetch the data entered in the table.

public List<usuarioData> buscar(){
    List<usuarioData> list = new ArrayList<>();
    String[] colunas = new String[]{"_id","nome","email!","np","tipoFunc"};

    Cursor cursor = db.query("usuarios", colunas, null, null, null, null, "nome ASC");

    if(cursor.getCount() > 0){
        cursor.moveToFirst();

        do{

            usuarioData u = new usuarioData();

            u.setId(cursor.getInt(0));
            u.setNome(cursor.getString(1));
            u.setEmail(cursor.getString(2));
            u.setNp(cursor.getString(3));
            u.setTipoFunc(cursor.getString(4));


            list.add(u);

        }while(cursor.moveToNext());
    }

    return(list);
}

Does anyone know how to fix this mistake?

  • Have you tried to uninstall and install the app again or change the version of the database? Qdo vc adds a new column in the database, it is necessary

  • THANK YOU @Leonardodias

1 answer

2


I believe it must be because you included a "!" in the column name on that row:

String[] colunas = new String[]{"_id","nome","email!","np","tipoFunc"};

Since you are searching for data from all columns, this row is not even necessary. You can call the query directly this way:

Cursor cursor = db.query("usuarios", null, null, null, null, null, "nome ASC");
  • Thank you very much. So this String columns I would use only if I wanted to see some columns and not all?

  • Yes. To not have to bring data from the base unnecessarily, if you will not use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.