Nothing Special   »   [go: up one dir, main page]

SQL Program Android

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

SQL program

Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>

<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a name"
android:layout_margin="16dp"/>

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Name"

android:layout_marginTop="8dp"/>

<Button
android:id="@+id/displayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display All Data"

android:layout_marginTop="8dp"/>

<TextView
android:id="@+id/displayTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""

android:layout_marginTop="16dp"/>

</LinearLayout>

Java
package com.example.uefclass;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private EditText nameEditText;
private DatabaseHelper databaseHelper;
private TextView displayTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditText);
displayTextView = findViewById(R.id.displayTextView);
Button addButton = findViewById(R.id.addButton);
Button displayButton = findViewById(R.id.displayButton);

databaseHelper = new DatabaseHelper(this);

addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addName();
}
});

displayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayAllData();
}
});
}

private void addName() {


String name = nameEditText.getText().toString().trim();

if (!name.isEmpty()) {
long result = databaseHelper.insertName(name);

if (result != -1) {
Toast.makeText(this, "Name added successfully",
Toast.LENGTH_SHORT).show();
nameEditText.setText("");
} else {
Toast.makeText(this, "Failed to add name",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Please enter a name",
Toast.LENGTH_SHORT).show();
}
}
private void displayAllData() {
String allData = databaseHelper.getAllData();
displayTextView.setText(allData);
}
}

Database Helper
package com.example.uefclass;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "name_database.db";


private static final String TABLE_NAME = "name_table";
public static final String COL_ID = "ID";
public static final String COL_NAME = "NAME";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = "CREATE TABLE " + TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_NAME + " TEXT)";
db.execSQL(createTableQuery);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public long insertName(String name) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);

try {
return db.insert(TABLE_NAME, null, contentValues);
} catch (SQLException e) {
e.printStackTrace();
return -1;
} finally {
db.close();
}
}
public String getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
StringBuilder data = new StringBuilder();

if (cursor.getCount() == 0) {
data.append("No data available.");
} else {
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(COL_ID));
String name =
cursor.getString(cursor.getColumnIndex(COL_NAME));
data.append("ID: ").append(id).append(", Name:
").append(name).append("\n");
}
}

cursor.close();
db.close();
return data.toString();
}
}

You might also like