How to fetch data from SQLite database and put that in a PDF File in your Android App? - source code
Вставка
- Опубліковано 12 лис 2024
- 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: programmerworld1990@gmail.com or visit us at: programmerworl...
Source Code at:
programmerworl...
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) {
}
}
Great Sir!!
Sir I have a Arraylist, it has 100+ lines Data. I want to print it or make a PDF, as per page 20 lines...How to make PDF..Please sir help us...
Accumulate all your texts in a string variable and then write it in the pdf page as shown in this video.
Thus, something like below can be done:
String stringText=" ";
// Do the below for all the lines iteratively
String query = “Select Text from PDFTable where SerialNumber=” + editTextSerialNumberFetch.getText().toString();
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
cursor.moveToFirst();
stringText = stringText + cursor.getString(0) + "
";
// Till this line.
// Once the stringText variable is prepared, just write on the pdf page as below:
PdfDocument pdfDocument = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600,1).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
page.getCanvas().drawText(stringText,10, 25, new Paint());
pdfDocument.finishPage(page);
String filePath = Environment.getExternalStorageDirectory().getPath()+”/Download/”+editTextSerialNumberFetch.getText().toString()+”.pdf”;
File file = new File(filePath);
pdfDocument.writeTo(new FileOutputStream(file));
pdfDocument.close();
// End
Hope the above helps.
Part of above code is taken from below page:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Cheers
Programmer World
programmerworld.co
-
Hello, I've got a little problem. When I try to create my pdf file it says "open failed: EACCES (Permission denied)" in logs, even if I give it the permission to read/write. Any idea ?
Suppose I'm displaying pdf from asset, how can I store the current reading page with SQLite so that anytime I want to read the pdf, it will start from where I ended reading.
It depends on the application you are using to read the PDF. If the application is giving back the information of the last read page number then just store it in the SQLite database.
However, I will recommend to use Shared Preference variables to store this information in your App's data. One such use case is shown in the below page:
programmerworld.co/android/how-to-create-walking-step-counter-app-using-accelerometer-sensor-and-shared-preference-in-android/
Cheers
Programmer World
programmerworld.co
-
Hi Sir, as u r giving manually page number but what I do for dynamic pages? I don't want to give pages number it shows multiple pages according to data. Will, you plz elaborate on it?
I think in that case just take take the 3rd argument in some variable form.
For example replace the hardcoded '1' in the below
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600,1).create();
with variable 'PageNumber' (of type int) as shown below:
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, PageNumber).create();
This should resolve the issue of taking the page numbers dynamically.
However, if you do not want to put the page numbers at all then I don't think it is possible as of now.
For reference, the source code of this project is available in the below link:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Cheers
Programmer World
programmerworld.co/
-
can you help me for generate pdf from dynamical database mysql?
I think the concept to work with MySQL database will remain same as shown in this video for SQLite database.
The only difference will be to get the right drivers to connect to the database. In my below video it is shown how to connect to the MS SQL database:
ua-cam.com/video/MnmEXqfV5BU/v-deo.html
One can try the same approach for My SQL database.
Cheers
Programmer World
programmerworld.co
-
Hello sir, no error at running, but I can't see the created PDF . It is not displaied in another window, as in your tutorial. Where else can I search for this PDF, to see it exists ?
Tks !
Check in the path (folder) where you are creating the PDF. In this video the file path used is:
String filePath = Environment.getExternalStorageDirectory().getPath()+”/Download/”+editTextSerialNumberFetch.getText().toString()+”.pdf”;
You can refer to the below link for complete source code of this video:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Good Luck
Programmer World
programmerworld.co
-
Hello sir, I have data of 5 to 6 department in CSV file and i have to fatch it in listview or in recyclerview and from each department I hve to select some user and then i hve to create PDF. How it's possible ?
I hope you we help me.
Every department has defferent CSV file.
Hello sir my question is if user enter a number in sign up activity then how could we call on that number by fetching it from sqlite database
Simply retrieve the phone number from the SQLite database (either using the number used during the sign-up or index of the number in the database or any other reference which can be used).
And then use the retrieved number to make the call. Below link should help:
programmerworld.co/android/how-to-call-or-dial-a-phone-number-from-your-own-custom-android-app-complete-source-code/
Cheers
Programmer World
programmerworld.co
-
@@ProgrammerWorld sir I want to fetch the number get by user in sign up activity now there is nothing to add in xml of phone call activity just when the activity open through intent it make call
@@ProgrammerWorld I use db.fetch of class cursor in databasehelper class how to implement that fetch function in phone activity
hi, thanks for your video, i did what u said in the video and its working well, but when im tryin to do it with all the data from a sqlite database is not working i mean with more columns and rows
It has to work with all the data from various columns of the Sqlite database.
Just ensure that in the below line of the code, cursor.getString() is referring to the correct data type in the database:
page.getCanvas().drawText(cursor.getString(0),10, 25, new Paint());
If still you have issues then please copy the error message you are getting.
Complete source code of this video can be referred at:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Good Luck
Programmer World
programmerwold.co
-
@@ProgrammerWorld thanks for your answer, and yeah It works with all de columns , but only with one line or row and i wanted to extract to the PDF múltiple lines and columns like a list
great video bro
How to add images from URL to pdf from recyclerview..
Plz help me...
You can check my below video:
ua-cam.com/video/sOKV3iHl5aM/v-deo.html
In this video, I have shown how to put images in the PDF file.
Hope this helps.
Cheers
Programmer World
programmerworld.co
-
sir,how to connect it to phpmyadmin and extract data from mysql database and display it in pdf??
Watch my below video. It may help.
ua-cam.com/video/MnmEXqfV5BU/v-deo.html
Good Luck
Programmer World
programmerworld.co
-
extends _SQLiteopen not able to give input
Kindly use "extends SQLiteOpenHelper" and check. It should work.
Complete source code of this tutorial can be found in the below link:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Cheers
Programmer World
programmerworld.co
-
Sir , how to create doc files in android studio
Hi Santosh,
Thanks for your suggestion. I will make a tutorial as per your requirement. Will let you know once I post the video.
Cheers
Programmer World!
programmerworld.co
-
@@ProgrammerWorld Hi sir, then how about to create excel file?
@@irfanjasni9372 Thanks for your message. I will try to prepare a tutorial to show both doc file and excel file creation from your Android App.
Thanks
Programmer World
programmerworld.co
-
Where is the button function?
I added a button now the add button is working, when I fetch the data it keeps stopping
Could you debug the code and check the error message or exception being thrown?
For reference, the complete details and source code shown in this video is also shared in the below link:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Cheers
Programmer World
programmerworld.co
-
my code not working anyone guys can help me?
Any specific error you are getting?
Just a hint, if required, then please refer to the source code of this App from the below link:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Cheers
Programmer World
programmerworld.co
-
this is not good please give the full details or make a full video sir
AFAIK this video covers complete steps required to develop the App. However, if you are looking for any additional information then please feel free to visit the below webpage of this tutorial:
programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
Good Luck
Programmer World
programmerworld.co
-
Hello sir my question is if user enter a number in sign up activity then how could we call on that number by fetching it from sqlite database
Simply retrieve the phone number from the SQLite database (either using the number used during the sign-up or index of the number in the database or any other reference which can be used).
And then use the retrieved number to make the call. Below link should help:
programmerworld.co/android/how-to-call-or-dial-a-phone-number-from-your-own-custom-android-app-complete-source-code/
Cheers
Programmer World
programmerworld.co
-