Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. package com.tobias.blazer.timeregistreringv1;
  2.  
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import android.database.Cursor;
  6. import android.content.Context;
  7. import android.content.ContentValues;
  8.  
  9. public class MyDBHandler extends SQLiteOpenHelper{
  10. private static final int DATABASE_VERSION = 1;
  11. public static final String DATABASE_NAME = "productDB.db";
  12. public static final String TABLE_PRODUCTS = "products";
  13. public static final String COLUMN_ID = "_id";
  14. public static final String COLUMN_PRODUCTNAME = "productname";
  15. public static final String COLUMN_QUANTITY = "quantity";
  16. public static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS";
  17.  
  18. //We need to pass database information along to superclass
  19. public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  20. super(context, DATABASE_NAME, factory, DATABASE_VERSION);
  21. }
  22.  
  23. @Override
  24. public void onCreate(SQLiteDatabase db) {
  25. String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
  26. COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
  27. COLUMN_PRODUCTNAME + " TEXT " + COLUMN_QUANTITY +
  28. ");";
  29. db.execSQL(query);
  30. }
  31. //Lesson 51
  32. @Override
  33. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  34. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
  35. onCreate(db);
  36. }
  37.  
  38. //Add a new row to the database
  39. public void addProduct(Products product){
  40. ContentValues values = new ContentValues();
  41. values.put(COLUMN_PRODUCTNAME, product.get_productname());
  42. values.put(COLUMN_QUANTITY, product.get_hours());
  43. SQLiteDatabase db = getWritableDatabase();
  44. db.insert(TABLE_PRODUCTS, null, values);
  45. db.close();
  46. }
  47.  
  48. //Delete a product from the database
  49. public void deleteProduct(String productName){
  50. SQLiteDatabase db = getWritableDatabase();
  51. db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
  52. }
  53.  
  54. public void deleteAllProduct() {
  55. SQLiteDatabase db = getWritableDatabase();
  56. db.delete(TABLE_PRODUCTS, null, null);
  57. }
  58.  
  59. // this is goint in record_TextView in the Main activity.
  60. public String databaseToString(){
  61. String dbString = "";
  62. SQLiteDatabase db = getWritableDatabase();
  63. String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";// why not leave out the WHERE clause?
  64.  
  65.  
  66. //Cursor points to a location in your results
  67. Cursor recordSet = db.rawQuery(query, null);
  68. //Move to the first row in your results
  69. recordSet.moveToFirst();
  70.  
  71. //Position after the last row means the end of the results
  72. while (!recordSet.isAfterLast()) {
  73. // null could happen if we used our empty constructor
  74. if (recordSet.getString(recordSet.getColumnIndex("productname")) != null) {
  75. dbString += recordSet.getString(recordSet.getColumnIndex("productname"));
  76. dbString += "\n";
  77. }
  78. recordSet.moveToNext();
  79. }
  80. db.close();
  81. return dbString;
  82. }
  83.  
  84. public String dataBasehoursToString(){
  85. String dbhString = "";
  86. SQLiteDatabase db = getWritableDatabase();
  87. String query = "SELECT * FROM " + TABLE_PRODUCTS + "";// why not leave out the WHERE clause?
  88.  
  89.  
  90.  
  91. db.close();
  92. return dbhString;
  93. }
  94.  
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement