Advertisement
Guest User

Untitled

a guest
Jun 28th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. public class DbAdapter extends AbstractDbAdapter {
  2.  
  3. private static final String DATABASE_TABLE_1 = "Lists";
  4.  
  5. public static final String KEY_ROWID1 = "_id";
  6. public static final String KEY_TITLE = "Title";
  7. public static final String KEY_SHOP = "Shop";
  8. public static final String KEY_DATA = "Data";
  9. public static final String KEY_B_ALLOC = "Budget_allocated";
  10. public static final String KEY_B_SPENT = "Budget_spent";
  11.  
  12. private static final String DATABASE_TABLE_2 = "Products";
  13.  
  14. public static final String KEY_ROWID2 = "_id";
  15. public static final String KEY_LISTID = "ListId";
  16. public static final String KEY_ITEM = "Item";
  17. public static final String KEY_QUANTITY = "Quantity";
  18. public static final String KEY_UNITS = "Units";
  19.  
  20. private static final String DATABASE_TABLE_3 = "Shops";
  21.  
  22. public static final String KEY_ROWID3 = "_id";
  23. public static final String NAME = "Name";
  24. public static final String ADRESS = "Adress";
  25. public static final String TYPE = "Type";
  26. public static final String LATITUDINE = "Latitudine";
  27. public static final String LONGITUDINE = "Longitudine";
  28.  
  29. private static final String DATABASE_TABLE_4 = "Budget";
  30.  
  31. public static final String KEY_ROWID4 = "_id";
  32. public static final String KEY_BUDGET = "Budget";
  33. public static final String KEY_BUDGET_DATA = "Data_budget";
  34.  
  35. DbAdapter db;
  36.  
  37. public DbAdapter(Context ctx) {
  38. super(ctx);
  39. }
  40.  
  41. /* create */
  42. public long createlist(String title, String shop, String data,
  43. String b_alloc, String b_spent) {
  44. ContentValues args = new ContentValues();
  45. args.put(KEY_TITLE, title);
  46. args.put(KEY_SHOP, shop);
  47. args.put(KEY_DATA, data);
  48. args.put(KEY_B_ALLOC, b_alloc);
  49. args.put(KEY_B_SPENT, b_spent);
  50.  
  51. return mDb.insert(DATABASE_TABLE_1, null, args);
  52. }
  53.  
  54. public long createproduct(Integer listid, String item, String quantity,
  55. String units) {
  56. ContentValues args = new ContentValues();
  57. args.put(KEY_LISTID, listid);
  58. args.put(KEY_ITEM, item);
  59. args.put(KEY_QUANTITY, quantity);
  60. args.put(KEY_UNITS, units);
  61.  
  62. return mDb.insert(DATABASE_TABLE_2, null, args);
  63. }
  64.  
  65. public long createshop(String shop, String adress, String types,
  66. Double latitudine, Double longitudine) {
  67. ContentValues args = new ContentValues();
  68. args.put(NAME, shop);
  69. args.put(ADRESS, adress);
  70. args.put(TYPE, types);
  71. args.put(LATITUDINE, latitudine);
  72. args.put(LONGITUDINE, longitudine);
  73. return mDb.insert(DATABASE_TABLE_3, null, args);
  74. }
  75.  
  76. public long createbudget(String budget, String data) {
  77. ContentValues args = new ContentValues();
  78. args.put(KEY_BUDGET, budget);
  79. args.put(KEY_BUDGET_DATA, data);
  80. return mDb.insert(DATABASE_TABLE_4, null, args);
  81. }
  82.  
  83. /* Update */
  84. public boolean updatelist(long rowId, String title, String shop,
  85. String data, String b_alloc, String b_spent) {
  86. ContentValues args = new ContentValues();
  87. args.put(KEY_TITLE, title);
  88. args.put(KEY_SHOP, shop);
  89. args.put(KEY_DATA, data);
  90. args.put(KEY_B_ALLOC, b_alloc);
  91. args.put(KEY_B_SPENT, b_spent);
  92.  
  93. return mDb.update(DATABASE_TABLE_1, args, KEY_ROWID1 + "=" + rowId,
  94. null) > 0;
  95. }
  96.  
  97. public boolean updateproduct(long rowId, Integer listid, String item,
  98. String quantity, String units) {
  99. ContentValues values = new ContentValues();
  100. values.put(KEY_LISTID, listid);
  101. values.put(KEY_ITEM, item);
  102. values.put(KEY_QUANTITY, quantity);
  103. values.put(KEY_UNITS, units);
  104. return mDb.update(DATABASE_TABLE_2, values, KEY_ROWID2 + "=" + rowId,
  105. null) > 0;
  106. }
  107.  
  108. public boolean updateshop(long rowId, String shop, String adress,
  109. String types, Double latitudine, Double longitudine) {
  110. ContentValues args = new ContentValues();
  111. args.put(NAME, shop);
  112. args.put(ADRESS, adress);
  113. args.put(TYPE, types);
  114. args.put(LATITUDINE, latitudine);
  115. args.put(LONGITUDINE, longitudine);
  116. return mDb.update(DATABASE_TABLE_3, args, KEY_ROWID3 + "=" + rowId,
  117. null) > 0;
  118. }
  119.  
  120. public boolean updatebudget(long rowId, String budget, String data) {
  121. ContentValues args = new ContentValues();
  122. args.put(KEY_BUDGET, budget);
  123. args.put(KEY_BUDGET_DATA, data);
  124. return mDb.update(DATABASE_TABLE_4, args, KEY_ROWID4 + "=" + rowId,
  125. null) > 0;
  126. }
  127.  
  128. /* Delete */
  129. public boolean deletelist(long rowId) {
  130. return mDb.delete(DATABASE_TABLE_1, KEY_ROWID1 + "=" + rowId, null) > 0;
  131. }
  132.  
  133. public boolean deleteproduct(long rowId) {
  134. return mDb.delete(DATABASE_TABLE_2, KEY_ROWID2 + "=" + rowId, null) > 0;
  135. }
  136.  
  137. public boolean deleteshop(long rowId) {
  138. return mDb.delete(DATABASE_TABLE_3, KEY_ROWID3 + "=" + rowId, null) > 0;
  139. }
  140.  
  141. public boolean deletebudget(long rowId) {
  142. return mDb.delete(DATABASE_TABLE_4, KEY_ROWID4 + "=" + rowId, null) > 0;
  143. }
  144.  
  145. /* Getall */
  146.  
  147. public Cursor getAllLists() {
  148. return mDb.query(DATABASE_TABLE_1, new String[] { KEY_ROWID1,
  149. KEY_TITLE, KEY_SHOP, KEY_DATA, KEY_B_ALLOC, KEY_B_SPENT },
  150. null, null, null, null, null);
  151. }
  152.  
  153. public Cursor getAllProducts() {
  154. return mDb.query(DATABASE_TABLE_2, new String[] { KEY_ROWID2,
  155. KEY_LISTID, KEY_ITEM, KEY_QUANTITY, KEY_UNITS }, null, null,
  156. null, null, null);
  157. }
  158.  
  159. public Cursor getAllShops() {
  160. return mDb.query(DATABASE_TABLE_3, new String[] { KEY_ROWID3, NAME,
  161. ADRESS, TYPE, LATITUDINE, LONGITUDINE }, null, null, null,
  162. null, null);
  163. }
  164.  
  165. public Cursor getAllBudget() {
  166. return mDb.query(DATABASE_TABLE_4, new String[] { KEY_ROWID4,
  167. KEY_BUDGET, KEY_BUDGET_DATA }, null, null, null, null, null);
  168. }
  169.  
  170. /* Getone */
  171.  
  172. public Cursor getList(long rowId) throws SQLException {
  173. Cursor mCursor = mDb.query(true, DATABASE_TABLE_1, new String[] {
  174. KEY_ROWID1, KEY_TITLE, KEY_SHOP, KEY_DATA, KEY_B_ALLOC,
  175. KEY_B_SPENT }, KEY_ROWID1 + "=" + rowId, null, null, null,
  176. null, null);
  177. if (mCursor != null) {
  178. mCursor.moveToFirst();
  179. }
  180. return mCursor;
  181.  
  182. }
  183.  
  184. public Cursor getProduct(long rowId) throws SQLException {
  185. Cursor mCursor = mDb.query(true, DATABASE_TABLE_2, new String[] {
  186. KEY_ROWID2, KEY_LISTID, KEY_ITEM, KEY_QUANTITY, KEY_UNITS },
  187. KEY_ROWID2 + "=" + rowId, null, null, null, null, null);
  188. if (mCursor != null) {
  189. mCursor.moveToFirst();
  190. }
  191. return mCursor;
  192.  
  193. }
  194.  
  195. public Cursor getShop(long rowId) throws SQLException {
  196. Cursor mCursor = mDb.query(true, DATABASE_TABLE_3, new String[] {
  197. KEY_ROWID3, NAME, ADRESS, TYPE, LATITUDINE, LONGITUDINE },
  198. KEY_ROWID3 + "=" + rowId, null, null, null, null, null);
  199. if (mCursor != null) {
  200. mCursor.moveToFirst();
  201. }
  202. return mCursor;
  203.  
  204. }
  205.  
  206. public Cursor getBudget(long rowId) throws SQLException {
  207. Cursor mCursor = mDb.query(true, DATABASE_TABLE_4, new String[] {
  208. KEY_ROWID4, KEY_BUDGET, KEY_BUDGET_DATA }, KEY_ROWID4 + "="
  209. + rowId, null, null, null, null, null);
  210. if (mCursor != null) {
  211. mCursor.moveToFirst();
  212. }
  213. return mCursor;
  214.  
  215. }
  216.  
  217. public Cursor fetchListId(String listId) throws SQLException {
  218. Cursor mCursor = mDb.query(true, DATABASE_TABLE_2, new String[] {
  219. KEY_ROWID2, KEY_LISTID, KEY_ITEM, KEY_QUANTITY, KEY_UNITS },
  220. KEY_LISTID + "=" + listId, null, null, null, null, null);
  221. if (mCursor != null) {
  222. mCursor.moveToFirst();
  223. }
  224. return mCursor;
  225. }
  226.  
  227. /* Altele */
  228.  
  229. /* For shops */
  230. public long getId(Cursor c) {
  231. return (c.getLong(0));
  232. }
  233.  
  234. public String getName(Cursor c) {
  235. return (c.getString(1));
  236. }
  237.  
  238. public String getAddress(Cursor c) {
  239. return (c.getString(2));
  240. }
  241.  
  242. public String getType(Cursor c) {
  243. return (c.getString(3));
  244. }
  245.  
  246. public Double getLatitudine(Cursor c) {
  247. return (c.getDouble(4));
  248. }
  249.  
  250. public Double getLongitudine(Cursor c) {
  251. return (c.getDouble(5));
  252. }
  253.  
  254. /* For Lists */
  255.  
  256. public String getTitle(Cursor c) {
  257. return (c.getString(1));
  258.  
  259. }
  260.  
  261. public String getShop(Cursor c) {
  262. return (c.getString(2));
  263.  
  264. }
  265.  
  266. public String getData(Cursor c) {
  267. return (c.getString(3));
  268. }
  269.  
  270. /* For Budget */
  271.  
  272. public String getBudget(Cursor c) {
  273. return (c.getString(1));
  274. }
  275.  
  276. public String getDataBudget(Cursor c) {
  277. return (c.getString(2));
  278. }
  279.  
  280. /* incercari */
  281.  
  282. public Cursor getAllSearch(String where, String orderBy) {
  283. StringBuilder buf=new StringBuilder("SELECT _id, Title, Shop , Data , Budget_allocated ," +
  284. " Budget_spent FROM Lists");
  285.  
  286. if (where!=null) {
  287. buf.append(" WHERE ");
  288. buf.append(where);
  289. }
  290.  
  291. if (orderBy!=null) {
  292. buf.append(" ORDER BY ");
  293. buf.append(orderBy);
  294. }
  295.  
  296. return(mDb.rawQuery(buf.toString(), null));
  297. }
  298.  
  299. public Cursor getAll(){
  300.  
  301. return (mDb.rawQuery("SELECT _id, Title, Shop , Data , Budget_allocated ," +
  302. " Budget_spent FROM Lists ORDER BY Title",null));
  303. }
  304. /*public Cursor getListsOrder(String[] columns, String selection,
  305. String[] selectionArgs, String groupBy, String having,
  306. String sortBy, String sortOption) {
  307. return mDb.query(DATABASE_TABLE_1, columns, selection, selectionArgs,
  308. groupBy, having, sortBy + " " + sortOption);
  309. }*/
  310.  
  311. /*public Cursor getListsOrder() {
  312.  
  313. Cursor mCursor = db.query(DATABASE_TABLE_1, new String[] { KEY_ROWID1,
  314. KEY_TITLE, KEY_SHOP, KEY_DATA, KEY_B_ALLOC, KEY_B_SPENT },
  315. null, null, null, null,KEY_TITLE + " ASC");
  316. return mCursor;
  317. // db.query(table, columns, selection, selectionArgs, groupBy, having,
  318. // orderBy)
  319. }*/
  320.  
  321. /*public ArrayList<String> getData(String[] keys, String selection,
  322. String[] selectionArgs, String groupBy, String having,
  323. String sortBy, String sortOption) {
  324.  
  325. ArrayList<String> list = new ArrayList<String>();
  326. Cursor results = db.getListsOrder(keys, selection, selectionArgs,
  327. groupBy, having, sortBy, sortOption);
  328. while (results.moveToNext())
  329. list.add(results.getString(results.getColumnIndex(sortBy)));
  330. return list;
  331.  
  332. }*/
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement