Android Open Source - RadiusDev D B Helper






From Project

Back to project page RadiusDev.

License

The source code is released under:

MIT License

If you think the Android project RadiusDev listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.radiusdev;
/*from  w ww. j ava 2 s. c o  m*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper  extends SQLiteOpenHelper {
  
  public DBHelper(Context applicationContext){
    super(applicationContext, "contactbook.db", null, 1);
  }

  @Override
  public void onCreate(SQLiteDatabase db){
    String query = "CREATE TABLE contacts (contactId INTEGER PRIMARY KEY, firstName TEXT,"+
          "lastName TEXT, phoneNumber TEXT, emailAddress TEXT)";
          
    db.execSQL(query);
      
  //  }
    
    
    
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    String query = "DROP TABLE IF EXISTS contacts";
    db.execSQL(query);
    onCreate(db);
    
  }
  
  //Checks if database exists or not
  /* private boolean checkDataBase() {
      SQLiteDatabase checkDB = null;
      File f=new File("/data/data/your.app.package/databases/contactbook.db");
      try {
          checkDB = SQLiteDatabase.openDatabase(f.getPath(), null,
                  SQLiteDatabase.OPEN_READONLY);
          checkDB.close();
      } catch (SQLiteException e) {
          // database doesn't exist yet.
      }
      return checkDB != null ? true : false;
  } */
  
  public void addUser(HashMap<String, String>queryValues){
    
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("firstName", queryValues.get("firstName"));
    values.put("lastName", queryValues.get("lastName"));
    values.put("phoneNumber", queryValues.get("phoneNumber"));
    values.put("emailAddress", queryValues.get("emailAddress"));
    db.insert("contacts", null, values);
    
    db.close();
    
  }
  public void insertContact(HashMap<String, String>queryValues){
    
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("firstName", queryValues.get("firstName"));
    values.put("lastName", queryValues.get("lastName"));
    values.put("phoneNumber", queryValues.get("phoneNumber"));
    values.put("emailAddress", queryValues.get("emailAddress"));
    db.insert("contacts", null, values);
    
    db.close();
    
  }
  
  public void userData(HashMap<String, String>queryValues){
    
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("firstName", queryValues.get("firstName"));
    values.put("lastName", queryValues.get("lastName"));
    values.put("phoneNumber", queryValues.get("phoneNumber"));
    values.put("emailAddress", queryValues.get("emailAddress"));
    db.insert("userData", null, values);
    
    db.close();
    
  }
  
  
  public int updateContact(HashMap<String, String>queryValues){
    
    SQLiteDatabase db = this.getWritableDatabase();
    
    ContentValues values = new ContentValues();
    
    values.put("firstName", queryValues.get("firstName"));
    values.put("lastName", queryValues.get("lastName"));
    values.put("phoneNumber", queryValues.get("phoneNumber"));
    values.put("emailAddress", queryValues.get("emailAddress"));
    
    return db.update("contacts", values, 
        "contactId"+"=?", new String[] {queryValues.get("contactId")});
  }
  
  
  public void deleteContact(String id){
    
    SQLiteDatabase db = this.getWritableDatabase();
    
    String deleteQuery = "DELETE FROM contacts WHERE contactId = '"+id+"'";
    
    db.execSQL(deleteQuery);
  }
  
  
  public ArrayList<HashMap<String,String>> getAllContacts(){
    
    ArrayList<HashMap<String,String>> contactArrayList = new ArrayList<HashMap<String,String>>();
    
    String selectQuery = "SELECT * FROM contacts ORDER BY lastName";
    
    SQLiteDatabase db = this.getWritableDatabase();
    
    Cursor cursor = db.rawQuery(selectQuery, null);
    
    if(cursor.moveToFirst()){
      
      do{
        HashMap<String, String> contactMap = new HashMap<String, String>();
        
        contactMap.put("contactId", cursor.getString(0));
        contactMap.put("firstName", cursor.getString(1));
        contactMap.put("lastName", cursor.getString(2));
        contactMap.put("phoneNumber", cursor.getString(3));
        contactMap.put("emailAddress", cursor.getString(4));
        
        contactArrayList.add(contactMap);
      }while(cursor.moveToNext());
    }
    
    return contactArrayList;
  }
  
  public HashMap<String, String> getContactInfo(String id){
    
    HashMap<String, String> contactMap = new HashMap<String, String>();
    
    SQLiteDatabase db = this.getReadableDatabase();
    
    String selectQuery = "SELECT * FROM contacts WHERE contactId = '"+id+"'";
    
    Cursor cursor = db.rawQuery(selectQuery, null);
    
    if(cursor.moveToFirst()){
      
      do{        
        contactMap.put("contactId", cursor.getString(0));
        contactMap.put("firstName", cursor.getString(1));
        contactMap.put("lastName", cursor.getString(2));
        contactMap.put("phoneNumber", cursor.getString(3));
        contactMap.put("emailAddress", cursor.getString(4));
                
      }while(cursor.moveToNext());
    }
    
    return contactMap;
    
  }
  
  

}




Java Source Code List

com.example.radiusdev.AppPreferenceActivity.java
com.example.radiusdev.ArrayAdapter.java
com.example.radiusdev.Contact.java
com.example.radiusdev.CustomizedListView.java
com.example.radiusdev.DBHelper.java
com.example.radiusdev.DisplayAbout.java
com.example.radiusdev.DisplayContactList.java
com.example.radiusdev.DisplayMapContacts.java
com.example.radiusdev.DisplayUserInfo.java
com.example.radiusdev.EditContact.java
com.example.radiusdev.EditUser.java
com.example.radiusdev.GPS.java
com.example.radiusdev.JSONparser.java
com.example.radiusdev.Login.java
com.example.radiusdev.MainActivity.java
com.example.radiusdev.Main.java
com.example.radiusdev.MapWrapperLayout.java
com.example.radiusdev.Map.java
com.example.radiusdev.NewContact.java
com.example.radiusdev.OnInfoWindowElemTouchListener.java
com.example.radiusdev.PreferencesHelper.java
com.example.radiusdev.Register.java
com.example.radiusdev.UserSettings.java
com.example.radiusdev.imageUploader.java