Advertisement
Guest User

Untitled

a guest
Jul 11th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. package com.damkell.kamera;
  2. import java.io.ByteArrayOutputStream;
  3. import java.util.ArrayList;
  4.  
  5.  
  6.  
  7.  
  8. import java.util.List;
  9.  
  10. import android.content.ContentValues;
  11. import android.content.Context;
  12. import android.database.Cursor;
  13. import android.database.sqlite.SQLiteDatabase;
  14. import android.database.sqlite.SQLiteOpenHelper;
  15. import android.graphics.Bitmap;
  16. import android.graphics.Bitmap.CompressFormat;
  17. import android.graphics.BitmapFactory;
  18. import android.widget.Toast;
  19.  
  20.  
  21.  
  22.        
  23.     public class DatabaseHandler extends SQLiteOpenHelper {
  24.         // All Static variables
  25.         // Database Version
  26.         private static final int DATABASE_VERSION = 1;
  27.      
  28.         // Database Name
  29.         private static final String DATABASE_NAME = "contactsManager";
  30.      
  31.         // Contacts table name
  32.         private static final String TABLE_CONTACTS = "contacts";
  33.      
  34.         // Contacts Table Columns names
  35.         private static final String KEY_ID = "id";
  36.         private static final String KEY_NAME = "name";
  37.         private static final String KEY_PH_NO = "phone_number";
  38.         private static final String KEY_RAKTAS = "raktas";
  39.      
  40.         public DatabaseHandler(Context context) {
  41.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  42.         }
  43.      
  44.         // Creating Tables
  45.         @Override
  46.         public void onCreate(SQLiteDatabase db) {
  47.             String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
  48.                     + KEY_ID + " INTEGER," + KEY_NAME + " TEXT,"
  49.                     + KEY_RAKTAS + " TEXT" + ")";
  50.             db.execSQL(CREATE_CONTACTS_TABLE);
  51.         }
  52.      
  53.         // Upgrading database
  54.         @Override
  55.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  56.             // Drop older table if existed
  57.             db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
  58.      
  59.             // Create tables again
  60.             onCreate(db);
  61.         }
  62.         //Adding new contact
  63.         public void addContact(String eilute, int kelintas, String kelintas2){
  64.             SQLiteDatabase db = this.getWritableDatabase();
  65.             ContentValues values = new ContentValues();
  66.             values.put(KEY_NAME, eilute);
  67.             values.put(KEY_ID, kelintas);
  68.             values.put(KEY_RAKTAS,kelintas2);
  69.             values.put(KEY_PH_NO, "ale");
  70.             //Inserting row
  71.             db.insert(TABLE_CONTACTS, null, values);
  72.             db.close(); // Close database connection
  73.         }
  74.        
  75.         //Getting single contact
  76.         public String getContact(int id, int id2){
  77.             String eilute = null;
  78.             boolean tesk = true;
  79.             SQLiteDatabase db = this.getReadableDatabase(); ////// eilute zemiau. CRASH
  80.             Cursor cursor = db.query(TABLE_CONTACTS, new String[] {KEY_ID,KEY_RAKTAS},KEY_PH_NO + "=?",new String[] {String.valueOf(id2)},null,null,null,null);
  81.             /*if(cursor != null){
  82.                 cursor.moveToFirst();
  83.                 eilute = cursor.getString(1);
  84.             }*/
  85.             if (eilute != null)
  86.             return eilute;
  87.             else return eilute = "NERA";
  88.         }
  89.         /*
  90.         //Getting all contacts
  91.         public List<Contact> getAllContacts(){
  92.             List<Contact> contactList = new ArrayList<Contact>();
  93.             //Selecting all query
  94.             String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
  95.            
  96.             SQLiteDatabase db = this.getWritableDatabase();
  97.             Cursor cursor = db.rawQuery(selectQuery,null);
  98.            
  99.             //Looping through all rows and adding to list
  100.             if(cursor.moveToFirst()){
  101.                 do{
  102.                     Contact contact = new Contact();
  103.                     contact.setID(Integer.parseInt(cursor.getString(0)));
  104.                     contact.setName(cursor.getString(1));
  105.                     contact.setPhoneNumber(cursor.getString(2));
  106.                     //adding contact to list
  107.                     contactList.add(contact);
  108.                 } while (cursor.moveToNext());
  109.             }
  110.            
  111.             return contactList;
  112.         }
  113.        
  114.         //Getting contacts count
  115.         public int getContactsCount(){
  116.             String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
  117.             SQLiteDatabase db = this.getReadableDatabase();
  118.             Cursor cursor = db.rawQuery(countQuery,null);
  119.             cursor.close();
  120.            
  121.             //return count
  122.             return cursor.getCount();
  123.         }
  124.         //Updating single contact
  125.         public int updateContact(Contact contact){
  126.             SQLiteDatabase db = this.getWritableDatabase();
  127.             ContentValues values = new ContentValues();
  128.             values.put(KEY_NAME, contact.getName());
  129.             values.put(KEY_PH_NO, contact.getPhoneNumber());
  130.            
  131.             //Updating row
  132.             return db.update(TABLE_CONTACTS,values,KEY_ID + "=?",new String[]{String.valueOf(contact.getID())});
  133.         }
  134.        
  135.         //Deleting single contact
  136.         public void deleteContact(Contact contact){
  137.             SQLiteDatabase db = this.getWritableDatabase();
  138.             db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[]{String.valueOf(contact.getID())});
  139.             db.close();*/
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement