Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot bind buffer in Android #428

Closed
maitre-piccolo opened this issue Jun 8, 2023 · 13 comments
Closed

Cannot bind buffer in Android #428

maitre-piccolo opened this issue Jun 8, 2023 · 13 comments

Comments

@maitre-piccolo
Copy link

Hello,

I'm using GUID columns in my tables. GUID type is basically a BLOB/Binary type in SQLite.

In javascript we use Buffer to store the values of such fields.

With the web version of this plugin, the binding on insert works great.

But when testing the same code in android I get this error :

Error: Run: Cannot bind {"type":"Buffer","data":[17,238,5,240,250,46,224,16,157,33,165,212,107,42,117,113]} at index 1 Supported types: Null, ByteArray, Float, Double, Long, Int, Short, Byte, String

It seems that in android, it doesn't pass the Buffer directly but pass the result of Buffer.toJSON()

I can't figure a way to circumvent this issue.

Am I missing something or is it not supported yet ?

@jepiqueau
Copy link
Collaborator

@maitre-piccolo Can you share your app so i can test and try fixing the issue

@maitre-piccolo
Copy link
Author

maitre-piccolo commented Jun 9, 2023

I cannot share my app but I can share a test case using one of your starter app.

Step 1 : Install this app https://github.com/jepiqueau/react-sqlite-app-starter

Step 2 : Add the buffer package (npm install buffer)

Step 3 : replace the file src/pages/Tab1.tsx with
Tab1.txt
(Of course be sure to revert the extension to tsx)

I added code in this file to create a table with a GUID field and then an example of inserting a buffer. This code triggers the problem I have.

BTW, there seem to be a bindBlob function in android SQLite lib.

@jepiqueau
Copy link
Collaborator

@maitre-piccolo this has been fixed for iOS ans Android and will be in the next release late today

@jepiqueau
Copy link
Collaborator

@maitre-piccolo Can you test release 5.0.3-1 and give me your feedback

@maitre-piccolo
Copy link
Author

Thanks, I just did a simple test and it seems to be working perfectly now. I'll test properly everything I have with GUID/buffer tomorrow and if all is good I'll close this issue :)

@maitre-piccolo
Copy link
Author

Ok tested properly, everything is good for me, thanks again for the quick fix !

@rattata2023
Copy link

I have tested that with android it is working fine but with iOS I am getting this error
"errorMessage":"Query: Failed in selectSQL : Error: bindValues bind failed The operation couldn’t be completed. (CapacitorCommunitySqlite.UtilsSQLCipherError error 26.)
after some debug found that
if let value = value.unsafelyUnwrapped as? [UInt8] {
let data: Data = Data(value)
sqlite3_bind_blob(handle, Int32(idx), data.bytes,
Int32(data.bytes.count), SQLITETRANSIENT)
}
code is not going under these lines for Blob images in iOS I have used same code that you told in SQLiteBlob.md file
could you fix that as I am not that good in iOS can do it in android.

thanks

@jepiqueau
Copy link
Collaborator

@rattata2023 can you send the url of the image that you try to save and the code you are using to store it in SQLite db. I have a test case and it works fine

@rattata2023
Copy link

@jepiqueau thanks for the quick response I have tried to save base64 image here is my code

async saveUserInfo(profile:any):Promise{
let copyProfile = Object.assign({}, profile)

console.log('test profile')
const userImage = await b64ToBlob(profile.profile_image)
const qrCode =  await b64ToBlob(profile.qr_code)

delete copyProfile["profile_image"];
delete copyProfile["qr_code"];

try {
let deleteOutput = await this.sqliteDB.query('DELETE FROM user_profile',[])
let query = await this.sqliteDB.query('INSERT into user_profile(user_image,qr_code,user_info) VALUES (?,?,?)',[userImage,qrCode,JSON.stringify(copyProfile)])
return true
} catch (error) {
console.log(error)
throw error
}
}

export const b64ToBlob = async (b64Image:string):Promise => new Promise(async (resolve,reject)=>{
const blob = await(await fetch(b64Image)).blob();
const userImage:Buffer = Buffer.from(new Uint8Array(await blob.arrayBuffer()));
resolve(userImage)
})

@jepiqueau
Copy link
Collaborator

@jepiqueau if you save a base64 image in the db as a blob you do not need to do this as it is a string so you can directly use the value returned in the image src.if the string returned does not start by something like "data:image/png;base64" if it is a png image do
Image.src = "data:image/png;base64," + returned value

That is it
Look at angular-sqlite-app-starter testblobs.page.ts

@rattata2023
Copy link

@jepiqueau I was trying to save base64 image as blob in the db I am not storing bas64 image in the Db I have also tried with the testblobs.page.ts it stores base64 image and return base64 image .

@jepiqueau
Copy link
Collaborator

@rattata2023 Look at angular-sqlite-app-starter 5.0.2 i add a test to save base64 image as blob and it works fine. So you should be looking at your code

@rattata2023
Copy link

ok thanks working now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants