0

我的数据库中的一个表有问题。问题是 MOVEMENTS 表:每当我插入东西时它都可以正常工作,但是当我尝试检索运动时,我的应用程序崩溃了。

我不知道我做错了什么,它与 CLIENTS 表基本完全相同

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

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

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "ClientsManager";

    private static final String CLIENTS_TABLE = "Clients";
    private static final String MOVEMENTS_TABLE = "Movements";

    //Client Table Columns
    private static final String KEY_ROWID = "numint";
    private static final String KEY_NAME = "name";
    private static final String KEY_LOCAL = "locality";
    private static final String KEY_ADDR = "address";
    private static final String KEY_BTTLP = "bottle_price";
    private static final String KEY_PBLNC = "prev_balance";
    private static final String KEY_BLNC = "balance";
    private static final String KEY_BBTTLS = "bonif_bottles";
    private static final String KEY_PBTTLS = "prev_bottles";
    private static final String KEY_BTTLS = "bottles";
    private static final String KEY_SYNC = "sync_date";
    private static final String KEY_MODIF = "modif_date";

    //Movements Table Columns
    private static final String MKEY_ROWID = "numint";
    private static final String MKEY_ID_MOVE = "move_id";
    private static final String MKEY_CLIENT_ID = "client_id";
    private static final String MKEY_PICKUP = "pickup";
    private static final String MKEY_DELIVER = "deliver";
    private static final String MKEY_PAYMENT = "payment";
    private static final String MKEY_MOVE_DATE = "move_date";
    private static final String MKEY_SYNC_DATE = "sync_date";
    private static final String MKEY_FLAG_SYNC = "flag_sync";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_CLIENTS_TABLE = "CREATE TABLE " + CLIENTS_TABLE + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
                + KEY_NAME + " TEXT NOT NULL, "
                + KEY_LOCAL + " TEXT NOT NULL, "
                + KEY_ADDR + " TEXT NOT NULL, "
                + KEY_BTTLP + " REAL NOT NULL, "
                + KEY_PBLNC + " REAL NOT NULL, "
                + KEY_BLNC + " REAL NOT NULL, "
                + KEY_BBTTLS + " INTEGER NOT NULL, "
                + KEY_PBTTLS + " INTEGER NOT NULL, "
                + KEY_BTTLS + " INTEGER NOT NULL, "
                + KEY_SYNC + " TEXT NOT NULL, "
                + KEY_MODIF + " TEXT NOT NULL )";

        String CREATE_MOVEMENTS_TABLE = "CREATE TABLE " + MOVEMENTS_TABLE + "("
                + MKEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
                + MKEY_ID_MOVE + " TEXT NOT NULL, "
                + MKEY_CLIENT_ID + " INTEGER NOT NULL, "
                + MKEY_PICKUP + " INTEGER NOT NULL, "
                + MKEY_DELIVER + " INTEGER NOT NULL, "
                + MKEY_PAYMENT + " REAL NOT NULL, "
                + MKEY_MOVE_DATE + " TEXT NOT NULL, "
                + MKEY_SYNC_DATE + " TEXT NOT NULL, "
                + MKEY_FLAG_SYNC + " INTEGER NOT NULL )";

        sqLiteDatabase.execSQL(CREATE_CLIENTS_TABLE);
        sqLiteDatabase.execSQL(CREATE_MOVEMENTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + CLIENTS_TABLE);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + MOVEMENTS_TABLE);
        onCreate(sqLiteDatabase);
    }

    public void addClient(Client client) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_NAME, client.getName());
        values.put(KEY_LOCAL, client.getLocality());
        values.put(KEY_ADDR, client.getAddress());
        values.put(KEY_BTTLP, client.getBottlePrice());
        values.put(KEY_PBLNC, client.getPrevBalance());
        values.put(KEY_BLNC, client.getBalance());
        values.put(KEY_BBTTLS, client.getBonifBottles());
        values.put(KEY_PBTTLS, client.getPrevBottles());
        values.put(KEY_BTTLS, client.getBottles());
        values.put(KEY_SYNC, client.getSyncDate());
        values.put(KEY_MODIF, client.getModifDate());

        //Insert row
        db.insert(CLIENTS_TABLE, null, values);
        db.close();
    }

    public Client getClient(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(CLIENTS_TABLE, new String[] { KEY_ROWID, KEY_NAME, KEY_LOCAL,
                KEY_ADDR, KEY_BTTLP, KEY_PBLNC, KEY_BLNC, KEY_BBTTLS, KEY_PBTTLS, KEY_BTTLS, KEY_SYNC, KEY_MODIF}, KEY_ROWID + "=?",
                new String[] {String.valueOf(id)}, null, null, null, null);

        if(cursor != null) {
            cursor.moveToFirst();
        }

        Client client = new Client(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),
                cursor.getString(3), Float.parseFloat(cursor.getString(4)), Float.parseFloat(cursor.getString(5)),
                Float.parseFloat(cursor.getString(6)), Integer.parseInt(cursor.getString(7)), Integer.parseInt(cursor.getString(8)),
                Integer.parseInt(cursor.getString(9)), cursor.getString(10), cursor.getString(11));

        return client;
    }

    public List<Client> getAllClients() {
        List<Client> clientList = new ArrayList<Client>();

        String selectQuery = "SELECT * FROM " + CLIENTS_TABLE;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()) {
            do {

                Client client = new Client(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),
                        cursor.getString(3), Float.parseFloat(cursor.getString(4)), Float.parseFloat(cursor.getString(5)),
                        Float.parseFloat(cursor.getString(6)), Integer.parseInt(cursor.getString(7)), Integer.parseInt(cursor.getString(8)),
                        Integer.parseInt(cursor.getString(9)), cursor.getString(10), cursor.getString(11));

                clientList.add(client);

            }while(cursor.moveToNext());
        }

        return clientList;
    }

    public int getClientCount() {
        String countQuery = "SELECT * FROM " + CLIENTS_TABLE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        return cursor.getCount();
    }

    public int updateClient(Client client) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_PBLNC, client.getPrevBalance());
        values.put(KEY_BLNC, client.getBalance());
        values.put(KEY_PBTTLS, client.getPrevBottles());
        values.put(KEY_BTTLS, client.getBottles());
        values.put(KEY_MODIF, client.getModifDate());

        return db.update(CLIENTS_TABLE, values, KEY_ROWID + "=?",
                new String[] {String.valueOf(client.getId())});
    }

    public int deleteClient(Client client) {
        int count = 0;
        SQLiteDatabase db = this.getWritableDatabase();
        count = db.delete(CLIENTS_TABLE, KEY_ROWID + "=?", new String[] {String.valueOf(client.getId())});
        db.close();

        return count;
    }

    public int deleteAllClients() {
        List<Client> clientList = getAllClients();
        SQLiteDatabase db = this.getWritableDatabase();
        int count = 0;
        for(int i = 0; i < clientList.size(); i++) {
            count += db.delete(CLIENTS_TABLE, KEY_ROWID + "=?", new String[] {String.valueOf(clientList.get(i).getId())});
        }

        return  count;
    }

    public void addMovement(Movement movement) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(MKEY_ID_MOVE, movement.getMoveId());
        values.put(MKEY_CLIENT_ID, movement.getClientId());
        values.put(MKEY_PICKUP, movement.getPickUp());
        values.put(MKEY_DELIVER, movement.getDeliver());
        values.put(MKEY_PAYMENT, movement.getPayment());
        values.put(MKEY_MOVE_DATE, movement.getMoveDate());
        values.put(MKEY_SYNC_DATE, movement.getSyncDate());
        values.put(MKEY_FLAG_SYNC, movement.getFlagSync());

        db.insert(MOVEMENTS_TABLE, null, values);
        db.close();
    }

    public Movement getMovement(int clientId) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(MOVEMENTS_TABLE, new String[] {MKEY_ROWID, MKEY_ID_MOVE, MKEY_CLIENT_ID,
        MKEY_PICKUP, MKEY_DELIVER, MKEY_PAYMENT, MKEY_MOVE_DATE, MKEY_SYNC_DATE, MKEY_SYNC_DATE}, MKEY_CLIENT_ID + "=?",
                 new String[] {String.valueOf(clientId)}, null, null, null, null);

        Movement movement = null;

        if(cursor != null) {
            if(cursor.moveToFirst()) {
                movement = new Movement(Integer.parseInt(cursor.getString(0)), cursor.getString(1), Integer.parseInt(cursor.getString(2)),
                        Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Float.parseFloat(cursor.getString(5)),
                        cursor.getString(6), cursor.getString(7), Integer.parseInt(cursor.getString(8)));
            }
        }

        return movement;
    }

    public List<Movement> getAllMovements() {
        List<Movement> movementList = new ArrayList<Movement>();

        String query = "SELECT * FROM " + MOVEMENTS_TABLE;

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(query, null);

        if(cursor.moveToFirst()) {
            do {

                Movement movement = new Movement(Integer.parseInt(cursor.getString(0)), cursor.getString(1), Integer.parseInt(cursor.getString(2)),
                        Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Float.parseFloat(cursor.getString(5)),
                        cursor.getString(6), cursor.getString(7), Integer.parseInt(cursor.getString(8)));

                movementList.add(movement);

            }while (cursor.moveToNext());
        }

        return movementList;
    }

    public int deleteAllMovements() {
        SQLiteDatabase db = this.getWritableDatabase();
        List<Movement> movementList = getAllMovements();

        int count = 0;

        for(int i = 0; i < movementList.size(); i++) {
            count += db.delete(MOVEMENTS_TABLE, MKEY_ROWID + "=?", new String[] {String.valueOf(movementList.get(i).getId())});
        }

        return count;
    }

    public int updateMovement(Movement movement) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(MKEY_PICKUP, movement.getPickUp());
        values.put(MKEY_DELIVER, movement.getDeliver());
        values.put(MKEY_PAYMENT, movement.getPayment());
        values.put(MKEY_MOVE_DATE, movement.getMoveDate());

        return db.update(MOVEMENTS_TABLE, values, MKEY_ROWID + "=?", new String[] {String.valueOf(movement.getId())});

    }

}
4

1 回答 1

0

我通过使用 Cursor 方法解决了这个问题:getInt() 和 getFloat() 而不是解析 getString() 的结果

现在该应用程序工作正常

于 2013-08-10T22:13:14.307 回答