Advertisement
kavallo

Helper SQLite

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