Advertisement
Guest User

asfafaagghghhkl

a guest
Nov 27th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. public void addCode(Code code){
  2.         Log.d("addCode", code.toString());
  3.         // 1. get reference to writable DB
  4.         SQLiteDatabase db = this.getWritableDatabase();
  5.  
  6.         // 2. create ContentValues to add key "column"/value
  7.         ContentValues values = new ContentValues();
  8.         values.put(KEY_CODE, code.getCode()); // get title
  9.         values.put(KEY_LAT, code.getLat());
  10.         values.put(KEY_LONG, code.getLon());
  11.         //values.put(KEY_locale, code.getAuthor()); // get author
  12.  
  13.         // 3. insert
  14.         db.insert(TABLE_CODES, // table
  15.                 null, //nullColumnHack
  16.                 values); // key/value -> keys = column names/ values = column values
  17.       //  contId++;
  18.         // 4. close
  19.         db.close();
  20.     }
  21.    
  22.     public Code getCode(int id){
  23.          
  24.         // 1. get reference to readable DB
  25.         SQLiteDatabase db = this.getReadableDatabase();
  26.  
  27.         // 2. build query
  28.         Cursor cursor = db.query(TABLE_CODES, COLUMNS, " id = ?", new String[] { String.valueOf(id) }, null, null, null, null);
  29.  
  30.         // 3. if we got results get the first one
  31.         if (cursor != null)
  32.             cursor.moveToFirst();
  33.  
  34.         // 4. build code object
  35.         Code code = new Code();
  36.         code.setId(Integer.parseInt(cursor.getString(0)));
  37.         code.setCode(cursor.getString(1));
  38.         code.setLat(cursor.getString(2));
  39.         code.setLon(cursor.getString(3));
  40.         //code.setAuthor(cursor.getString(2));
  41.  
  42.         Log.d("getCode("+id+")", code.toString());
  43.  
  44.         // 5. return book
  45.         return code;
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement