Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48979

How do you add new column in sqlite?

$
0
0

so upon creating the database, it displays on the app but when i try to add a new column in the create table it shows errors :C

The error

[ng] [console.error]: {
[ng]   "message": "Failed to import SQL; message=sqlite3_prepare_v2 failure: near \")\": syntax error",
[ng]   "code": 5,
[ng]   "statement": "CREATE TABLE IF NOT EXISTS storee (\n\tstore_id INT PRIMARY KEY,\n\tstore_name varchar(255),\n\tstore_address varchar(255),\n\tphone varchar(255),\n)"
[ng] }
[ng] [console.error]: "Unhandled Promise rejection:" "a statement error callback did not return false: Failed to import SQL; message=sqlite3_prepare_v2 failure: near \")\": syntax error" "; Zone:" "<root>" "; Task:" null "; Value:" {
[ng]   "code": 5
[ng] } "Error: a statement error callback did not return false: Failed to import SQL; message=sqlite3_prepare_v2 failure: near \")\": syntax error\n 
   at newSQLError (http://localhost:8100/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:26:18)\n    at SQLitePluginTransaction.handleStatementFailure (http://localhost:8100/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:403:13)\n    at Object.error (http://localhost:8100/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:423:18)\n    at mycb (http://localhost:8100/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:465:20)\n    at Object.callbackFromNative (http://localhost:8100/cordova.js:295:58)\n    at <anonymous>:1:9"

The sqlite table

CREATE TABLE IF NOT EXISTS appsto (
	store_id INTEGER PRIMARY KEY,
	store_name VARCHAR (255),
	store_barangay VARCHAR (255),
	store_address VARCHAR,
	phone VARCHAR (255),	
);

INSERT INTO appsto VALUES(2,'3ML COMMERCIAL SPACE RENTAL','addre','address','09628783984');

my db.service.ts

import { Injectable } from '@angular/core';
import { Platform } from '@ionic/angular';
import { Store } from './store';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { BehaviorSubject, Observable, throwError  } from 'rxjs';
import { SQLitePorter } from '@ionic-native/sqlite-porter/ngx';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
import { catchError, map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class DbService {
  private storage: SQLiteObject;
  songsList = new BehaviorSubject([]);
  private isDbReady: BehaviorSubject<boolean> = new BehaviorSubject(false);

  constructor(
    private platform: Platform, 
    private sqlite: SQLite, 
    private httpClient: HttpClient,
    private sqlPorter: SQLitePorter,
  ) {
    
 
      this.platform.ready().then(() => {
        console.log("db platform");
      alert("create db")
      this.sqlite.create({
        name: 'store.db',
        location: 'default'
        })
        .then((db: SQLiteObject) => {
          alert("sqlobject");
            this.storage = db;
            this.getFakeData();
        });
      });
     
  
  }
  dbState(){
    console.log("db state");
    return this.isDbReady.asObservable();
    
  }

  fetchStores(): Observable<Store[]> {
    console.log("fetch done");
    return this.songsList.asObservable();
  }

  //Render fake data
  getFakeData() {
    console.log("fake done");
    alert("get db");
    this.httpClient.get(
      'assets/stores.sql', 
      {responseType: 'text' }
    ).subscribe(data => {
      console.log("porter");
      alert("porter");
      this.sqlPorter.importSqlToDb(this.storage, data)
      
        .then(_ => {
          console.log("porter2");
          this.getStores();
          this.isDbReady.next(true);
        })
        .catch(error => console.error(error));
        console.log("error done");
    });
  }



  // Get list
  getStores(){
    console.log("getstorees done");
    alert("get stores");
    return this.storage.executeSql('SELECT * FROM storee', []).then(res => {
      let items: Store[] = [];
      if (res.rows.length > 0) {
        for (var i = 0; i < res.rows.length; i++) { 
          items.push({
            store_id: res.rows.item(i).store_id,
            store_name: res.rows.item(i).store_name,  
            store_barangay: res.rows.item(i).store_barangay,
            store_address: res.rows.item(i).store_address,
            phone: res.rows.item(i).phone,
            store_category: res.rows.item(i).store_category
           
           });
        }
      }
      console.log("fake done");
      this.songsList.next(items);
    });
  }

   }

if i try to add a new column in the create table and insert it below, it gives and error

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 48979

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>