无法将数据保存到从服务器检索的数据库中

时间:2014-01-18 04:46:42

标签: android

public class DataController extends AsyncTask<String, Void, JSONObject> {
    ProgressDialog login_show;
    private Context context;
    private Route_planDataSource rds;

    private final static String TAG = "DataController";

    public DataController(Context context) {
        this.context = context;

    }

    protected void onPreExecute() {
        rds = new Route_planDataSource(context);

    }

    @Override
    protected JSONObject doInBackground(String... params) {

        JSONObject object = JSONParser.getJSONFromUrl(URL.getRoutePlan,
                params[0]);

        return object;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);

        JSONArray jArray;
        try {
            rds.open();
            jArray = result.getJSONArray("routePlan");
            for (int i = 0; i < jArray.length(); i++) {



                JSONObject jo = jArray.getJSONObject(i);
                Log.i(TAG, "VALUES" + jo.getString("terratory_id").toString());
                Log.i(TAG, "VALUES" + jo.getString("target_st_date"));
                Log.i(TAG, "VALUES" + jo.getString("territory_name"));
                rds.createRoute_plan(1, "", "");
                rds.createRoute_plan(jo.getLong("terratory_id"),
                        jo.getString("target_st_date"),
                        jo.getString("territory_name"))                }
            rds.close();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Log.i("dfdf", rds.getAllRoute_plan().toString());

    }
}

数据库

public class Route_planDataSource {
    private SQLiteDatabase database;
    private CLSQLiteHelper dbHelper;
    private int openedConnections = 0;
    private String[] allColumns = { TableDEtails.COLUMN_ID_ROUTE_PLAN_ID_AND,
            TableDEtails.COLUMN_ID_ROUTE, TableDEtails.COLUMN_ROUTE_NAME,
            TableDEtails.COLUMN_ROUTE_PLAN_DATE };

    public Route_planDataSource(Context context) {
        dbHelper = new CLSQLiteHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
          openedConnections++;
    }



    public   void close() {


           dbHelper.close();

    }

    public Route_plan createRoute_plan(long id_route, String route_plan_date,
            String route_name) {
        ContentValues values = new ContentValues();

        values.put(TableDEtails.COLUMN_ID_ROUTE, id_route);
        values.put(TableDEtails.COLUMN_ROUTE_NAME, route_name);
        values.put(TableDEtails.COLUMN_ROUTE_PLAN_DATE, route_plan_date);

        long insertId = database.insert(TableDEtails.TABLE_ROUTE_PLAN, null,
                values);
        Cursor cursor = database.query(TableDEtails.TABLE_ROUTE_PLAN,
                allColumns, TableDEtails.COLUMN_ID_ROUTE_PLAN_ID_AND + " = "
                        + insertId, null, null, null, null);
        cursor.moveToFirst();
        // ProductBrand productBand = cursorToproductBand(cursor);
        Route_plan route_plan = cursorToRoute_plan(cursor);
        cursor.close();
        return route_plan;
    }

    public void deleteRoute_plan(Route_plan route_plan) {
        long id = route_plan.getRoute_plan_id_and();
        System.out.println("productBand deleted with id: " + id);
        database.delete(TableDEtails.TABLE_ROUTE_PLAN,
                TableDEtails.COLUMN_ID_ROUTE_PLAN_ID_AND + " = " + id, null);
    }

    public List<Route_plan> getAllRoute_plan() {
        List<Route_plan> Route_plans = new ArrayList<Route_plan>();

        Cursor cursor = database.query(TableDEtails.TABLE_ROUTE_PLAN,
                allColumns, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Route_plan route_plan = cursorToRoute_plan(cursor);
        //  ProductBrand productBand = cursorToRoute_plan(cursor);
            Route_plans.add(route_plan);
            cursor.moveToNext();
        }
        // make sure to close the cursor
        cursor.close();
        return Route_plans;
    }

    private Route_plan cursorToRoute_plan(Cursor cursor) {
        Route_plan route_plan = new Route_plan();
        route_plan.setRoute_plan_id_and(cursor.getLong(0));
        route_plan.setId_route(cursor.getLong(1));
        route_plan.setRoute_name(cursor.getString(3));
        route_plan.setRoute_plan_date(cursor.getString(2));


        return route_plan;
    }

}

tabledetails

public class TableDEtails {

    public static final String TABLE_ROUTE_PLAN = "table_route_plan";



        /** This data related to route plan table **/
        public static final String COLUMN_ID_ROUTE_PLAN_ID_AND = "column_id_route_plan_id_and";
        public static final String COLUMN_ID_ROUTE = "column_id_route";
        public static final String COLUMN_ROUTE_PLAN_DATE = "column_route_plan_date";
        public static final String COLUMN_ROUTE_NAME = "column_route_name";   

        // route_Plan table creating query
        public static final String ROUTE_PLAN_TABLE_CREATE = "create table "
                + TABLE_ROUTE_PLAN + "(" + COLUMN_ID_ROUTE_PLAN_ID_AND
                + " integer primary key autoincrement, " + COLUMN_ID_ROUTE
                + " integer," + COLUMN_ROUTE_PLAN_DATE + " text,"
                + COLUMN_ROUTE_NAME + " text)";

        // outlet table creating query


    }

CLSQLiteHelper

public class CLSQLiteHelper extends SQLiteOpenHelper {  


  private static final String DATABASE_NAME = "system.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement


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

    @Override


public void onCreate(SQLiteDatabase database) {

          database.execSQL(TableDEtails.ROUTE_PLAN_TABLE_CREATE);


        }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(CLSQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " +  TableDEtails.TABLE_USER);
    onCreate(db);
  }

} 

我尝试了上面的代码从服务器检索数据并将数据保存到数据库以进行脱机访问。从服务器下载代码工作正常,但是在发生错误后将数据保存到数据库中

01-18 04:30:09.375: E/AndroidRuntime(20453): android.database.sqlite.SQLiteException: not an error (code 0)
01-18 04:30:09.375: E/AndroidRuntime(20453):    at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
01-18 04:30:09.375: E/AndroidRuntime(20453):    at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:858)
01-18 04:30:09.375: E/AndroidRuntime(20453):    at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
01-18 04:30:09.375: E/AndroidRuntime(20453):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)

0 个答案:

没有答案