Executing multiple insert statements using the executeSql method

I am encountering an issue with multiple inserts in my code. Here is the snippet:

 db.openDatabase({
      name: "data.db",
      location: "default"
    }).then(() => {
      db.executeSql(
        +"INSERT INTO check_item (name) VALUES ('Pneus - calibragem'); "
        +"INSERT INTO check_item (name) VALUES ('Pneus – banda de rodagem'); "
        +"INSERT INTO check_item (name) VALUES ('Nivel do oleo'); "
        +"INSERT INTO check_item (name) VALUES ('Luzes dianteiras'); "
        +"INSERT INTO check_item (name) VALUES ('Luzes traseiras'); "
        +"INSERT INTO check_item (name) VALUES ('Triangulo de sinalizacao'); "
        +"INSERT INTO check_item (name) VALUES ('Chave de roda'); "
        +"INSERT INTO check_item (name) VALUES ('Documentacao do veiculo'); ", {}).then((data) => {
          console.log("TABLE CREATED: ", data);
        }, (error) => {
          console.error("Unable to execute sql teste", error);

        })
    }, (error) => {
      console.error("Unable to open database", error);
    });

Any suggestions on how to resolve this problem? Thanks in advance!

Answer №1

At this moment, you are attempting to run multiple insertion statements using a single call to executeSql(). The correct syntax for inserting multiple records in one statement is as follows:

INSERT INTO check_item (name)
VALUES ('Tires - inflation'),
       ('Tires - tread depth'),
       ('Oil level'),
       ...
       ('Vehicle documentation');

If you wish to avoid inserting duplicate names, the simplest solution may be to add a unique constraint on the name column. Although directly adding a unique constraint in SQLite requires table recreation, you can achieve similar results by adding a unique index:

CREATE UNIQUE INDEX name_index ON check_item (name);

For more information, refer to: How to add unique constraint in already made table in sqlite ios?

Answer №2

ADD TO table_name (x,y,z) INPUTS (10,20,30), (40,50,60), (70,80,90);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering an error of TypeError while attempting to generate a new GraphQL

Currently using Apollo-Server/TypeScript with graphql-tool's makeExecutableSchema() to set up schema/directives. Encountering an error while attempting to add a basic GraphQL Directive: TypeError: Class constructor SchemaDirectiveVisitor cannot be in ...

Guide to setting a white background color specifically for a lightbox in Angular

I want to change the background-color to white for my gallery items. Currently, I am using the following code to open the full-screen view: this.lightbox.open(0, 'lightbox1', { panelClass: 'fullscreen'}) Can someone please guide me on ...

After upgrading to Angular 15, the Router getCurrentNavigation function consistently returns null

Since upgrading to angular 15, I've encountered a problem where the this.router.getCurrentNavigation() method is returning null when trying to access a state property passed to the router. This state property was initially set using router.navigate in ...

What method can be used to automatically set the headerTooltip in ag-grid to match the data displayed in the cell?

Looking for some assistance with ag-grid. I'm trying to implement a default tooltip on every grid in my application. The tooltip value should display the same value as the cell someone is hovering over. I attempted to achieve this using the following ...

Troubleshooting a Type Parameter Error in React Native TypeScript

I am working on a project in React Native using TypeScript, and I encountered this issue: I am getting the error Argument of type 'GestureResponderEvent' is not assignable to parameter of type 'SetStateAction<string>'.ts(2345) wit ...

Struggled with the implementation of a customized Angular filter pipe

I have recently developed a custom filter type to sort the notes-list in my application, with each note containing a 'title' and 'message'. Although there are no errors, I am facing issues as the pipe doesn't seem to be working pr ...

Display a customized modal showcasing the data of a particular user

Seeking advice on how to pass data to a modal based on a specific user. I have an array with user information and would like to display their name and email in a modal when the user is clicked on. Any suggestions on how to accomplish this? ...

Transferring Cookies through FETCH API using a GET method from the client-side to the server-side

Struggling with a challenge here: Attempting to send a cookie via a GET request to determine if the user is logged in. The cookie is successfully transmitted to my browser and is visible in the developer tools. When I manually make a request through the UR ...

Angular is unable to modify a property that is marked as read-only

When attempting to update the system value in the object telecom, I encountered an error message at this stage: Cannot assign to read only property 'system' of object '[object Object]' this.organization.telecoms.forEach((telecom: T ...

How can I simulate an Item in dynamoose with a custom updateAt timestamp?

Currently, I am integrating end-to-end testing for a micro service. One of the features I am working on involves verifying that an item was last updated over 2 hours ago. However, when I create a mock object with a date that is 6 hours older than the cur ...

Tips for accurately inputting a Record key in typescript

Within my code, I have a function that filters the properties of an object based on a specified filtering function: function filterProps<K extends string, V>(object: Record<K, V>, fn: (key:K, value: V, object: Record<K, V>) => unknown) ...

Issue: Multiplying values within an array in TypeScript requires that the left-hand side of the arithmetic operation must be of type 'any', 'number', or 'enum'

Having trouble with Typescript as I encounter this error message The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362) Specifically facing issues when trying to multi ...

Using React.useContext in a Class Component with Typescript: Issue resolving the signature of the class decorator

Struggling to implement a store using React.createContext within my class components. The setup of my App is as follows: const someStore = new SomeStore(); export const StoreContext = React.createContext(someStore); export interface IProps { store ...

What is the reason for the return of undefined with getElementsByClassName() in puppeteer?

Currently, I am utilizing puppeteer to fetch certain elements from a webpage, specifically class items (divs). Although I understand that getElementsByClassName returns a list that needs to be looped through, the function always returns undefined for me, e ...

Customize drawerLabel in React Native based on user's current location

I am facing an issue with my react-native application where I need to dynamically render a menu item based on the user's location. However, since the function that fetches the user location is asynchronous, I am encountering an undefined error. My que ...

Ensuring that the field remains active in Angular2 even after editing it with a value

When the 3rd dropdown value is selected in the first field dropdown, it enables the 2nd field. However, when I edit and change a different value, since the 2nd field is disabled, I receive null or undefined as the value. I want the 2nd field to retain its ...

Creating a custom Object Type based on the values of an array of objects using Typescript

I have been attempting to create a generic type (Response) that consolidates all values from KeysForResponse, specifically the values from the valueIWant property for each object in MyObject[]. I am struggling to find a solution and wondering if it is even ...

Using Typescript: Passing the name of a subclass as a parameter to a function

Below is the simplified code snippet that I am working with: class Component { } class CameraComponent extends Component { foo: number; constructor(bar: number) { super() } } function doSomething(klass: typeof Component) { } doSo ...

Angular - Issue with highcharts-angular / bootstrap causing charts to not resize to fit parent container width

I'm currently utilizing Angular in conjunction with bootstrap and Highcharts / Highcharts-angular My issue lies in the fact that when I adjust the size of the grids, the charts do not expand to 100% width. Below is the code snippet: app.component.h ...

What is the best approach to managing exceptions consistently across all Angular 2/ Typescript observables?

Throughout the learning process of Angular2, I have noticed that exceptions are often caught right at the point of the call. For example: getHeroes(): Promise<Hero[]> { return this.http.get(this.heroesUrl) .toPromise() ...