1. Are you ready for the Galaxy S20? Here is everything we know so far!

SQLite to pdf doesnt work

Discussion in 'Android Development' started by GabeTheGreat73, May 2, 2020.

  1. GabeTheGreat73

    Thread Starter

    I watched youtube vid for basic example of SQLite to pdf file... but the table was created in the MainActivity instead of the databasehelper. I moved it to the dbhelper class, and the insert data functions work fine, but when I try to create the pdf and display the string in the textView box as well, it crashes...
    android studio isn't showing any red for errors and it compiles fine, the mainactivity opens fine, inserting data works fine, its only when I try to create the pdf that it crashes... any help is appreciated!

    Here is my mainactivity and dbhelper files:

    Code (Text):
    1.  
    2.  
    3. import androidx.annotation.RequiresApi;
    4. import androidx.appcompat.app.AppCompatActivity;
    5. import androidx.core.app.ActivityCompat;
    6. import android.content.pm.PackageManager;
    7. import android.database.Cursor;
    8. import android.database.sqlite.SQLiteDatabase;
    9. import android.graphics.Paint;
    10. import android.graphics.pdf.PdfDocument;
    11. import android.os.Build;
    12. import android.os.Bundle;
    13. import android.os.Environment;
    14. import android.view.View;
    15. import android.widget.EditText;
    16. import android.widget.TextView;
    17. import android.widget.Toast;
    18. import android.widget.Button;
    19. import java.io.File;
    20. import java.io.FileOutputStream;
    21. import java.io.IOException;
    22. import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
    23. import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
    24.  
    25. public class MainActivity extends AppCompatActivity {
    26.  
    27.     mySQLiteDBHandler sqlLiteDBHandler;
    28.     EditText editTextSerialNumberInsert;
    29.     EditText editTextSerialNumberFetch;
    30.     EditText editTextInsert;
    31.     TextView textViewDisplay;
    32.     Button btnInsertUpdate;
    33.  
    34.     SQLiteDatabase sqLiteDatabase;
    35.    
    36.     @Override
    37.     protected void onCreate(Bundle savedInstanceState) {
    38.         super.onCreate(savedInstanceState);
    39.         setContentView(R.layout.activity_main);
    40.         sqlLiteDBHandler = new mySQLiteDBHandler(null,null,null,1);
    41.         ActivityCompat.requestPermissions(this,new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
    42.  
    43.         sqLiteDatabase = sqlLiteDBHandler.getWritableDatabase();
    44.  
    45.         editTextSerialNumberInsert = findViewById(R.id.editText1);
    46.         editTextInsert = findViewById(R.id.editText2);
    47.         editTextSerialNumberFetch = findViewById(R.id.editText3);
    48.         textViewDisplay = findViewById(R.id.textView1);
    49.         btnInsertUpdate = findViewById(R.id.btn_insert);
    50.         InsertUpdateData();
    51.     }
    52.    
    53.     public void InsertUpdateData() {
    54.         btnInsertUpdate.setOnClickListener(
    55.                 new View.OnClickListener() {
    56.                     @Override
    57.                     public void onClick(View v) {
    58.                         boolean isUpdate = sqlLiteDBHandler.insertUpdateData(editTextSerialNumberInsert.getText().toString(),
    59.                                 editTextInsert.getText().toString());
    60.                         if(isUpdate == true)
    61.                             Toast.makeText(MainActivity.this,"Data Updated",Toast.LENGTH_LONG).show();
    62.                         else
    63.                             Toast.makeText(MainActivity.this,"Something went wrong!",Toast.LENGTH_LONG).show();
    64.                     }
    65.                 }
    66.         );
    67.     }
    68.    
    69.     @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    70.     public void CreatePDF(View view){
    71.         String query = "Select Text from PDFTable where SerialNumber=" + editTextSerialNumberFetch.getText().toString();
    72.         Cursor cursor = sqLiteDatabase.rawQuery(query,null);
    73.         try{
    74.             cursor.moveToFirst();
    75.             textViewDisplay.setText(cursor.getString(0));
    76.         }
    77.         catch (Exception e){
    78.             e.printStackTrace();
    79.             textViewDisplay.setText("uh-oh..");
    80.             return;
    81.         }
    82.  
    83.         PdfDocument pdfDocument = new PdfDocument();
    84.         PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300,600,1).create();
    85.         PdfDocument.Page page = pdfDocument.startPage(pageInfo);
    86.  
    87.         page.getCanvas().drawText(cursor.getString(0),10,25, new Paint());
    88.         pdfDocument.finishPage(page);
    89.  
    90.         String filePath = Environment.getExternalStorageDirectory().getPath()+"/Download/"+editTextSerialNumberFetch.getText().toString()+".pdf";
    91.         File file = new File(filePath);
    92.  
    93.         try {
    94.             pdfDocument.writeTo(new FileOutputStream(file));
    95.         } catch (IOException e) {
    96.             e.printStackTrace();
    97.         }
    98.         pdfDocument.close();
    99.     }
    100. }
    101.  

    AND THIS IS THE mySQLiteDBHandler.java file:

    Code (Text):
    1.  
    2.  
    3. import android.content.ContentValues;
    4. import android.content.Context;
    5. import android.database.Cursor;
    6. import android.database.sqlite.SQLiteDatabase;
    7. import android.database.sqlite.SQLiteOpenHelper;
    8. import androidx.annotation.Nullable;
    9. import static android.content.ContentValues.*;
    10.  
    11. public class mySQLiteDBHandler extends SQLiteOpenHelper {
    12.     public static final String DATABASE_NAME = "PDFDatabase.db";
    13.     public static final String TABLE_NAME = "PDFTable";
    14.     public static final String COL_1 = "SerialNumber";
    15.     public static final String COL_2 = "Text";
    16.  
    17.     public mySQLiteDBHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
    18.        super(context, name, factory, 1);
    19.     }
    20.     @Override
    21.     public void onCreate(SQLiteDatabase db) {
    22.         db.execSQL("create table " + TABLE_NAME +" (SERIALNUMBER TEXT,TEXT TEXT)");
    23.  
    24.     }
    25.     @Override
    26.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    27.     }
    28.  
    29.     public boolean insertUpdateData(String serialnumber,String text) {
    30.         SQLiteDatabase db = this.getWritableDatabase();
    31.         ContentValues contentValues = new ContentValues();
    32.         contentValues.put(COL_1,serialnumber);
    33.         contentValues.put(COL_2,text);
    34.         db.update(TABLE_NAME, contentValues, "SERIALNUMBER = ?", new String[] {serialnumber});
    35.         return true;
    36.     }
    37. }
    38.  
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - SQLite pdf doesnt
  1. werner111
    Replies:
    6
    Views:
    1,213
  2. rowlanda38
    Replies:
    0
    Views:
    463
  3. iliji
    Replies:
    1
    Views:
    1,383
  4. dplusk
    Replies:
    0
    Views:
    943
  5. selva_0707
    Replies:
    0
    Views:
    1,319
  6. manjula1
    Replies:
    0
    Views:
    700
  7. Dilip Birajadar
    Replies:
    4
    Views:
    863
  8. Negrazo1949
    Replies:
    3
    Views:
    1,041
  9. Praful Wadkar
    Replies:
    1
    Views:
    483
  10. Fred Pino
    Replies:
    2
    Views:
    3,180

Share This Page

Loading...