Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
androidTutorialForBeginners/DatabaseHelper.java at master · HRashedy/androidTutorialForBeginners · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
HRashedy
androidTutorialForBeginners
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
androidTutorialForBeginners
/
DatabaseHelper.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
112 lines (87 loc) · 3.46 KB
master
Breadcrumbs
androidTutorialForBeginners
/
DatabaseHelper.java
Copy path
Top
File metadata and controls
Code
Blame
112 lines (87 loc) · 3.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
private
SQLiteDatabase
db
;
static
final
String
DATABASE_NAME
=
"College"
;
static
final
String
STUDENTS_TABLE_NAME
=
"students"
;
static
final
int
DATABASE_VERSION
=
1
;
static
final
String
CREATE_DB_TABLE
=
" CREATE TABLE IF NOT EXISTS "
+
STUDENTS_TABLE_NAME
+
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+
" name TEXT NOT NULL, "
+
" age TEXT NOT NULL);"
;
/**
* Helper class that actually creates and manages
* the provider's underlying data repository.
*/
private
static
class
DatabaseHelper
extends
SQLiteOpenHelper
{
DatabaseHelper
(
Context
context
){
super
(
context
,
DATABASE_NAME
,
null
,
DATABASE_VERSION
);
}
@
Override
public
void
onCreate
(
SQLiteDatabase
db
)
{
db
.
execSQL
(
CREATE_DB_TABLE
);
}
@
Override
public
void
onUpgrade
(
SQLiteDatabase
db
,
int
oldVersion
,
int
newVersion
) {
db
.
execSQL
(
"DROP TABLE IF EXISTS "
+
STUDENTS_TABLE_NAME
);
onCreate
(
db
);
}
}
//user class
DatabaseHelper
dbHelper
=
new
DatabaseHelper
(
context
);
db
=
dbHelper
.
getWritableDatabase
();
public
long
insert
(
Uri
uri
,
ContentValues
values
)
{
long
rowID
=
db
.
insert
(
STUDENTS_TABLE_NAME
,
""
,
values
);
/*
if (rowID > 0)
{
//added
}*/
return
rowID
;
}
//select
//projection coloum
String
[]
projection
={
"name"
};
public
Cursor
query
(
Uri
uri
,
String
[]
projection
,
String
selection
,
String
[]
selectionArgs
,
String
sortOrder
)
{
SQLiteQueryBuilder
qb
=
new
SQLiteQueryBuilder
();
qb
.
setTables
(
STUDENTS_TABLE_NAME
);
//get all
// qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
//get one
// qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
Cursor
c
=
qb
.
query
(
db
,
projection
,
selection
,
selectionArgs
,
null
,
null
,
sortOrder
);
return
c
;
}
public
int
delete
(
Uri
uri
,
String
selection
,
String
[]
selectionArgs
)
{
int
count
=
db
.
delete
(
STUDENTS_TABLE_NAME
,
selection
,
selectionArgs
);
return
count
;
}
public
int
update
(
Uri
uri
,
ContentValues
values
,
String
selection
,
String
[]
selectionArgs
)
{
int
count
=
0
;
count
=
db
.
update
(
STUDENTS_TABLE_NAME
,
values
,
selection
,
selectionArgs
);
// count = db.update(STUDENTS_TABLE_NAME, values, _ID + " = " + uri.getPathSegments().get(1) +
// (!TextUtils.isEmpty(selection) ? " AND (" +selection + ')' : ""), selectionArgs);
return
count
;
}
// Add a new student record
ContentValues
values
=
new
ContentValues
();
// insert value
values
.
put
(
StudentsProvider
.
NAME
,
"a"
);
values
.
put
(
StudentsProvider
.
Age
,
"b"
);
// define the play to insert the values in
insert
(
values
);
//select
Cursor
c
=
query
(
students
,
null
,
null
,
null
,
"name"
);
// move through all items
if
(
c
.
moveToFirst
()) {
do
{
// load the record name and age and id
Toast
.
makeText
(
this
,
c
.
getString
(
c
.
getColumnIndex
(
StudentsProvider
.
_ID
)) +
", "
+
c
.
getString
(
c
.
getColumnIndex
(
StudentsProvider
.
NAME
)) +
", "
+
c
.
getString
(
c
.
getColumnIndex
(
StudentsProvider
.
Age
)),
Toast
.
LENGTH_SHORT
).
show
();
}
while
(
c
.
moveToNext
());
You can’t perform that action at this time.