How to fetch data from SQLite database and put that in a PDF File in your Android App? – source code



How to fetch data from SQLite database and put that in a PDF File in your Android App? – source code

How to fetch data from SQLite database and put that in a PDF File in your Android App? - source code

This video shows the steps to create a SQLite database from scratch. Then it shows how one can write (Insert or update) the data in the database. Further, it shows the code to query the database and fetch the required data from the database. Finally, it shows how to use the fetched data to display it on a text view in the App’s layout and also to create a PDF file to write that text in the PDF.

We hope you like this video. For any query, suggestions or appreciations we will be glad to hear from you at: [email protected] or visit us at: https://programmerworld.co

Source Code at:

https://programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/

package com.example.mysqlitedbtopdffile;

import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

public class MainActivity extends AppCompatActivity {

private mySQLiteDBHandler sqlLiteDBHandler;
private EditText editTextSerialNumberInsert;
private EditText editTextSerialNumberFetch;
private EditText editTextInsert;
private TextView textViewDisplay;

private SQLiteDatabase sqLiteDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

try {
sqlLiteDBHandler = new mySQLiteDBHandler(this,”PDFDatabase”, null,1);
sqLiteDatabase = sqlLiteDBHandler.getWritableDatabase();
sqLiteDatabase.execSQL(“CREATE TABLE PDFTable(SerialNumber TEXT, Text TEXT)”);
}
catch (Exception e){
e.printStackTrace();
}
editTextInsert = findViewById(R.id.editText2);
editTextSerialNumberInsert = findViewById(R.id.editText);
editTextSerialNumberFetch = findViewById(R.id.editText3);
textViewDisplay = findViewById(R.id.textView);
}

public void InsertUpdatedButton(View view){
ContentValues contentValues = new ContentValues();
contentValues.put(“SerialNumber”, editTextSerialNumberInsert.getText().toString());
contentValues.put(“Text”, editTextInsert.getText().toString());
sqLiteDatabase.insert(“PDFTable”,null,contentValues);
sqLiteDatabase.update(“PDFTable”, contentValues, null,null);
}

public void CreatePDF(View view){
String query = “Select Text from PDFTable where SerialNumber=” + editTextSerialNumberFetch.getText().toString();
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
try {
cursor.moveToFirst();
textViewDisplay.setText(cursor.getString(0));
}
catch (Exception e){
e.printStackTrace();
textViewDisplay.setText(“”);
return;
}

PdfDocument pdfDocument = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600,1).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
page.getCanvas().drawText(cursor.getString(0),10, 25, new Paint());
pdfDocument.finishPage(page);
String filePath = Environment.getExternalStorageDirectory().getPath()+”/Download/”+editTextSerialNumberFetch.getText().toString()+”.pdf”;
File file = new File(filePath);
try {
pdfDocument.writeTo(new FileOutputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
pdfDocument.close();
}
}

————–

package com.example.mysqlitedbtopdffile;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

public class mySQLiteDBHandler extends SQLiteOpenHelper {
public mySQLiteDBHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}

Comments are closed.