无法从CursorWindow读取第1行col -1。在从中访问数据之前,请确保正确初始化Cursor

时间:2017-03-09 15:22:38

标签: android sqlite

如果网络可用,我正在尝试更新数据。从SQLite数据库中读取数据时出现问题。 它显示 readFromLocalDatabase 方法 DbHelper1.java 中光标初始化的问题

NetworkMonitor.java

public class NetworkMonitor1 extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {

    if(checkNetworkConnection(context)){
        DbHelper1.getInstance(context);
        final SQLiteDatabase database = DbHelper1.getInstance(context).getWritableDatabase();

        Cursor cursor=DbHelper1.getInstance(context).readFromLocalDatabase(database);

        while (cursor.moveToNext()){
            int sync_status = cursor.getInt(cursor.getColumnIndex(DbContact.SYNC_STATUS_1));
            if(sync_status == DbContact.SYNC_STATUS_FAILED_1){
                final String Vehicle = cursor.getString(cursor.getColumnIndex(DbContact.VEHICLE_NO));
                final String Exp_Date = cursor.getString(cursor.getColumnIndex(DbContact.EXP_DATE));
                final String Society_Name  = cursor.getString(cursor.getColumnIndex(DbContact.SOCIETY_NAME));
                final String Spl_Qty = cursor.getString(cursor.getColumnIndex(DbContact.SPL_QTY));
                final String Transport_Name = cursor.getString(cursor.getColumnIndex(DbContact.TRANSPORT_NAME));
                final String Transport_Vehicle_No = cursor.getString(cursor.getColumnIndex(DbContact.TRANSPORT_VEHICLE_NO));
                final String Hsd = cursor.getString(cursor.getColumnIndex(DbContact.HSD));
                final String Toll_Tax = cursor.getString(cursor.getColumnIndex(DbContact.TOLL_TAX));
                final String Food = cursor.getString(cursor.getColumnIndex(DbContact.FOOD));
                final String P_Name = cursor.getString(cursor.getColumnIndex(DbContact.P_NAME));
                final String P_Expenses = cursor.getString(cursor.getColumnIndex(DbContact.P_EXPENSES));
                final String Reading_Open = cursor.getString(cursor.getColumnIndex(DbContact.READING_OPEN));
                final String Reading_Close = cursor.getString(cursor.getColumnIndex(DbContact.READING_CLOSE));
                final String Reading_Net = cursor.getString(cursor.getColumnIndex(DbContact.READING_NET));
                final String Net_Amount = cursor.getString(cursor.getColumnIndex(DbContact.NET_AMOUNT));
                final String Opening_Balance = cursor.getString(cursor.getColumnIndex(DbContact.OPENING_BALANCE));
                final String Closing_Balance = cursor.getString(cursor.getColumnIndex(DbContact.CLOSING_BALANCE));
                final String Remarks = cursor.getString(cursor.getColumnIndex(DbContact.REMARKS));

                StringRequest stringRequest = new StringRequest(Request.Method.POST, DbContact.SERVER_URL_TRANSPORT,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    String Response = jsonObject.getString("response");

                                    if(Response.equals("OK")){
                                       DbHelper1.getInstance(context).updateLocalDatabase(Vehicle,Exp_Date,Society_Name, Spl_Qty,
                                               Transport_Name, Transport_Vehicle_No,Hsd,Toll_Tax,Food,P_Name,P_Expenses,Reading_Open,
                                               Reading_Close,Reading_Net, Net_Amount,Opening_Balance,Closing_Balance,Remarks,
                                               DbContact.SYNC_STATUS_OK_1);
                                        context.sendBroadcast(new Intent(DbContact.UI_UPDATE_BROADCAST));


                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                })
                {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> params = new HashMap<>();
                        params.put("vehicle_no",Vehicle);
                        params.put("exp_date",Exp_Date);
                        params.put("society_name",Society_Name);
                        params.put("spl_qty", Spl_Qty);
                        params.put("transport_name", Transport_Name);
                        params.put("transport_vehicle_no", Transport_Vehicle_No);
                        params.put("hsd", Hsd);
                        params.put("toll_tax", Toll_Tax);
                        params.put("food", Food);
                        params.put("p_name", P_Name);
                        params.put("p_expenses", P_Expenses);
                        params.put("reading_open",Reading_Open);
                        params.put("reading_close", Reading_Close);
                        params.put("reading_net",Reading_Net);
                        params.put("net_amount", Net_Amount);
                        params.put("opening_balance", Opening_Balance);
                        params.put("closing_balance",Closing_Balance);
                        params.put("remarks", Remarks);


                        return params;
                    }
                } ;
                MySingleton.getInstance(context).addToRequestQueue(stringRequest);
            }
        }

        DbHelper1.getInstance(context).close();
    }

}

public boolean checkNetworkConnection(Context context){
    ConnectivityManager connectivityManager =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
    return (networkInfo!= null && networkInfo.isConnected());
}

}

DbHelper1.java

public class DbHelper1 extends SQLiteOpenHelper {
private static DbHelper1 sInstance;
private SQLiteDatabase jewlotDB;


//database version
private static final int DATABASE_VERSION = 3;
public static final String DATABASE_NAME = "ddagroin";



private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + DbContact.TABLE_NAME_TRANSPORT;

public static synchronized DbHelper1 getInstance(Context context) {

    // Use the application context, which will ensure that you
    // don't accidentally leak an Activity's context
    if (sInstance == null) {
        sInstance = new DbHelper1(context.getApplicationContext());
    }
    return sInstance;
}

/**
 * Constructor should be private to prevent direct instantiation.
 * make call to static method "getInstance()" instead.
 */

private DbHelper1(Context context){
    super(context,DATABASE_NAME,null,DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE=  "CREATE TABLE " + DbContact.TABLE_NAME_TRANSPORT
            + "(" + DbContact.COLUMN_ID +
            " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + DbContact.VEHICLE_NO + " VARCHAR, "
            + DbContact.EXP_DATE + " VARCHAR, "
            + DbContact.SOCIETY_NAME + " VARCHAR, "
            + DbContact.SPL_QTY + " VARCHAR, "
            + DbContact.TRANSPORT_NAME + " VARCHAR, "
            + DbContact.TRANSPORT_VEHICLE_NO + " VARCHAR, "
            + DbContact.HSD + " VARCHAR, "
            + DbContact.TOLL_TAX + " VARCHAR, "
            + DbContact.FOOD + " VARCHAR, "
            + DbContact.P_NAME + " VARCHAR, "
            + DbContact.P_EXPENSES + " VARCHAR, "
            + DbContact.READING_OPEN + " VARCHAR, "
            + DbContact.READING_CLOSE + " VARCHAR, "
            + DbContact.READING_NET + " VARCHAR, "
            + DbContact.NET_AMOUNT + " VARCHAR, "
            + DbContact.OPENING_BALANCE + " VARCHAR, "
            + DbContact.CLOSING_BALANCE + " VARCHAR, "
            + DbContact.REMARKS + " VARCHAR, "
            + DbContact.SYNC_STATUS_1 + " INTEGER);";

    db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL(DROP_TABLE);
    onCreate(db);
}

public void saveToLocalDatabase(String vehicle, String exp_date,String society_name,
                                String spl_qty, String transport_name, String transport_vehicle_no,
                                String hsd, String toll_tax, String food, String p_name,
                                String  p_expenses, String reading_open, String reading_close,
                                String reading_net,String net_amount, String opening_balance,
                                String closing_balance, String remarks,
                                int sync_status, SQLiteDatabase database){

    ContentValues contentValues = new ContentValues();
    contentValues.put(DbContact.VEHICLE_NO, vehicle);
    contentValues.put(DbContact.EXP_DATE, exp_date);
    contentValues.put(DbContact.SOCIETY_NAME, society_name);
    contentValues.put(DbContact.SPL_QTY, spl_qty);
    contentValues.put(DbContact.TRANSPORT_NAME, transport_name);
    contentValues.put(DbContact.TRANSPORT_VEHICLE_NO, transport_vehicle_no);
    contentValues.put(DbContact.HSD, hsd);
    contentValues.put(DbContact.TOLL_TAX, toll_tax);
    contentValues.put(DbContact.FOOD, food);
    contentValues.put(DbContact.P_NAME, p_name);
    contentValues.put(DbContact.P_EXPENSES, p_expenses);
    contentValues.put(DbContact.READING_OPEN, reading_open);
    contentValues.put(DbContact.READING_CLOSE, reading_close);
    contentValues.put(DbContact.READING_NET, reading_net);
    contentValues.put(DbContact.NET_AMOUNT, net_amount);
    contentValues.put(DbContact.OPENING_BALANCE, opening_balance);
    contentValues.put(DbContact.CLOSING_BALANCE, closing_balance);
    contentValues.put(DbContact.REMARKS, remarks);
    contentValues.put(DbContact.SYNC_STATUS_1, sync_status);

    database.insert(DbContact.TABLE_NAME_TRANSPORT,null,contentValues);


}

public Cursor readFromLocalDatabase(SQLiteDatabase database) {

    String[] projection = {DbContact.VEHICLE_NO,DbContact.EXP_DATE,DbContact.SOCIETY_NAME,DbContact.SPL_QTY,
            DbContact.TRANSPORT_NAME,DbContact.TRANSPORT_VEHICLE_NO,DbContact.HSD,DbContact.TOLL_TAX,DbContact.FOOD,
            DbContact.P_NAME,DbContact.P_EXPENSES,DbContact.READING_OPEN,DbContact.READING_CLOSE,DbContact.READING_NET,
            DbContact.NET_AMOUNT, DbContact.OPENING_BALANCE,DbContact.CLOSING_BALANCE, DbContact.REMARKS,DbContact.SYNC_STATUS_1};

    return (database.query(DbContact.TABLE_NAME_TRANSPORT, projection, null, null, null, null, null));
}


public void openDataBase() throws SQLException {
    // open the database...
    try {
        String myPath = DATABASE_NAME + DbContact.TABLE_NAME;
        jewlotDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// check the state of db ??....
public boolean isOpen() {
    return jewlotDB != null && jewlotDB.isOpen();
}

@Override
public synchronized void close() {
    if (jewlotDB != null)
        jewlotDB.close();
    super.close();
}

public void updateLocalDatabase(String vehicle,String exp_date, String society_name,
                                String spl_qty, String transport_name, String transport_vehicle_no,
                                String hsd, String toll_tax, String food, String p_name,
                                String  p_expenses, String reading_open, String reading_close,
                                String reading_net,String net_amount, String opening_balance,
                                String closing_balance, String remarks, int sync_status) {
    if (!isOpen()) openDataBase();

    SQLiteDatabase database = this.getReadableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(DbContact.SYNC_STATUS_1, sync_status);
    String selection = DbContact.VEHICLE_NO + " LIKE ?" +" AND "
            + DbContact.EXP_DATE + " LIKE ?"+" AND "
            + DbContact.SOCIETY_NAME + " LIKE ?"
            + DbContact.SPL_QTY + " LIKE ?" +" AND "
            + DbContact.TRANSPORT_NAME + " LIKE ?" +" AND "
            + DbContact.TRANSPORT_VEHICLE_NO + " LIKE ?" +" AND "
            + DbContact.HSD + " LIKE ?" +" AND "
            + DbContact.TOLL_TAX + " LIKE ?" +" AND "
            + DbContact.FOOD + " LIKE ?" +" AND "
            + DbContact.P_NAME + " LIKE ?" +" AND "
            + DbContact.P_EXPENSES + " LIKE ?" +" AND "
            + DbContact.READING_OPEN + " LIKE ?" +" AND "
            + DbContact.READING_CLOSE + " LIKE ?" +" AND "
            + DbContact.READING_NET + " LIKE ?" +" AND "
            + DbContact.NET_AMOUNT + " LIKE ?" +" AND "
            + DbContact.OPENING_BALANCE + " LIKE ?" +" AND "
            + DbContact.CLOSING_BALANCE + " LIKE ?" +" AND "
            + DbContact.REMARKS + " LIKE ?" ;
    String[] selection_args = {vehicle,exp_date,society_name,spl_qty,transport_name,transport_vehicle_no,
            hsd,toll_tax,food,p_name,p_expenses,reading_open,reading_close,reading_net,net_amount,
            opening_balance,closing_balance,remarks};
    database.update(DbContact.TABLE_NAME_TRANSPORT, contentValues, selection, selection_args);
    database.close();
}

}

logcat的

java.lang.RuntimeException: Unable to start receiver com.example.myapplication.Transport.NetworkMonitor1: java.lang.IllegalArgumentException: column 'transport_vehicle_no  ' does not exist

0 个答案:

没有答案