sqlfluttersqflite

Exception in data insertion in sqflite


I'm using the sqflite package to store data locally in my mobile app. Upon testing the app on my physical android device, it's showing the following exception that I printed in a debugPrint statement:

I/flutter ( 6488): SQL INSERTION EXCEPTION: DatabaseException(near "logo": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO Preferences (co_id, default_br_id, default_wh_id, logo, business_name, business_tax_vat_id, last_sales_invoice_id, last_sales_return_invoice_id, last_purchase_invoice_id, last_purchase_return_invoice_id, last_sales_quotation_id)
I/flutter ( 6488):     VALUES (13, 1, 1, the logo, شركة الرحاب للعطور, business_tax_vat_id, null, null, null, null, null)) sql '    INSERT INTO Preferences (co_id, default_br_id, default_wh_id, logo, business_name, business_tax_vat_id, last_sales_invoice_id, last_sales_return_invoice_id, last_purchase_invoice_id, last_purchase_return_invoice_id, last_sales_quotation_id)
I/flutter ( 6488):     VALUES (13, 1, 1, the logo, شركة الرحاب للعطور, business_tax_vat_id, null, null, null, null, null)
I/flutter ( 6488):     ' args []

This is the code statement causing the exception:

await database.rawInsert("""
    INSERT INTO Preferences (co_id, default_br_id, default_wh_id, logo, business_name, business_tax_vat_id, last_sales_invoice_id, last_sales_return_invoice_id, last_purchase_invoice_id, last_purchase_return_invoice_id, last_sales_quotation_id)
    VALUES (${json['co_id']}, ${json['default_br_id']}, ${json['default_wh_id']}, ${json['logo']}, ${json['business_name']}, ${'business_tax_vat_id'}, null, null, null, null, null)
    """);

I checked the syntax many times and checked for similar issues on the internet but nothing worked.


Solution

  • Try this:

    await database.rawInsert("""
        INSERT INTO Preferences (
            co_id, default_br_id, default_wh_id, logo, business_name, business_tax_vat_id, 
            last_sales_invoice_id, last_sales_return_invoice_id, last_purchase_invoice_id, 
            last_purchase_return_invoice_id, last_sales_quotation_id
        ) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        """, [
            json['co_id'],
            json['default_br_id'],
            json['default_wh_id'],
            json['logo'],
            json['business_name'],
            json['business_tax_vat_id'],
            null, 
            null,  
            null,  
            null,  
            null   
        ]);