Categories: ANDROID APP

The right way to fetch information from SQLite database and put that in a PDF File in your Android App? – supply code



This video exhibits the steps to create a SQLite database from scratch. Then it exhibits how one can write (Insert or replace) the information within the database. Additional, it exhibits the code to question the database and fetch the required information from the database. Lastly, it exhibits how you can use the fetched information to show it on a textual content view within the App’s structure and likewise to create a PDF file to write down that textual content within the PDF.

We hope you want this video. For any question, strategies or appreciations we might be glad to listen to from you at: programmerworld1990@gmail.com or go to us at: https://programmerworld.co

Supply 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/

bundle com.instance.mysqlitedbtopdffile;

import android.content material.ContentValues;
import android.content material.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.Atmosphere;
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 {

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

personal SQLiteDatabase sqLiteDatabase;

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

attempt {
sqlLiteDBHandler = new mySQLiteDBHandler(this,”PDFDatabase”, null,1);
sqLiteDatabase = sqlLiteDBHandler.getWritableDatabase();
sqLiteDatabase.execSQL(“CREATE TABLE PDFTable(SerialNumber TEXT, Textual content 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(“Textual content”, editTextInsert.getText().toString());
sqLiteDatabase.insert(“PDFTable”,null,contentValues);
sqLiteDatabase.replace(“PDFTable”, contentValues, null,null);
}

public void CreatePDF(View view){
String question = “Choose Textual content from PDFTable the place SerialNumber=” + editTextSerialNumberFetch.getText().toString();
Cursor cursor = sqLiteDatabase.rawQuery(question,null);
attempt {
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.Web page web page = pdfDocument.startPage(pageInfo);
web page.getCanvas().drawText(cursor.getString(0),10, 25, new Paint());
pdfDocument.finishPage(web page);
String filePath = Atmosphere.getExternalStorageDirectory().getPath()+”/Obtain/”+editTextSerialNumberFetch.getText().toString()+”.pdf”;
File file = new File(filePath);
attempt {
pdfDocument.writeTo(new FileOutputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
pdfDocument.shut();
}
}

————–

bundle com.instance.mysqlitedbtopdffile;

import android.content material.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 title, @Nullable SQLiteDatabase.CursorFactory manufacturing facility, int model) {
tremendous(context, title, manufacturing facility, model);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}

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

source

linda

Recent Posts

Essential Tips for Successful Condo Renovation

Before embarking on the renovation journey, it's crucial to comprehend what the process involves. Condo…

20 hours ago

Tips on how to Maximize Your Glory Casino Benefit

Hey there, fellow casino lover! If you're on the hunt to help make the most…

1 day ago

How to Maximize Wins on Glory Casino

Hey there, fellow gaming enthusiast! Are you ready to dive into the exciting world of…

2 days ago

Best Private Universities to Consider throughout Turkey

Hey there! So you're interested in studying in Turkey? That's a fantastic choice! Egypt is…

2 days ago

Best Benefits of Harbor City Hemp

Welcome to the world of Harbor Town Hemp! If you're curious about exactly how hemp…

4 days ago

Common Issues with Gear Pumps and Solutions

Before we discuss the issues, let's quickly review what a gear pump is. A gear…

4 days ago