I am unable to list my Sqlite data

Asked

Viewed 145 times

-1

Good evening, I’m creating an application for financial control, and I’m testing the database, making it show me the data to see if the event Insert is working. When you open Activity that shows the data, nothing appears. I don’t know if you have a problem with array, or at the click of Insert, I’ve reviewed the code several times and even compared it to an old project I had, and I’m not seeing what the problem is... Could someone help me?

Dbsettings:

package project.welcomemoney.Base;

//Settings Base
public class DBSettings {
    private int id;
    private String NomeEmpresa;
    private int salario;
    private int gastoFixoTotal;
    private int gastoDiversoTotal;
    private int gastoTransporteTotal;
    private int totalFinal;

    public DBSettings(){/*NULL*/}

    public DBSettings(int id, String NomeEmpresa, int salario, int gastoFixoTotal, int gastoDiversoTotal, int gastoTransporteTotal, int totalFinal){
        this.id = id;
        this.NomeEmpresa = NomeEmpresa;
        this.salario = salario;
        this.gastoFixoTotal = gastoFixoTotal;
        this.gastoDiversoTotal = gastoDiversoTotal;
        this.gastoTransporteTotal = gastoTransporteTotal;
        this.totalFinal = totalFinal;
    }

    //Settings ID
    public int getId(){return id;}
    public void setId (int id) {this.id = id;}

    //Settings NomeEmpresa
    public  String getNomeEmpresa () {return NomeEmpresa;}
    public void setNomeEmpresa (String NomeEmpresa) {this.NomeEmpresa = NomeEmpresa;}

    //Settings Salario
    public int getSalario(){return salario;}
    public void setSalario(int salario){this.salario = salario;}

    //Settings Gasto fixo total

    public int getGastoFixoTotal(){return gastoFixoTotal;}
    public void setGastoFixoTotal(int gastoFixoTotal){this.gastoFixoTotal = gastoFixoTotal;}

    //Settings Gasto Diverso Total

    public int getGastoDiversoTotal(){return gastoDiversoTotal;}
    public void setGastoDiversoTotal(int gastoDiversoTotal) {this.gastoDiversoTotal = gastoDiversoTotal;}

    //Settings Gasto Transporte Total
    public int getGastoTransporteTotal(){return gastoTransporteTotal;}
    public void setGastoTransporteTotal(int gastoTransporteTotal) {this.gastoTransporteTotal = gastoTransporteTotal;}

    //Settings Total Final
    public int getTotalFinal() {return totalFinal;}
    public void setTotalFinal (int totalFinal) {this.totalFinal = totalFinal;}

    public String toString(){
        return "Nome da empresa: "
                +NomeEmpresa + " "
                + " Salário: "
                + salario + " "
                + " Seu Gasto Fixo: "
                + gastoFixoTotal + " "
                + " Seu Gasto Diverso: "
                + gastoDiversoTotal + " "
                +" Seu Total do mês: "
                + totalFinal;
    }

}

Dbfunctioncrud:

package project.welcomemoney.Base;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBFunctionCRUD extends SQLiteOpenHelper {

    private static final String NAME_BASE = "Information";
    private static final int VERSION_BASE = 1;


    public DBFunctionCRUD(Context context) {
        super(context, NAME_BASE, null, VERSION_BASE);
    }

    @Override
    public void onCreate(SQLiteDatabase functionDB) {
        String sqlCreateTableInformation = "CREATE TABLE dados("
                + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "NameCompany TEXT,"
                + "Salario INT,"
                + "GastoFixo INT,"
                + "GastoDiverso INT,"
                + "GastoTrasnporte INT,"
                + "TotalFinal INT" + ")";

        functionDB.execSQL(sqlCreateTableInformation);
    }

    @Override
    public void onUpgrade(SQLiteDatabase functionDB, int BeforeVersion, int AfterVersion) {

        String sqlUpdateTableInformaction = "DROP TABLE dados";

        functionDB.execSQL(sqlUpdateTableInformaction);

        onCreate(functionDB);

    }


    public void insertInformation(DBSettings dados) {
        SQLiteDatabase base = getWritableDatabase();

        ContentValues insertValues = new ContentValues();
        insertValues.put("NameCompany", dados.getNomeEmpresa());
        insertValues.put("Salario", dados.getSalario());
        insertValues.put("GastoFixo", dados.getGastoFixoTotal());
        insertValues.put("GastoDiverso", dados.getGastoDiversoTotal());
        insertValues.put("GastoTransporte", dados.getGastoTransporteTotal());
        insertValues.put("TotalFinal", dados.getTotalFinal());

        base.insert("dados", null, insertValues);
        base.close();
    }

    public List<DBSettings> selectTodosResult() {
        List<DBSettings> listResult = new ArrayList<DBSettings>();
        SQLiteDatabase db = getReadableDatabase();

        String sqlSelectTodosResult = "SELECT * FROM dados";

        Cursor allC = db.rawQuery(sqlSelectTodosResult, null);

        try {

            if (allC != null && allC.moveToFirst()) {
                allC.moveToFirst();
                do {
                    DBSettings InformationAll = new DBSettings();
                    InformationAll.setId(allC.getInt(0));
                    InformationAll.setNomeEmpresa(allC.getString(allC.getColumnIndexOrThrow("Salario")));
                    InformationAll.setGastoFixoTotal(allC.getInt(allC.getColumnIndexOrThrow("GastoFixo")));
                    InformationAll.setGastoDiversoTotal(allC.getInt(allC.getColumnIndexOrThrow("GastoDiverso")));
                    InformationAll.setGastoTransporteTotal(allC.getInt(allC.getColumnIndexOrThrow("GastoTransporte")));
                    InformationAll.setTotalFinal(allC.getInt(allC.getColumnIndexOrThrow("TotalFinal")));

                    listResult.add(InformationAll);

                } while (allC.moveToNext());

            }
        }catch (android.database.SQLException or){
            or.printStackTrace();
        }
        db.close();
        return listResult;
    }


    public void Delete(){
        SQLiteDatabase deleteBase = getReadableDatabase();
        String sqlSelectAllResult = "DELETE * FROM dados";
        Cursor listC = deleteBase.rawQuery(sqlSelectAllResult, null);
    }
}

Click: onClick set in xml

public void salvarBancos (View view){
        DBSettings DBinsert = new DBSettings();
        DBinsert.setNomeEmpresa(EmpresaNome.getText().toString());
        DBinsert.setSalario(Integer.parseInt(receita.getText().toString()));
        DBinsert.setGastoFixoTotal(Integer.parseInt(gastoFixo.getText().toString()));
        DBinsert.setGastoFixoTotal(Integer.parseInt(metaConfig.getText().toString()));


        DBFunctionCRUD functionHelp = new DBFunctionCRUD(this);
        functionHelp.insertInformation(DBinsert);

        Toast.makeText(this, "Salvo com sucesso", Toast.LENGTH_LONG).show();
    }

listing in the array: I created a custom array as text1

package project.welcomemoney;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.List;

import project.welcomemoney.Base.DBFunctionCRUD;
import project.welcomemoney.Base.DBSettings;

public class MostrandoDadosTeste extends AppCompatActivity {

    ListView TestList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mostrando_dados_teste);

        TestList = (ListView) findViewById(R.id.listaTeste);
    }

    @Override
    public void onResume(){
        super.onResume();

        DBFunctionCRUD funDB = new DBFunctionCRUD(this);
        List<DBSettings> listaTesteDB = funDB.selectTodosResult();
        ArrayAdapter<DBSettings> adp = new ArrayAdapter<DBSettings>(this, R.layout.array_personalizada, android.R.id.text1, listaTesteDB);
        TestList.setAdapter(adp);
    }
}

When clicking the button see result appears in Logcat the following error:

