Skip to content

Commit e8f2af1

Browse files
committed
Initial commit
0 parents  commit e8f2af1

40 files changed

+1240
-0
lines changed

Diff for: .gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

Diff for: .idea/gradle.xml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/misc.xml

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/modules.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/terminal.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Diff for: app/build.gradle

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
defaultConfig {
6+
applicationId "com.example.laksh.sqlapp"
7+
minSdkVersion 19
8+
targetSdkVersion 26
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation 'com.android.support:appcompat-v7:26.1.0'
24+
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
25+
testImplementation 'junit:junit:4.12'
26+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
27+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28+
}

Diff for: app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.laksh.sqlapp;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals( "com.example.laksh.sqlapp", appContext.getPackageName() );
25+
}
26+
}

Diff for: app/src/main/AndroidManifest.xml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.laksh.sqlapp">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
<provider
20+
android:authorities="com.example.laksh.sqlapp"
21+
android:name="com.example.laksh.sqlapp.DBContentProvider"/>
22+
</application>
23+
24+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.example.laksh.sqlapp;
2+
3+
import android.content.ContentValues;
4+
import android.content.Context;
5+
import android.database.Cursor;
6+
import android.database.sqlite.SQLiteDatabase;
7+
import android.database.sqlite.SQLiteOpenHelper;
8+
9+
/**
10+
* Created by laksh on 6/5/2018.
11+
*/
12+
13+
public class DatabaseHelper extends SQLiteOpenHelper {
14+
15+
//Initialize all the fields needed for database
16+
public static final String DATABASE_NAME = "Students.db";
17+
public static final String TABLE_NAME = "student_data";
18+
public static final String COL_1 = "ID";
19+
public static final String COL_2 = "Name";
20+
public static final String COL_3 = "Surname";
21+
public static final String COL_4 = "Marks";
22+
public static final String COL_5 = "Date";
23+
public static final String LBR = "(";
24+
public static final String RBR = ")";
25+
public static final String COM = ",";
26+
27+
//Just pass context of the app to make it simpler
28+
public DatabaseHelper(Context context) {
29+
super( context, DATABASE_NAME, null, 2 );
30+
31+
}
32+
33+
@Override
34+
public void onCreate(SQLiteDatabase db) {
35+
36+
//Creating table
37+
38+
db.execSQL( "create table " + TABLE_NAME + LBR + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT" + COM +
39+
COL_2 + " TEXT" + COM + COL_3 + " TEXT" + COM + COL_4 + " INTEGER" + COM + COL_5 + " INTEGER" +RBR );
40+
41+
// Another way of writing the CREATE TABLE query
42+
/* db.execSQL( "create table student_data (ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Surname TEXT," +
43+
"Marks INTEGER, Date TEXT)" );*/
44+
45+
}
46+
47+
@Override
48+
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
49+
50+
//Dropping old table
51+
db.execSQL( "DROP TABLE IF EXISTS " + TABLE_NAME);
52+
onCreate( db );
53+
54+
}
55+
56+
//Insert data in database
57+
public boolean instertData(String name, String surname, String marks, String date){
58+
59+
//Get the instance of SQL Database which we have created
60+
SQLiteDatabase db = getWritableDatabase();
61+
62+
//To pass all the values in database
63+
ContentValues contentValues = new ContentValues();
64+
contentValues.put( COL_2, name );
65+
contentValues.put( COL_3, surname );
66+
contentValues.put( COL_4, marks );
67+
contentValues.put( COL_5, date);
68+
69+
long result = db.insert( TABLE_NAME, null, contentValues );
70+
71+
if(result == -1)
72+
return false;
73+
else
74+
return true;
75+
}
76+
77+
//Cursor class is used to move around in the database
78+
public Cursor getData(){
79+
80+
//Get the data from database
81+
SQLiteDatabase db = getWritableDatabase();
82+
Cursor res = db.rawQuery( "select * from " + TABLE_NAME, null );
83+
return res;
84+
}
85+
86+
//Update fields of database using ID (Unique identifier)
87+
public boolean updateData(String id, String name, String surname, String marks, String date){
88+
89+
SQLiteDatabase db = getWritableDatabase();
90+
91+
ContentValues contentValues = new ContentValues( );
92+
// When you want to update only name field
93+
if(surname.equals( "" ) && marks.equals( "" )){
94+
contentValues.put( COL_1, id );
95+
contentValues.put( COL_2, name );
96+
contentValues.put( COL_5, date);
97+
}
98+
// When you want to update only surname field
99+
if(name.equals( "" ) && marks.equals( "" )){
100+
contentValues.put( COL_1, id );
101+
contentValues.put( COL_3, surname );
102+
contentValues.put( COL_5, date);
103+
}
104+
// When you want to update only marks field
105+
if(name.equals( "" ) && surname.equals( "" )){
106+
contentValues.put( COL_1, id );
107+
contentValues.put( COL_4, marks );
108+
contentValues.put( COL_5, date);
109+
}
110+
// When you want to update name and surname field
111+
if(marks.equals( "" ) && !name.isEmpty() && !surname.isEmpty()){
112+
contentValues.put( COL_1, id );
113+
contentValues.put( COL_2, name );
114+
contentValues.put( COL_3, surname );
115+
contentValues.put( COL_5, date);
116+
}
117+
// When you want to update marks and surname field
118+
if(name.isEmpty() && !marks.isEmpty() && !surname.isEmpty()){
119+
contentValues.put( COL_1, id );
120+
contentValues.put( COL_3, surname );
121+
contentValues.put( COL_4, marks );
122+
contentValues.put( COL_5, date);
123+
}
124+
// When you want to update name and marks field
125+
if(surname.isEmpty() && !name.isEmpty() && !marks.isEmpty()){
126+
contentValues.put( COL_1, id );
127+
contentValues.put( COL_2, name );
128+
contentValues.put( COL_4, marks );
129+
contentValues.put( COL_5, date);
130+
}
131+
// When you want to update every data field
132+
if(!id.isEmpty() && !name.isEmpty() && !surname.isEmpty() && !marks.isEmpty()){
133+
contentValues.put( COL_1, id );
134+
contentValues.put( COL_2, name );
135+
contentValues.put( COL_3, surname );
136+
contentValues.put( COL_4, marks );
137+
contentValues.put( COL_5, date);
138+
}
139+
140+
// UPDATE query
141+
db.update( TABLE_NAME, contentValues, "ID = ?", new String[]{id} );
142+
return true;
143+
}
144+
145+
//Delete data from the databse using ID (Primary Key)
146+
public Integer deleteData(String id){
147+
148+
SQLiteDatabase db = getWritableDatabase();
149+
return db.delete( TABLE_NAME, "ID = ?", new String [] {id} );
150+
}
151+
}

0 commit comments

Comments
 (0)