capacitor-community / sqlite

Community plugin for native & electron SQLite databases
MIT License
426 stars 104 forks source link

executeSet not inserting rows #547

Closed beard7 closed 2 weeks ago

beard7 commented 3 weeks ago

Describe the bug I'm trying to use executeSet on Android to insert a batch of rows into a table, but rather than the rows being inserted, I just get a single row with just the identity column set; all other columns are null.

To Reproduce Steps to reproduce the behavior:

const sqlite = new SQLiteConnection(CapacitorSQLite);
db = await sqlite.createConnection("mydb", false, "no-encryption", 1);
const executeSet = [
  {
    statement: "INSERT INTO projects (id, project_name, full_reference, latitude, longitude, number_name, project_open, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
    values: [
      [17026,"Test Project","218.0TEST",55.7,-2.01,"218.0TEST Test Project",1,"2024-04-19 16:22:29",null],
      [17027,"Test Project 2","219.0TEST",55.7,-2.01,"219.0TEST Test Project 2",1,"2024-04-20 16:22:29",null]
    ]
  }
];
const result = await db.executeSet(executeSet);

Expected behavior Two rows with supplied values should be inserted into the table, instead, if I run const selectAllResult = await db.query("SELECT * FROM projects;"); I get a single row: {"values":[{"identity":21,"id":null,"project_name":null,"full_reference":null,"latitude":null,"longitude":null,"number_name":null,"project_open":null,"updated_at":null,"deleted_at":null}]}

Smartphone):

jepiqueau commented 3 weeks ago

@beard7 Are you using capacitor6 android 14 is not supported by capacitor5

beard7 commented 3 weeks ago

No. Capacitor 5. The device I'm testing on is running Android 14, but I'm not targetting 14.

jepiqueau commented 3 weeks ago

@beard7 can you share the set of data and the way you do the executeSet

jepiqueau commented 3 weeks ago

@beard7 sorry i mean the database schema

jepiqueau commented 3 weeks ago

@beard7 can you Split it into two run commands and tell me if it works

beard7 commented 3 weeks ago

Hi,

If the set is split into separate commands it works fine:

const executeSet = [
  { statement: "INSERT INTO projects (id, project_name, full_reference, latitude, longitude, number_name, project_open, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
    values: [17026,"Test Project","218.0TEST",55.7,-2.01,"218.0TEST Test Project",1,"2024-04-19 16:22:29",null] },
  { statement: "INSERT INTO projects (id, project_name, full_reference, latitude, longitude, number_name, project_open, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
    values: [17027,"Test Project 2","219.0TEST",55.7,-2.01,"219.0TEST Test Project 2",1,"2024-04-20 16:22:29",null]
  }
];
const result = await db.executeSet(executeSet);

This inserts the rows.

I'm happy to use this format, but I don't know if there is any difference in performance. I sometimes need to run INSERT OR REPLACE queries on thousands of rows.

The table schema is pretty straight forward:

CREATE TABLE projects ( 
identity INTEGER PRIMARY KEY AUTOINCREMENT, 
_state_ TEXT NULL, 
Id INTEGER NULL, 
project_name TEXT NULL, 
full_reference TEXT NULL, 
latitude INTEGER NULL, 
longitude INTEGER NULL, 
number_name TEXT NULL, 
project_open INTEGER NULL, 
updated_at TEXT NULL, 
deleted_at TEXT NULL )
jepiqueau commented 2 weeks ago

@beard7 This has been fixed in v5.7.3-3. Thanks for raising the issue

beard7 commented 2 weeks ago

Great. Thanks.