04-24 21:00:15.849 2705-2705/project.welcomemoney E/SQLiteLog: (1) table dados has no column named GastoDiverso
04-24 21:00:15.849 2705-2705/project.welcomemoney E/SQLiteDatabase: Error inserting GastoDiverso=0 GastoTransporte=0 NameCompany=teste GastoFixo=121212 Salario=12145121 TotalFinal=0
    android.database.sqlite.SQLiteException: table dados has no column named GastoDiverso (code 1): , while compiling: INSERT INTO dados(GastoDiverso,GastoTransporte,NameCompany,GastoFixo,Salario,TotalFinal) VALUES (?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
        at project.welcomemoney.Base.DBFunctionCRUD.insertInformation(DBFunctionCRUD.java:61)
        at project.welcomemoney.setting_home.salvarBancos(setting_home.java:40)
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-24 21:00:15.904 2705-2788/project.welcomemoney V/RenderScript: 0xa1c3d000 Launching thread(s), CPUs 2
04-24 21:00:15.961 2705-2705/project.welcomemoney E/SQLiteLog: (1) table dados has no column named GastoDiverso
04-24 21:00:15.961 2705-2705/project.welcomemoney E/SQLiteDatabase: Error inserting GastoDiverso=0 GastoTransporte=0 NameCompany=teste GastoFixo=121212 Salario=12145121 TotalFinal=0
    android.database.sqlite.SQLiteException: table dados has no column named GastoDiverso (code 1): , while compiling: INSERT INTO dados(GastoDiverso,GastoTransporte,NameCompany,GastoFixo,Salario,TotalFinal) VALUES (?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
        at project.welcomemoney.Base.DBFunctionCRUD.insertInformation(DBFunctionCRUD.java:61)
        at project.welcomemoney.setting_home.salvarBancos(setting_home.java:40)
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Thank you...

2 answers

1

There is an error in query sqlCreateTableInformation. The column TotalFinal does not have a specific type.

Check and set a type for this column, if {INTEGER, TEXT, BOOLEAN, DOUBLE, ETC. }

Here’s an example of what it should look like double:

TotalFinal double

Query complete:

String sqlCreateTableInformation = "CREATE TABLE dados("
                + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "NameCompany TEXT,"
                + "Salario INT,"
                + "GastoFixo INT,"
                + "GastoDiverso INT,"
                + "GastoTrasnporte INT,"
                + "TotalFinal DOUBLE" + ")";

EDITION

This mistake:

(1) table data has no column named Gastodiverso

You mean you don’t have the spine GastoDiverso on your table. You forgot to put a comma before that column, so that’s why the error:

Change the line:

"GastoFixo INT"

for:

"GastoFixo INT,"
  • Opa... It was a mistake of mine, thanks for warning, but my problem still continues :(, it does not appear the database data in the array

  • @Nathan knows how to tell which mistake is making?

  • Is giving the following conflict : - Error inserting GastoDiverso=0 GastoTransporte=0 NameCompany=teste GastoFixo=121212 Salario=12145121 TotalFinal=0&#xA; android.database.sqlite.SQLiteException: table dados has no column named GastoDiverso (code 1): , while compiling: INSERT INTO dados(GastoDiverso,GastoTransporte,NameCompany,GastoFixo,Salario,TotalFinal) VALUES (?,?,?,?,?,?) I will add to the question the complete error

  • @Nathan found one more mistake... try again. ^^

  • It was a mistake, but it still gives problem, but now I know that it is something that is giving "failure" in the column Gastodiverso... Thank you, but if you can help me more I’m grateful colleague.

  • @Nathan and now what’s the mistake? hehee

  • So the message is the same, saying that the Gastodiverso column does not exist, so I deleted it to see if it worked and it worked, I believe it may be causing and at the time of setting it in the file setting_home because the fields that are set are being used as Company name, Gastofixo, Salario... and Gastodiverso is not being used, so perhaps the problem, because in other places the name Gastodiverso is running... Well maybe that’s it, I’m still beginner haha.

Show 2 more comments

0

The name of your column in the creation of the database has a typo:

Gastotrasnporte

And you’re trying to input data into the column that should be the right name

Gastotransporte

  • Another suggestion, try working with Content Provider. Access to data is more isolated and controlled and much less susceptible to typos like this, because if you change the structure of the database, it will have almost no impact on the application code, as you only need to touch the Content Provider Agreement class. Another advantage of working with Content Provider you can list the data in a Listview or Recyclerview using Cursorloaders to keep the list always synchronized with the database.

  • Oops, I like the tip, I’ll find out about it.

  • Check out this free course. Very good: https://br.udacity.com/course/android-basics-data-storage--ud845/

Browser other questions tagged

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