Android SQLite Example and Interface Use
Hello there. In this article, I’ll reiterate the use of the database that some of the testers said they got errors, and I will also refer to the use of SQLite.
I’m skipping steps to create projects. The application will do exactly as follows. For a person to enter basic information such as model, name will be saved
Screen Design
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.taha.sqliteexample.MainActivity" android:orientation="vertical"> <EditText android:hint="Name" android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:hint="Surname" android:id="@+id/surname" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:hint="Age" android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:onClick="SavePerson" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save"/> </LinearLayout> |
Here we enter information for a person. As you click the Save button, these values will be added to the database.
Create a Database
Create a class called “Database” by right-clicking on the src folder and calling new-class. Edit your classroom as follows.
He underlined it because we did not import the package. Press ALT + ENTER, or add and say import by holding on to the cursor.
I’m telling these things for. If I give you the finished form of the class directly, do not waste time typing and understand how it is. Also, there are a lot of big and small letters here.
I will not tell you again what you are doing. But I will briefly mention it. You can learn what these mean from the previous database lessons. After filling in these fields, the view looks like this.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Database extends SQLiteOpenHelper { private static final String DATABASE_NAME="Persons"; private static final int VERSION=1; public Database(Context context) { super(context,DATABASE_NAME,null,VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("CREATE TABLE Person(name TEXT,surname TEXT,age INTEGER)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { sqLiteDatabase.execSQL("DROP TABLE IF EXIST Person"); onCreate(sqLiteDatabase); } } |
Here we briefly create a database named “persons”, a table named “persons”, and entered the table fields. Now that the database creation is over, let’s go to the actual code.
Matching Tools with Id Values
We will match the id values for the tools we have defined on the screen, such as button, TexView. Scroll down to the MainActivity.java class and type the following code.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class MainActivity extends AppCompatActivity { EditText name; EditText surname; EditText age; private Database db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name=(EditText)findViewById(R.id.name); surname=(EditText)findViewById(R.id.surname); age=(EditText)findViewById(R.id.age); db=new Database(this); } public void SavePerson(View view) { } } |
As you see, we performed the above set operations. There is no id for defining the onClick method for the Save button.
If something is underlined, you have to import it. You can add it by pressing ALT + ENTER.
Now let’s write what will be done when the button is clicked.
Save Button Codes
We created the click event using the onClick method. Let’s write the things that will happen when you click here. After writing, the final view of the codes looks like this:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
public class MainActivity extends AppCompatActivity { EditText name; EditText surname; EditText age; private Database db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name=(EditText)findViewById(R.id.name); surname=(EditText)findViewById(R.id.surname); age=(EditText)findViewById(R.id.age); db=new Database(this); } public void SavePerson(View view) { try { AddRecord(name.getText().toString(),surname.getText().toString(),Integer.parseInt(age.getText().toString())); } catch (Exception ex) { } finally { db.close(); } } private void AddRecord(String name,String surname,int age) { } } |
Click on the button to enter the AddRecord () method.
Now fill in the AddRecord () method.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public class MainActivity extends AppCompatActivity { EditText name; EditText surname; EditText age; private Database db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name=(EditText)findViewById(R.id.name); surname=(EditText)findViewById(R.id.surname); age=(EditText)findViewById(R.id.age); db=new Database(this); } public void SavePerson(View view) { try { AddRecord(name.getText().toString(),surname.getText().toString(),Integer.parseInt(age.getText().toString())); } catch (Exception ex) { } finally { db.close(); } } private void AddRecord(String name,String surname,int age) { SQLiteDatabase database=db.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("name",name); cv.put("surname",surname); cv.put("age",age); database.insertOrThrow("Person",null,cv); database.close(); } } |
Now let’s run our practice and give it a try.
Displaying the Information in the Database with SQLite
Now we can see the values we added to the database with the SQLite program. I downloaded the Db Browser for SQlite program. Now we need to take our database to the desktop first. Do this for that.
Download it by clicking on the appropriate link at the site and perform the installation.
Run your app. So you work for the emulator. Not without this. Fully run and view your project. Later,
Do not forget the project will be open !!
- Step 1 Click on the File Explorer tab
- Step 2 From here, click on data> data> project name options from the right hand side to go to the folder where your database is located.
- Step 3 Press button on the right (pull a file from the device). Then save the database on your desktop.
Database Open
Let’s look at the program we downloaded.
- Click Open Database
Select the database file we saved above and open it
Then, click on executeSQL. Let’s take a query to our database and see our result
Get Record
I’ll just take the name, you can get everything you want
First, we add the design TextView and Button
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.taha.sqliteexample.MainActivity" android:orientation="vertical"> <EditText android:hint="Name" android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:hint="Surname" android:id="@+id/surname" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:hint="Age" android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:onClick="SavePerson" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save"/> <Button android:onClick="GetRecord" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get"/> <TextView android:id="@+id/get_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |
After doing this, we organize the MainActivity part like this
- File:src/MainActivity.class
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
public class MainActivity extends AppCompatActivity { EditText name; EditText surname; EditText age; private Database db; TextView get_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name=(EditText)findViewById(R.id.name); surname=(EditText)findViewById(R.id.surname); age=(EditText)findViewById(R.id.age); get_name=(TextView) findViewById(R.id.get_name); db=new Database(this); } public void SavePerson(View view) { try { AddRecord(name.getText().toString(),surname.getText().toString(),Integer.parseInt(age.getText().toString())); } catch (Exception ex) { Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show(); } finally { db.close(); } } private void AddRecord(String name,String surname,int age) { SQLiteDatabase database=db.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("name",name); cv.put("surname",surname); cv.put("age",age); database.insertOrThrow("Person",null,cv); database.close(); } public void GetRecord(View view) { String selectQuery = "SELECT * FROM Person"; // query string TextView textView=new TextView(this); SQLiteDatabase database=db.getReadableDatabase(); ContentValues cv=new ContentValues(); Cursor cursor= database.rawQuery(selectQuery,null); // select query if(cursor.moveToFirst()) { do { get_name.setText(cursor.getString(cursor.getColumnIndex("name")));// record enter to text }while(cursor.moveToNext()); } } } |
Demo
You can print it on the name screen by clicking on the Get button.
the function that performs the operation has already been shared “GetRecord()”
if you have question do not forget to write from the chat button next to it or from the comment
Recent Comments