-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContactsDatabaseHandler.java
More file actions
108 lines (93 loc) · 3.26 KB
/
Copy pathContactsDatabaseHandler.java
File metadata and controls
108 lines (93 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.example.tarun.auxilium;
import android.app.Activity;
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 java.util.ArrayList;
/**
* Created by TARUN on 07-07-2016.
*/
public class ContactsDatabaseHandler extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="Contacts.db";
public static final String TABLE_CONTACTS="Contacts_Imported";
public static final String COLUMN_ID="_id";
public static final String COLUMN_NAME="name";
public static final String COLUMN_PHONENO="number";
public ContactsDatabaseHandler(Context context) {
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String query="CREATE TABLE "+TABLE_CONTACTS+" ( "+COLUMN_ID+" INTEGER PRIMARY KEY, "
+COLUMN_NAME+" TEXT, "+COLUMN_PHONENO+" INTEGER "+");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_CONTACTS);
onCreate(db);
}
public void addContacts(String id,String contactName,String contactPhoneNo)
{
ContentValues contentValues=new ContentValues();
SQLiteDatabase db=getWritableDatabase();
contentValues.put(COLUMN_ID,id);
contentValues.put(COLUMN_NAME,contactName);
contentValues.put(COLUMN_PHONENO,contactPhoneNo);
db.insert(TABLE_CONTACTS,null,contentValues);
// db.close();
}
public void onStop()
{
}
public void deleteContacts(String contactName)
{
SQLiteDatabase db=getWritableDatabase();
String query="DELETE FROM " + TABLE_CONTACTS + " WHERE " + COLUMN_NAME + "=\"" + contactName +"\";";
db.execSQL(query);
}
Cursor getCursor(String name)
{
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + ";";
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery,null);
return cursor;
}
/*
public ArrayList<String> databaseToString()
{
ArrayList<String> arrayList=new ArrayList<>();
String dbNameString="";
String dbPhoneNoString="";
SQLiteDatabase db=getWritableDatabase();
String query="SELECT * FROM "+TABLE_CONTACTS;
Cursor c=db.rawQuery(query,null);
c.moveToFirst();
while (!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("name"))!=null)
{
dbNameString+=c.getString(c.getColumnIndex("name"));
dbPhoneNoString+=c.getString(c.getColumnIndex("number"));
arrayList.add(dbNameString+"\n("+dbPhoneNoString+")");
}
c.moveToNext();
}
db.close();
return arrayList;
}
*/
Cursor getListContacts()
{ String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + ";";
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery,null);
return cursor;
}
}