forked from hussien89aa/AndroidTutorialForBeginners
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBManager.java
More file actions
Latest commit
91 lines (72 loc) · 2.78 KB
/
Copy pathDBManager.java
File metadata and controls
91 lines (72 loc) · 2.78 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
packagecom.hussienalrubaye.androiddemowork;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
importandroid.database.sqlite.SQLiteQueryBuilder;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.TextView;
importandroid.widget.Toast;
importjava.util.ArrayList;
/**
* Created by hussienalrubaye on 9/8/16.
*/
publicclassDBManager {
privateSQLiteDatabasesqlDB;
staticfinalStringDBName="Students";
staticfinalStringTableName="Logins";
staticfinalStringColUserName="UserName";
staticfinalStringColPassWord="Password";
staticfinalStringColID="ID";
staticfinalintDBVersion=3;
// create table Logins(ID integer primary key autoincrment, UserName text, Password text)
staticfinalStringCreateTable=" CREATE TABLE IF NOT EXISTS " +TableName+
"(ID INTEGER PRIMARY KEY AUTOINCREMENT,"+ ColUserName+
" TEXT,"+ ColPassWord + " TEXT);";
privatestaticclassDatabaseHelperUserextendsSQLiteOpenHelper{
Contextcontext;
DatabaseHelperUser(Contextcontext){
super(context,DBName,null,DBVersion);
this.context=context;
}
@Override
publicvoidonCreate(SQLiteDatabasedb) {
db.execSQL(CreateTable);
Toast.makeText(context,"Table is created",Toast.LENGTH_LONG).show();
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb, intoldVersion, intnewVersion) {
db.execSQL("Drop table IF EXISTS "+ TableName);
onCreate(db);
}
}
publicDBManager(Contextcontext){
DatabaseHelperUserdb=newDatabaseHelperUser(context) ;
sqlDB=db.getWritableDatabase();
}
publiclongInsert(ContentValuesvalues){
longID= sqlDB.insert(TableName,"",values);
//could insert id is user id, or fail id is or equal 0
returnID;
}
//select username,Password from Logins where ID=1
publicCursorquery(String[] Projection,StringSelection,String[] SelectionArgs,StringSortOrder){
SQLiteQueryBuilderqb= newSQLiteQueryBuilder();
qb.setTables(TableName);
Cursorcursor=qb.query(sqlDB,Projection,Selection,SelectionArgs,null,null,SortOrder);
returncursor;
}
publicintDelete(StringSelection,String[] SelectionArgs){
intcount=sqlDB.delete(TableName,Selection,SelectionArgs);
returncount;
}
publicintUpdate(ContentValuesvalues,StringSelection,String[] SelectionArgs)
{
intcount=sqlDB.update(TableName,values,Selection,SelectionArgs);
returncount;
}
}