Android How to Upload Data to Database
SQLite is another data storage available in Android where we can store data in the user's device and tin can employ information technology whatsoever time when required. In this commodity, we will accept a look at creating an SQLite database in the Android app and adding data to that database in the Android app. This is a serial of 4 manufactures in which we are going to perform the basic Grime (Create, Read, Update, and Delete) performance with SQLite Database in Android. We are going to cover the post-obit 4 articles in this series:
- How to Create and Add Information to SQLite Database in Android?
- How to Read Information from SQLite Database in Android?
- How to Update Data to SQLite Database in Android?
- How to Delete Information in SQLite Database in Android?
What is SQLite Database?
SQLite Database is an open up-source database provided in Android which is used to store data inside the user'south device in the form of a Text file. We tin perform and then many operations on this information such every bit adding new data, updating, reading, and deleting this information. SQLite is an offline database that is locally stored in the user'due south device and we practice not have to create any connection to connect to this database.
How Data is Being Stored in the SQLite Database?
Information is stored in the SQLite database in the form of tables. When we stored this data in our SQLite database it is arranged in the class of tables that are similar to that of an excel sheet. Beneath is the representation of our SQLite database which we are storing in our SQLite database.
Of import Methods in SQLite Database
Below is the several important methods that we will exist using in this SQLite database integration in Android.
Method | Description |
---|---|
getColumnNames() | This method is used to get the Array of column names of our SQLite table. |
getCount() | This method will render the number of rows in the cursor. |
isClosed() | This method returns a Boolean value when our cursor is closed. |
getColumnCount() | This method returns the total number of columns nowadays in our tabular array. |
getColumnName(int columnIndex) | This method will render the proper name of the column when we passed the index of our column in it. |
getColumnIndex(Cord columnName) | This method will return the alphabetize of our column from the proper noun of the column. |
getPosition() | This method will render the current position of our cursor in our table. |
What we are going to build in this article?
Nosotros volition exist edifice a simple application in which we volition be calculation data to the SQLite database. Nosotros will exist creating a database for adding course name, grade clarification, course elapsing, and form tracks. We will exist saving all this data in our SQLite database. A sample video is given below to go an idea about what we are going to do in this article. Notation that nosotros are going to implement this project using theCoffee language.
Footstep by Stride Implementation
Step ane: Create a New Projection
To create a new projection in Android Studio delight refer to How to Create/Offset a New Project in Android Studio. Notation that select Java as the programming language.
Stride 2: Adding permissions to access the storage in the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml and add the below code to information technology.
XML
<
uses-permission
android:proper noun
=
"android.permission.READ_EXTERNAL_STORAGE"
/>
Step iii: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below lawmaking to that file. Below is the code for the activity_main.xml file.
XML
<?
xml
version
=
"ane.0"
encoding
=
"utf-8"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
tools:context
=
".MainActivity"
>
<
EditText
android:id
=
"@+id/idEdtCourseName"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_margin
=
"10dp"
android:hint
=
"Enter course Name"
/>
<
EditText
android:id
=
"@+id/idEdtCourseDuration"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_margin
=
"10dp"
android:hint
=
"Enter Course Duration"
/>
<
EditText
android:id
=
"@+id/idEdtCourseTracks"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_margin
=
"10dp"
android:hint
=
"Enter Course Tracks"
/>
<
EditText
android:id
=
"@+id/idEdtCourseDescription"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_margin
=
"10dp"
android:hint
=
"Enter Grade Description"
/>
<
Button
android:id
=
"@+id/idBtnAddCourse"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_margin
=
"10dp"
android:text
=
"Add Grade"
android:textAllCaps
=
"false"
/>
</
LinearLayout
>
Pace 4: Creating a new Java class for performing SQLite operations
Navigate to the app > java > your app's packet name > Right-click on information technology > New > Java course and name it as DBHandler and add the below lawmaking to information technology. Comments are added inside the code to understand the lawmaking in more than item.
Coffee
import
android.content.ContentValues;
import
android.content.Context;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteOpenHelper;
public
class
DBHandler
extends
SQLiteOpenHelper {
private
static
terminal
String DB_NAME =
"coursedb"
;
individual
static
final
int
DB_VERSION =
1
;
private
static
final
String TABLE_NAME =
"mycourses"
;
private
static
final
Cord ID_COL =
"id"
;
private
static
terminal
String NAME_COL =
"name"
;
individual
static
final
String DURATION_COL =
"elapsing"
;
individual
static
last
Cord DESCRIPTION_COL =
"description"
;
private
static
final
Cord TRACKS_COL =
"tracks"
;
public
DBHandler(Context context) {
super
(context, DB_NAME,
cipher
, DB_VERSION);
}
@Override
public
void
onCreate(SQLiteDatabase db) {
String query =
"CREATE Tabular array "
+ TABLE_NAME +
" ("
+ ID_COL +
" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME_COL +
" TEXT,"
+ DURATION_COL +
" TEXT,"
+ DESCRIPTION_COL +
" TEXT,"
+ TRACKS_COL +
" TEXT)"
;
db.execSQL(query);
}
public
void
addNewCourse(Cord courseName, String courseDuration, String courseDescription, String courseTracks) {
SQLiteDatabase db =
this
.getWritableDatabase();
ContentValues values =
new
ContentValues();
values.put(NAME_COL, courseName);
values.put(DURATION_COL, courseDuration);
values.put(DESCRIPTION_COL, courseDescription);
values.put(TRACKS_COL, courseTracks);
db.insert(TABLE_NAME,
nothing
, values);
db.shut();
}
@Override
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion) {
db.execSQL(
"Drop Table IF EXISTS "
+ TABLE_NAME);
onCreate(db);
}
}
Step five: Working with the MainActivity.coffee file
Become to the MainActivity.coffee file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to sympathise the lawmaking in more than item.
Coffee
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
private
EditText courseNameEdt, courseTracksEdt, courseDurationEdt, courseDescriptionEdt;
private
Button addCourseBtn;
private
DBHandler dbHandler;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseNameEdt = findViewById(R.id.idEdtCourseName);
courseTracksEdt = findViewById(R.id.idEdtCourseTracks);
courseDurationEdt = findViewById(R.id.idEdtCourseDuration);
courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);
addCourseBtn = findViewById(R.id.idBtnAddCourse);
dbHandler =
new
DBHandler(MainActivity.
this
);
addCourseBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
String courseName = courseNameEdt.getText().toString();
String courseTracks = courseTracksEdt.getText().toString();
Cord courseDuration = courseDurationEdt.getText().toString();
String courseDescription = courseDescriptionEdt.getText().toString();
if
(courseName.isEmpty() && courseTracks.isEmpty() && courseDuration.isEmpty() && courseDescription.isEmpty()) {
Toast.makeText(MainActivity.
this
,
"Delight enter all the data.."
, Toast.LENGTH_SHORT).bear witness();
render
;
}
dbHandler.addNewCourse(courseName, courseDuration, courseDescription, courseTracks);
Toast.makeText(MainActivity.
this
,
"Form has been added."
, Toast.LENGTH_SHORT).show();
courseNameEdt.setText(
""
);
courseDurationEdt.setText(
""
);
courseTracksEdt.setText(
""
);
courseDescriptionEdt.setText(
""
);
}
});
}
}
At present run your app and see the output of the app.
Output:
After successfully executed the code enter the required information inside the EditText. Virtually chiefly if you want to know How to View and Locate SQLite Database in Android Studio then please refer to this commodity . And you tin run into beneath this is how the data stored in the SQLite database.
Beneath is the complete project file structure later on performing the create and add operation:
Source: https://www.geeksforgeeks.org/how-to-create-and-add-data-to-sqlite-database-in-android/
0 Response to "Android How to Upload Data to Database"
Post a Comment