Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.simplestsqlite;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import androidx.annotation.Nullable;
- public class MyContactDB extends SQLiteOpenHelper {
- public final static String DB_NAME = "my_contacts.DB";
- //increases in DB_VERSION should cause "onUpgrade"
- public final static int DB_VERSION = 1;
- public final static String TABLE_CONTACTS = "tContacts";
- public final static String COL_NAME = "cName";
- public final static String COL_PHONE = "cPhone";
- /*
- create table if not exists tContacts(
- _id INTEGER PRIMARY KEY NOT NULL,
- cName TEXT NOT NULL,
- cPhone TEXT NOT NULL
- );
- */
- public final static String CREATE_TABLE_CONTACTS =
- "create table if not exists "+TABLE_CONTACTS+"(\n"+
- "_id INTEGER PRIMARY KEY NOT NULL,\n"+
- COL_NAME+" TEXT NOT NULL,\n"+
- COL_PHONE+" TEXT NOT NULL\n);";
- public final static String DROP_TABLE_CONTACTS =
- "drop table if exists "+TABLE_CONTACTS+";";
- public MyContactDB(
- @Nullable Context context, //context
- @Nullable String name, //DB_NAME
- @Nullable SQLiteDatabase.CursorFactory factory, //
- int version //DB_VERSION
- ) {
- super(context, name, factory, version);
- }//MyContactDB
- public MyContactDB(
- @Nullable Context context //context
- ){
- super(
- context,
- DB_NAME,
- null,
- DB_VERSION
- );
- }//MyContact
- @Override
- public void onCreate(SQLiteDatabase db) {
- //automatically called by the framework
- //when the need to work with still non-existing
- //tables
- installDB(db);
- }//onCreate
- void installDB(SQLiteDatabase pDB){
- if (pDB!=null){
- try {
- pDB.execSQL(CREATE_TABLE_CONTACTS);
- }
- catch (Exception e){
- Log.e(
- "@MyContactDB",
- e.toString()
- );
- }
- }
- }//installDB
- @Override
- public void onUpgrade(
- SQLiteDatabase db,
- int oldVersion, //1
- int newVersion //2
- )
- {
- //automatically called when
- //the database changes
- //that is signaled by the database "version"
- if (newVersion>oldVersion){
- try{
- //destroy
- db.execSQL(
- DROP_TABLE_CONTACTS
- );
- //reconstruct
- installDB(db);
- }//try
- catch (Exception e){
- Log.e(
- "@MyContactsDB",
- e.toString()
- );
- }//catch
- }//if
- }//onUpgrade
- public final int DOES_NOT_EXIST = -1;
- /*
- receives a MyContact
- returns the _id in TABLE_CONTACTS
- where that MyContact already exists
- or
- -1, if it does not exist
- */
- public long getIdForContact(MyContact pC){
- SQLiteDatabase db = this.getReadableDatabase();
- if (db!=null){
- //select * from tContacts where (cName='Rita' and cPhone='123')
- String strQ = String.format(
- "select %s from %s where (%s='%s' and %s='%s')",
- //COL_NAME+","+COL_PHONE
- "*",
- TABLE_CONTACTS,
- COL_NAME,
- pC.mName,
- COL_PHONE,
- pC.mPhone
- );
- Cursor cursorResults =
- db.rawQuery(
- strQ,
- null
- );
- if (cursorResults!=null){
- int iHowMany = cursorResults.getCount();
- if (iHowMany>0){
- //a contact with that name and phone
- //already exists
- cursorResults.moveToFirst();
- int indexOfIdColumn =
- cursorResults.getColumnIndex(
- "_id"
- );
- long idOfAlreadyExistingContact =
- cursorResults.getLong(
- //0
- indexOfIdColumn
- );
- return idOfAlreadyExistingContact;
- }//if
- }
- }
- return DOES_NOT_EXIST;
- }//getIdForContact
- long insertNewContact(MyContact pC){
- long iCurrentIdForContact =
- getIdForContact(pC);
- boolean bWillBeNew =
- iCurrentIdForContact == DOES_NOT_EXIST;
- if (bWillBeNew){
- SQLiteDatabase db =
- this.getWritableDatabase();
- if (db!=null){
- ContentValues cvs = new ContentValues();
- cvs.put(COL_NAME, pC.mName);
- cvs.put(COL_PHONE, pC.mPhone);
- long lWhereInserted =
- db.insert(
- TABLE_CONTACTS,
- null,//we have no columns allowing null
- cvs
- );
- return lWhereInserted;
- }
- }
- return iCurrentIdForContact;
- }//insertNewContact
- }//MyContactDB
Add Comment
Please, Sign In to add comment