Knowee
Questions
Features
Study Tools

Create Android Application for performing the following operation onthe table customer (id, name, address, phone)(Use SQL database)i) Insert new customerii) Display details on toast

Question

Create Android Application for performing the following operation onthe table customer (id, name, address, phone)(Use SQL database)i) Insert new customerii) Display details on toast

🧐 Not the exact question you are looking for?Go ask a question

Solution

Creating an Android application that performs operations on a SQL database involves several steps. Here's a simplified version of how you might do it:

  1. Set up your Android Studio project: Start a new project in Android Studio. Choose "Empty Activity" and name your project.

  2. Add necessary dependencies: You will need to add Room Persistence Library dependencies in your build.gradle file. Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

  3. Define your Entity: Annotate a class with @Entity and define the table name to represent the customer table in the database.

@Entity(tableName = "customer")
public class Customer {
    @PrimaryKey
    private int id;
    private String name;
    private String address;
    private String phone;

    // getters and setters
}
  1. Create DAO (Data Access Object): Define a DAO interface that includes methods for each operation you want to perform on your data.
@Dao
public interface CustomerDao {
    @Insert
    void insert(Customer customer);

    @Query("SELECT * FROM customer WHERE id = :id")
    Customer getCustomer(int id);
}
  1. Create a Database instance: Define a database class that holds a reference to the database. The database class is annotated with @Database, lists all the entities, and includes an abstract method that has no arguments and returns the class that is annotated with @Dao.
@Database(entities = {Customer.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract CustomerDao customerDao();
}
  1. Perform operations: Now you can perform operations on the database. For example, to insert a new customer:
Customer customer = new Customer();
customer.setId(1);
customer.setName("John Doe");
customer.setAddress("123 Main St");
customer.setPhone("123-456-7890");

AppDatabase db = Room.databaseBuilder(getApplicationContext(),
        AppDatabase.class, "database-name").build();

db.customerDao().insert(customer);

And to display a customer's details on a toast:

Customer customer = db.customerDao().getCustomer(1);
Toast.makeText(getApplicationContext(), "Customer Name: " + customer.getName(), Toast.LENGTH_SHORT).show();

Remember to perform database operations off the main thread, or your app could throw an exception. Common practice is to use an AsyncTask, a Loader, or other such constructs.

This is a simplified example. In a real-world application, you would want to handle potential errors and edge cases.

This problem has been solved

Similar Questions

When you create a table in a database, you need to identify a suitable name. In this case, you can call it "customers". 2. Based on 'CM Mobiles' requirements the customers table will have three columns: ● username ● full name ● email address3. The customer username contains alphanumeric values such as: Custom001, Custom002, and Custom003. Notice here that the username is always nine characters in length, so choose the CHAR datatype as it allows for a fixed length of characters. In this case, choose 9 characters, no more or less. Therefore you can declare the username in the SQL statement using the following SQL syntax:

Create a package pkg_customer with specification and body. The package has a procedure named addCustomer to add new customer. The procedure will take customer id, name and address as parameter and inserts the customer to the t_customer table.PACKAGE pkg_customerPROCEDURE addCustomer(c_id   t_customer.cust_id%type,   c_name  t_customer.cust_name%type,   c_addr t_customer.cust_address%type)Package name : pkg_customerProcedure name : addCustomerParameter : c_id, c_name and c_addrNote:Do not change the package or procedure nameDo not change the argument count and orderDo not change the output text.You can query the table records using select statementDelimiter / is mandatory. If delimiter is missed, then while executing nothing will be displayedWhile executing, if you get WARNING: PROCEDURE CREATED WITH COMPILATION ERRORS, then use SHOW ERRORS to display the errors in the procedure.Instructions:1. Create the package successfully2. Once the package is created, check the functionality of the procedure using different anonymous block call.3. DO NOT submit the anonymous block. Submit only the CREATE PACKAGE query.Sample Input and Output:If the addCustomer procedure is called using 126, 'BNETON ELECTRONICS', '2215, Venus Avenue', then new records will be inserted into T_CUSTOMER table. You can query using SELECT and cross check it.

Create a package pkg_customer with specification and body. The package has a procedure named addCustomer to add new customer. The procedure will take customer id, name and address as parameter and inserts the customer to the t_customer table.PACKAGE pkg_customerPROCEDURE addCustomer(c_id   t_customer.cust_id%type,   c_name  t_customer.cust_name%type,   c_addr t_customer.cust_address%type)Package name : pkg_customerProcedure name : addCustomerParameter : c_id, c_name and c_addrNote:Do not change the package or procedure nameDo not change the argument count and orderDo not change the output text.You can query the table records using select statementDelimiter / is mandatory. If delimiter is missed, then while executing nothing will be displayedWhile executing, if you get WARNING: PROCEDURE CREATED WITH COMPILATION ERRORS, then use SHOW ERRORS to display the errors in the procedure.Instructions:1. Create the package successfully2. Once the package is created, check the functionality of the procedure using different anonymous block call.3. DO NOT submit the anonymous block. Submit only the CREATE PACKAGE query.Sample Input and Output:If the addCustomer procedure is called using 126, 'BNETON ELECTRONICS', '2215, Venus Avenue', then new records will be inserted into T_CUSTOMER table. You can query using SELECT and cross check it.SubmitSaveExecutePrevious Submission

Create a stored procedure. findStudents and write a query inside it to find a particular user(s) by their Department and display only the name and phone fields.The user name will be passed as an input to the procedure with data type VARCHAR(20).A Table named users is already been created.Sample TableNote :Table names are case-sensitive.Write only the logic for the procedure. The calling of the stored procedure and the creation of the tables are already written in the background.Input format :No console input.Output format :The output will display the names and phone numbers of the students of ECE and CIVIL departments.

Q4) Answer the following (Any Five) : [5 × 5 = 25]a) Explain architecture of Android.b) Write an application for the following Layout :Student InformationStud-idStud-nameStud-Markok cancelAfter clicking ok display detail on another activity.c) Write steps for Linking activities using intents.d) Write the use of onCreate( ), onUpgrade ( ) and getWritable Database( ) methods. With example.e) Write an application to send Email(Using - To, Subject and Message) Intent.f) Explain List View using adapter with the help of example.g) Differentiate between :i) Location based Services & Google Map.ii) Geocoding and Reverse geocoding.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.