Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateConnection: Connection already exists #615

Open
zagham-nadeem opened this issue Jan 21, 2025 · 0 comments
Open

CreateConnection: Connection already exists #615

zagham-nadeem opened this issue Jan 21, 2025 · 0 comments

Comments

@zagham-nadeem
Copy link

I am new to @capacitor-community/sqlite and really stucked at this error

when i run this code getting error connection already exists.
i also removed await this.initializeDatabase(); but when i removed this getting error undefined query
i have a database in my assets/databases/quran.db and want to load some data on. page 1 and after that data i want to load other data on page 2 please help me

`
"@angular/animations": "^19.0.0",
"@angular/common": "^19.0.0",
"@angular/compiler": "^19.0.0",
"@angular/core": "^19.0.0",
"@angular/fire": "^19.0.0-rc.0",
"@angular/forms": "^19.0.0",
"@angular/platform-browser": "^19.0.0",
"@angular/platform-browser-dynamic": "^19.0.0",
"@angular/router": "^19.0.0",
"@capacitor-community/sqlite": "^6.0.2",

`

import { Injectable } from '@angular/core';
import {
  CapacitorSQLite,
  SQLiteConnection,
  SQLiteDBConnection,
} from '@capacitor-community/sqlite';
import { Platform } from '@ionic/angular';

@Injectable({
  providedIn: 'root',
})
export class DatabaseService {
  private sqlite: SQLiteConnection;
  private db!: SQLiteDBConnection;
  private initialized:boolean = false;

  public constructor(private platform: Platform) {
    this.sqlite = new SQLiteConnection(CapacitorSQLite);
  }

  public async initializeDatabase():Promise<void> {
    if (this.initialized) return;

    try {
      // Copy database if on mobile platform
      if (this.platform.is('android') || this.platform.is('ios')) {
        await this.sqlite.copyFromAssets();
        // Create or open the database
        this.db = await this.sqlite.createConnection(
          'quran',
          false,
          'no-encryption',
          1,
          false
        );
        await this.db.open();
      }
      this.initialized = true;
    } catch (error) {
      console.error('Error initializing database:', error);
      throw error;
    }
  }

  public async getAllSurah(): Promise<any[]> {
    await this.initializeDatabase();
    const query = `
      SELECT * FROM surahs
    `;
    const result = await this.db.query(query);
    console.log(result);
    return result.values || [];
  }

  public async getSurah(surahNumber: number) {
    await this.initializeDatabase();
    const query = `
      SELECT * FROM surahs WHERE number = ?
    `;
    const result = await this.db.query(query, [surahNumber]);
    return result.values?.[0];
  }

  public async getAyahsForSurah(surahNumber: number) {
    await this.initializeDatabase();
    const query = `
      SELECT * FROM ayahs 
      WHERE surahNumber = ? 
      ORDER BY numberInSurah
    `;
    const result = await this.db.query(query, [surahNumber]);
    return result.values || [];
  }

  public async getAyahsByPage(pageNumber: number) {
    await this.initializeDatabase();
    const query = `
      SELECT a.*, s.name as surahName, s.englishName as surahEnglishName
      FROM ayahs a
      JOIN surahs s ON a.surahNumber = s.number
      WHERE a.page = ?
      ORDER BY a.number
    `;
    const result = await this.db.query(query, [pageNumber]);
    return result.values || [];
  }

  public async searchAyahs(searchText: string) {
    await this.initializeDatabase();
    const query = `
      SELECT a.*, s.name as surahName, s.englishName as surahEnglishName
      FROM ayahs a
      JOIN surahs s ON a.surahNumber = s.number
      WHERE a.text LIKE ? 
      OR a.englishTranslation LIKE ?
      ORDER BY a.number
      LIMIT 50
    `;
    const searchPattern = `%${searchText}%`;
    const result = await this.db.query(query, [searchPattern, searchPattern]);
    return result.values || [];
  }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant