Skip to content

bug: exportToJson: Error get tables 'Full' failed : Query result not well formatted #654

@davidgs

Description

@davidgs

Plugin version:

7.0.1

Platform(s):

iOS

Current behavior:

exportToJson: Error get tables 'Full' failed : Query result not well formatted

Expected behavior:

Succeed in exporting tables to JSON

Steps to reproduce:

 async exportToJson(mode = 'full') {
        console.log('[ sqliteLoader.js ] Exporting database to JSON...');
        return await sqlite.exportToJson({
          database: DB_NAME,
          jsonexportmode: mode
        });
      },

Related code:

Sample table:

    CREATE TABLE IF NOT EXISTS users (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT,
      lastName TEXT,
      phoneNumber TEXT,
      email TEXT,
      sobrietyDate TEXT,
      homeGroups TEXT,
      preferences TEXT,
      privacySettings TEXT,
      notificationSettings TEXT, -- JSON string for notification preferences
      createdAt TEXT DEFAULT CURRENT_TIMESTAMP,
      updatedAt TEXT DEFAULT CURRENT_TIMESTAMP,
      sql_deleted BOOLEAN DEFAULT 0 CHECK (sql_deleted IN (0, 1)),
      last_modified INTEGER DEFAULT (strftime('%s', 'now'))
    )
import { CapacitorSQLite } from '@capacitor-community/sqlite';

// Default database name for consistency
const DB_NAME = 'tracker.db';
/**
 * Initialize SQLite database
 * @returns {Promise<object>} Database connection object
 */
export default async function initSQLiteDatabase() {
  console.log('[ sqliteLoader.js:14 ] Initializing SQLite database via Capacitor...');

  try {
    const sqlite = CapacitorSQLite;

    if (!sqlite) {
      throw new Error('CapacitorSQLite plugin not found - this app requires SQLite for iOS');
    }

    console.log('[ sqliteLoader.js ]  Found CapacitorSQLite plugin:', !!sqlite);

    // Step 1: Create connection (check if already exists first)
    try {
      await sqlite.createConnection({
        database: DB_NAME,
        version: 1,
        encrypted: false,
        mode: 'no-encryption',
        readonly: false
      });
      console.log('[ sqliteLoader.js:50 ]  Database connection created');
    } catch (connectionError) {
      console.log('[ sqliteLoader.js:52 ]  Connection may already exist:', connectionError.message);
    }



    // Step 2: Open the database
    await sqlite.open({ database: DB_NAME, readonly: false });
    console.log('[ sqliteLoader.js:56 ]  Database opened successfully');

    // Step 3: Create sync table (required for CapacitorSQLite export/import operations)
    await sqlite.createSyncTable({ database: DB_NAME });
    console.log('[ sqliteLoader.js:58 ]  Sync table created successfully');
// Sync methods for iCloud integration
      async exportToJson(mode = 'full') {
        console.log('[ sqliteLoader.js ] Exporting database to JSON...');
        return await sqlite.exportToJson({
          database: DB_NAME,
          jsonexportmode: mode
        });
      },

      async importFromJson(jsonData) {
        console.log('[ sqliteLoader.js ] Importing database from JSON...');
        return await sqlite.importFromJson({
          jsonstring: JSON.stringify(jsonData)
        });
      },

      async setSyncDate(syncDate) {
        console.log('[ sqliteLoader.js ] Setting sync date...');
        return await sqlite.setSyncDate({
          database: DB_NAME,
          syncdate: syncDate
        });
      },

      async deleteExportedRows() {
        console.log('[ sqliteLoader.js ] Deleting exported rows...');
        return await sqlite.deleteExportedRows({
          database: DB_NAME
        });
      }
    };

  } catch (error) {
    console.error('[ sqliteLoader.js ] Database initialization failed:', error);
    throw error;
  }
}

Other information:

I have read through #59 and it is long-closed, but I am having this problem repeatedly and cannot find a fix for it.

Capacitor doctor:

% npx cap doctor
💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 7.4.2
  @capacitor/core: 7.4.2
  @capacitor/android: 7.4.2
  @capacitor/ios: 7.4.2

Installed Dependencies:

  @capacitor/cli: 7.4.2
  @capacitor/core: 7.4.2
  @capacitor/android: 7.4.2
  @capacitor/ios: 7.4.2
  
[success] iOS looking great! 👌
[error] gradlew file is missing in android
        AndroidManifest.xml is missing in app/src/main

Expected, since I'm not currently working on Android.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions