A simple sqlite example in Sketchware
In this example, the user can save details of pupils (name, age and place) in sqlite database in android app, and view the data in a ListView.
Create a new project in Sketchware.
In Options/Configuration, click on Java/Kotlin manager. Create a new Java class file with name DbHandler. Put following codes as contents of this DbHandler.java file.
In the above code, change package name to your own package name. This code is for adding three String variables in the database (name, age and place). If you want more number of variables or different types of variables, you can change the code accordingly.
In main.xml, add three edittext fields (for entering name, age and place), add a Button (to add data to database), add a ListView to display the data. Add a TextView to display the count of data.
In Logic, add a more block and declare database.
Create a more block named updateList and put following blocks as shown in image below.
This block retrieves data from database and displays it in ListView. The codes used in the more blocks are:
count = dbhandler.getCount();
and
dbtext = dbhandler.getText((int)n, 0) + ", " + dbhandler.getText((int)n, 1) + " years, " + dbhandler.getText((int)n, 2);
In onCreate event, put following codes:
dbhandler = new DbHandler(MainActivity.this);
After this add the updateList block to retrieve the data from database and display it in ListView.
In on Button Click event, use blocks as shown in image below. This will add data from EditText to the database and update the ListView.
The codes used here is,
dbhandler.addNewPupil(name, age, place);
Now run the app and check how it works.
Check the video below for more insight.
Comments
Post a Comment