Exploring the data from an SQLite SELECT query within Ionic2 version 3.4

Currently, I'm working with the latest version of Ionic2 (v3.4) and attempting to utilize the ionic native SQLite functionality. Successfully, I've managed to create a database file and insert a table into it as shown below:

this.sqlite.create({
  name: "data.db",
  location: "default"
})
.then((db:SQLiteObject) => {
  db.executeSql(tableQuery, {})
  .then(() => console.log("success"))
  .catch(() => console.log("fail"));
})

The insertion process is also functioning properly. However, when trying to retrieve the result of a selection query:

this.sqlite.create({
  name: "data.db",
  location: "default"
})
.then((db:SQLiteObject) => {
  db.executeSql("SELECT * FROM savedCoupons where itemId=" + itemId, {})
  .then((db) => {console.log(JSON.stringify(db))})
  .catch(() => console.log("***ERROR WITH SELECT***"));
})
.catch(() => console.log("ERROR: FAILED TO CREATE/OPEN DATABASE."));

Confusion arises due to inadequate documentation. Despite using JSON.stringify() to confirm the query's execution, the returned value is limited to

{"rows":{"length":1}, "rowsAffected":0}
. How can I effectively access the query results?

Answer №1

If you have a table with three columns such as id, name, and lastname, you can retrieve the data after querying in the following manner:

  db.executeSql('SELECT * FROM students WHERE id='+1, {})
          .then(result => {
            for (var i = 0; i < result.rows.length; i++) {
               console.log("---Id---"+result.rows.item(i).id);
               console.log("---Name---"+result.rows.item(i).name);
               console.log("---Lastname---"+result.rows.item(i).lastname);
           }
          });

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

Error TS2339: The property 'mock' is not found on the type '(type: string) => Promise'. Unable to create a mock for SQS Queue.sendMessage()

I am attempting to simulate a call to the SQS method sendMessage() that is used in the System Under Test (SUT) like this: private async pushJobIntoQueue(network: Network) { await this.contactInteractionsQueue.sendMessage( JSON.stringify({ ...

What is the best method for expanding nodes after the tree has been loaded?

Greetings! I am currently working on a web application using Angular 5. One of the features I have implemented is loading trees onto my webpage. These trees are populated with data from an API and are designed to be dynamic. After loading the tree, my goal ...

Sharing the label element as a prop in React component

I encountered the following code snippet: <div className="input-field"> <label htmlFor="timeObjective">Time Objective</label> <FrequencySet label='label'/> //HERE </div> My goal is to tra ...

Issue with autoSize feature in EditText not functioning properly on Android Studio

In the project I am currently working on, there is an EditText called exercise with fixed layout_width and layout_height. This EditText is programmatically expanded downwards by adding one line of text (String) + "\n" to it. Sometimes, the added line ...

Having trouble integrating a downloaded plugin into a Cordova/Phonegap project?

Is there a way to make a plugin function properly without installing it from the repository? Every time I attempt to install a plugin using phonegap plugin add <<git repository>>, I encounter various errors due to compatibility issues with Wind ...

Creating a signature for a function that can accept multiple parameter types in TypeScript

I am facing a dilemma with the following code snippet: const func1 = (state: Interface1){ //some code } const func2 = (state: Interface2){ //some other code } const func3: (state: Interface1|Interface2){ //some other code } However, ...

Error: Unexpected character < in node_modules/angular2/ts/package.json syntax

I am currently working on developing an app with Angular 2 and have encountered an error during setup. The error message I received is: SyntaxError: /home/mts/Desktop/sampleProject/appSails/node_modules/angular2/ts/package.json: Unexpected token < at O ...

How can I extract JSON data within the Ionic2 framework?

After making an AJAX post call to an API, I successfully received a JSON response. Here is the response: JSON Response: { "s": true, "m": { "i": 10, "n": "Apple Watch", "p": "14000" }} While testing my TypeScript code, I used an alert to display the J ...

Is there a way to retrieve the message body with a specific name in the XMPP Framework for iOS?

As I work on developing an IM app, I've encountered a coding issue that I'm struggling to solve. It could be due to my beginner level or a genuine challenge. <message to="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

TypeScript Generic Functions and Type Literals

Everything seems to be running smoothly: type fun = (uid: string) => string const abc: fun = value => value const efg = (callback:fun, value:string) =>callback(value) console.log(efg(abc, "123")) However, when we try to make it generic, we e ...

How can I prevent my splash screen background from getting distorted?

Can you help me figure out how to set a non-stretching background for a splash screen while keeping it at a density-independent size? To give you a better idea, here's an example: I have this large image file (1000x1000px) that I want as the backgrou ...

Tips on implementing SimpleOnGestureListener in a custom view?

I am struggling to understand how to implement a gesture detector in a custom view. I want to utilize the onLongPress method, but I'm unsure of where to place it or how to actually use it. class CustomView(context: Context, attr: AttributeSet) : View( ...

Having trouble resolving modules with Angular 2 and ASP.NET 5 (unable to locate angular2/core)?

I am diving into a fresh ASP.NET5/MVC6 project and facing some challenges along the way. Struggle 1 When I opt for classic as the moduleResolution in my tsconfig.json, I encounter an error stating: Cannot locate module 'angular2/core' Strugg ...

Fuzziness occurrence in distinct vue element

My Situation I have a custom DropDown with a filter text input above. The DropDown can be opened independently from the filter text input. My Goal I want the DropDown to close when the filter input loses focus and also when I click outside of the Drop ...

Having trouble reaching a public method within an object passed to the @Input field of an Angular component

My configurator object declaration is as follows. export class Config { constructor(public index: number, public junk: string[] = []) { } public count() : number { return this.junk.length; } } After declaring it, I pass it into the input decorated fi ...

Checking at compile time whether a TypeScript interface contains one or multiple properties

Is there a way to determine if a typescript interface contains at least one property at compile time without knowing the property names? For example, with the interfaces Cat and Dog defined as follows: export type Cat = {}; export type Dog = { barking: bo ...

Tips for sending a value to a container component

Within my task management application, I have implemented two selectors: export const selectFilter = (state: RootState) => state.visibilityFilter export const selectVisibleTodos = createSelector( [selectTodos, selectFilter], (todos: Todo[], filter : ...

What is the best way to create a memoized function in React?

I am currently developing an application using react and typescript, and I am facing a challenge in memoizing a function. const formatData = ( data: number[], gradientFill?: CanvasGradient ): Chart.ChartData => ({ labels: ["a", ...

How come extending the view group results in a child count of 0?

One issue I encountered is when trying to access the child count of a FrameLayout that I am extending. After loading the view from XML in the constructor, calling getChildCount() returns 0. How can this be resolved? public class CustomFrameLayout extends ...

The issue persists wherein getBoundingClientRect fails to provide the accurate value following a resize

I have implemented a custom directive that appends a dropdown to the body when it is displayed. The logic functions correctly when executed within the ngAfterViewInit lifecycle, but I encounter issues when attempting to use the same logic within the wind ...