0

Within my Android/iOS React Native (Expo managed) app I want a database to store my images which I encode to base64. Because I am using Expo, I use expo-sqlite within my React Native app. I initialize my database within my classes (extends Component) constructor the following

constructor(props){
   super(props);
   this.state = {
      db: null,
      ...
   }
}

and within my ComponentDidMount I initialize the database with

async setupDB() {
   const db = SQLite.openDatabase('database.db');
   this.setState({db});
   db.transaction( tx=> {
      tx.executeSql(
         'CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY AUTOINCREMENT, image BLOB)', 
         [], 
         (_, result) => {
            console.log('success');
         }, (_, error) => {
            console.error(error);
         },
      );
   });
}

I stuff my image into the DB using the following code

async toDB(image) {
   const { db } = this.state;
   try {
      await  db.transaction( tx=> {
      tx.executeSql(
         'INSERT INTO mytable (image) values (?)', 
         [image], 
         (_, result) => {
            console.log('success');
         }, (_, error) => {
            console.error(error);
         },
      );
   });
   } catch (error) { console.error(error); }
}

And afterwards I print the data using the following function

async printDB() {
   const { db } = this.state;
   try {
      await  db.transaction( tx=> {
      tx.executeSql(
         'SELECT * FROM mytable', 
         [image], 
         (_, result) => {
            const rows = result.rows;
            for(let i = 0; i < rows.length; i++) {
               console.log(rows.item(i).image); // this only prints null using the full length base64 image
            }
            console.log('success');
         }, (_, error) => {
            console.error(error);
         },
      );
   });
   } catch (error) { console.error(error); }
}

But when I reduce the length of my base64 image to e.g. 1/4 of its original length, it seems to be stored correctly. Printing out the length of my base64 console.log(image.length); results in 9188676 which should fit a row within the SQLite database as far as I know. I can also reproduce this using a String consisting only 0, so it is not related to the base64 encoding.

So here is my question, am I doing anything wrong here or do I have to use another database/technique to store my base64 images into the database?

EDIT1: I am using a workaround as I saw that quartering the base64 image works within the database table. Therefore I store every quarter with an identical name, so I have to read all four identical "name" entries and concatenate the strings together afterwards.

Flippy
  • 202
  • 3
  • 12

0 Answers0