Connect Sqlite and create a table

Continuing the discussion from In my angular ionic framework to connect sqlite but jeep-sqlite not present in dom error showing:

my sqlite service

import { Injectable } from ‘@angular/core’;
import { Capacitor } from ‘@capacitor/core’;
import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection } from ‘@capacitor-community/sqlite’;

@Injectable({
providedIn: ‘root’
})
export class SqliteService {
private sqlite: SQLiteConnection;
private db: SQLiteDBConnection | null = null;

constructor() {
this.sqlite = new SQLiteConnection(CapacitorSQLite);
}

async init() {
try {
console.log(‘Checking CapacitorSQLite…’);
console.log(‘CapacitorSQLite:’, CapacitorSQLite);
console.log(‘Checking IndexedDB support:’, !!window.indexedDB);

if (Capacitor.getPlatform() === 'web') {
  console.log('Running on web, initializing web store...');
  try {
    console.log("tryblock")
   await this.sqlite.initWebStore();
    console.log('Web store initialized successfully');
  } catch (webInitErr) {
    console.error('Error during initWebStore:', webInitErr);
  }
}

console.log(‘Before creating connection…’);

this.db = await this.sqlite.createConnection('testdb', false, 'no-encryption', 1, false);
const isOpen = await this.db.isDBOpen();

console.log(‘DB is open?’, isOpen);

await this.db.open();
console.log(' creating connection...');

console.log('DB connection opened');

await this.db.execute(`
  CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    name TEXT
  )
`);
console.log('Table created or exists');

} catch (err) {
console.error(‘SQLite init error:’, err);
}
}

async addUser(name: string) {
if (!this.db) {
console.error(‘DB not initialized’);
return;
}
const stmt = INSERT INTO users (name) VALUES (?);
const result = await this.db.run(stmt, [name]);
console.log(‘Insert Result:’, result);
}

async getUsers() {
if (!this.db) return ;
const result = await this.db.query(‘SELECT * FROM users’);
return result.values ?? ;
}
}
appcomponent.ts

import { Component } from ‘@angular/core’;
import { Platform } from ‘@ionic/angular’;
import { SqliteService } from ‘./services/sqlite.service’;

@Component({
selector: ‘app-root’,
templateUrl: ‘app.component.html’,
styleUrls: [‘app.component.scss’],
standalone: false,
})
export class AppComponent {
constructor(private platform: Platform, private sqliteService: SqliteService) {
this.platform.ready().then(async () => {
console.log(‘Platform is ready!’);
await this.sqliteService.init();
console.log(‘SQLite service initialized!’);
await this.addUser();
});
}
async addUser() {
await this.sqliteService.addUser(‘John’);
const users = await this.sqliteService.getUsers();
console.log(users);
}
}
main.ts

import { platformBrowserDynamic } from ‘@angular/platform-browser-dynamic’;
import { AppModule } from ‘./app/app.module’;

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
connect sqlite time jeep-sqlite not present in Dom message showing table not created

Please don’t create duplicate posts. You can reply to your previous one adding additional info or edit your original post.

Also, please use proper code blocks - Extended Syntax | Markdown Guide.