I am not able to list my SQLite data [closed]

-1

Good evening, I'm creating an application for financial control, and I'm testing the database, causing it to show me the data so I can see if the insert event is working. When you open the activity that shows the data, nothing appears. I do not know if it has a problem with the array , or the insert click, I've reviewed the code several times and even compared it to an old project I had, and I can not see what's the problem ... could anyone 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 the insert: 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 like 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 on the button see result the following error appears in Logcat:

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 ...

    
asked by anonymous 24.04.2017 / 20:29

2 answers

1

There is an error in query sqlCreateTableInformation . The TotalFinal column 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 to expect if it is of type double :

TotalFinal double

Query :

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

EDITION

This error:

  

(1) table given has no column named SpentDiverse

You do not have the GastoDiverso column in your table. You forgot to put a comma before that column, so why the error:

Change the line:

"GastoFixo INT"

To:

"GastoFixo INT,"
    
24.04.2017 / 22:02
0

Your column name in database creation is typed:

SpendTrasnporte

And you're trying to enter data into the column that should be the right name

GasTransport

    
24.04.2017 / 23:08