数据库中保存的数据格式不正确

时间:2017-12-15 21:28:17

标签: android database sqlite

我必须创建联系人应用。这是我第一个使用数据库连接的应用程序。 阅读并尝试了很多后,我成功阅读并保存,但数据保存不正确。

我的数据库视图:  android_db_view

如您所见,这些字段看起来像是android文本视图描述。

我的MainActivity类:

package com.example.joaoliveira.formdb;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.List;

public class MainActivity extends AppCompatActivity
{
    private DBHelper dbh;
    EditText etName, etAdress, etFirm;
    Button btnSave, btnDisplay;

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

        this.dbh = new DBHelper( this );
        etName = findViewById( R.id.etName );
        etAdress = findViewById( R.id.etAdress );
        etFirm = findViewById( R.id.etFirm );

        btnSave = findViewById( R.id.btnSave );
        btnDisplay = findViewById( R.id.btnDisplay );

        btnSave.setOnClickListener( new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                if( etName.length() > 0 && etAdress.length() > 0 && etFirm.length() > 0 ){
                    dbh.insert( etName.toString(), etAdress.toString(), etFirm.toString() );
                    AlertDialog.Builder adb = new AlertDialog.Builder( MainActivity.this );
                    adb.setTitle( "Success" );
                    adb.setMessage( "Accomplished register!" );
                    adb.show();

                    etName.setText("");
                    etAdress.setText("");
                    etFirm.setText("");
                }

                else{
                    AlertDialog.Builder adb = new AlertDialog.Builder( MainActivity.this );
                    adb.setTitle( "You have failed!" );
                    adb.setMessage( "All the fields must be filled" );
                    adb.show();

                    etName.setText("");
                    etAdress.setText("");
                    etFirm.setText("");
                }
            }
        });

        btnDisplay.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                List<Contact> contacts = dbh.queryGetAll();
                AlertDialog.Builder adb = new AlertDialog.Builder( MainActivity.this );

                try {
                    for (int i = 0; i < contacts.size(); i++) {
                        Contact c = contacts.get(i);

                        adb.setTitle("Contact " + (i+1) );
                        adb.setMessage( c.toString() );
                        adb.show();

                        adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface di, int which) {
                                di.dismiss();
                            }
                        });
                    }
                }
                catch ( Exception e ){
                    adb.setTitle("Oops!");
                    adb.setMessage("There are no saved contacts to display.");
                    adb.show();
                }
            }
        });

        //dbh.deleteAll();
    }
}

我的DBHelper课程:

package com.example.joaoliveira.formdb;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

import java.util.ArrayList;
import java.util.List;

public class DBHelper
{
    private static final String DATABASE_NAME = "android_db";
    private static final String TABLE_NAME = "Contact";
    private static final int DATABASE_VERSION = 1;

    private Context context;
    private SQLiteDatabase db;

    private SQLiteStatement insertStmt;
    private static final String INSERT = "insert into " + TABLE_NAME + "(name, adress, firm) values (?, ?, ?)";

    public DBHelper ( Context context )
    {
        this.context = context;
        OpenHelper openHelper = new OpenHelper( this.context );
        this.db = openHelper.getWritableDatabase();
        this.insertStmt = this.db.compileStatement( INSERT );
    }

    public long insert( String name, String adress, String firm )
    {
        this.insertStmt.bindString( 1, name );
        this.insertStmt.bindString( 2, adress );
        this.insertStmt.bindString( 3, firm );

        return this.insertStmt.executeInsert();
    }

    public void deleteAll()
    {
        this.db.delete( TABLE_NAME, null, null );
    }

    public List<Contact> queryGetAll()
    {
        List<Contact> list = new ArrayList<>();

        try {
            Cursor cursor = this.db.query( TABLE_NAME, new String[]{"name", "adress", "firm"}, null, null, null, null, null, null );
            int nRecords = cursor.getCount(); //getCursor

            if( nRecords != 0 ) {
                cursor.moveToFirst();
                do {
                    Contact contact = new Contact(cursor.getString(0), cursor.getString(1), cursor.getString(2));
                    list.add(contact);
                } while (cursor.moveToNext());

                if( cursor != null && !cursor.isClosed() ){
                    cursor.close();
                    return list;
                }
            }

            else
                return null;
        }

        catch( Exception e ) {
            return null;
        }

        return list;
    }

    private static class OpenHelper extends SQLiteOpenHelper
    {
        OpenHelper( Context context )
        {
            super( context, DATABASE_NAME, null, DATABASE_VERSION );
        }

        public void onCreate( SQLiteDatabase db )
        {
            String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, adress TEXT, firm TEXT);";
            db.execSQL( sql );
        }

        public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion )
        {
            db.execSQL( "DROP TABLE IF EXISTS " + TABLE_NAME );
            onCreate( db );
        }
    }
}

联系课程以确保您能够看到我的所有代码

package com.example.joaoliveira.formdb;

public class Contact
{
    private String name;
    private String adress;
    private String firm;

    public Contact(String name, String adress, String firm )
    {
        this.name = name;
        this.adress = adress;
        this.firm = firm;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public void setFirm(String firm) {
        this.firm = firm;
    }

    public String getName() {

        return name;
    }

    public String getAdress() {
        return adress;
    }

    public String getFirm() {
        return firm;
    }

    @Override
    public String toString() {
        return "Name: " + name +
                "Adress: " + adress +
                "Firm: " + firm;
    }

}

感谢您的关注!

1 个答案:

答案 0 :(得分:0)

您正在按照toString方法存储Views对象,而不是Views(EditTexts)的内容/值。您需要使用EditText的getText()方法来检索值,然后将其转换为String。所以

而不是dbh.insert( etName.toString(), etAdress.toString(), etFirm.toString() );

您应该使用: -

dbh.insert( etName.getText().toString(), 
    etAdress.getText().toString(), 
    etFirm.getText().toString()
);