{"record":{"id":"0b70b70ed35ac12d","repo":"chenhg5/cc-connect","slug":"query-cc-switch-db-w","errorCode":null,"errorMessage":"query cc-switch db: %w","messagePattern":"query cc-switch db: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cc-connect/provider.go","lineNumber":332,"sourceCode":"// queryCCSwitchDB opens the cc-switch SQLite database and returns provider rows.\n// appTypeFilter can be empty (return all) or \"claude\"/\"codex\".\nfunc queryCCSwitchDB(dbPath, appTypeFilter string) ([]ccSwitchRow, error) {\n\tdb, err := sql.Open(\"sqlite\", dbPath+\"?mode=ro\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"open cc-switch db: %w\", err)\n\t}\n\tdefer db.Close()\n\n\tquery := \"SELECT id, app_type, name, settings_config, is_current FROM providers\"\n\tvar args []any\n\tif appTypeFilter != \"\" {\n\t\tquery += \" WHERE app_type = ?\"\n\t\targs = append(args, appTypeFilter)\n\t}\n\n\trows, err := db.Query(query, args...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"query cc-switch db: %w\", err)\n\t}\n\tdefer rows.Close()\n\n\tvar result []ccSwitchRow\n\tfor rows.Next() {\n\t\tvar r ccSwitchRow\n\t\tif err := rows.Scan(&r.ID, &r.AppType, &r.Name, &r.SettingsConfig, &r.IsCurrent); err != nil {\n\t\t\tcontinue\n\t\t}\n\t\tresult = append(result, r)\n\t}\n\treturn result, rows.Err()\n}\n\nfunc convertCCSwitchProvider(row ccSwitchRow) (config.ProviderConfig, error) {\n\tvar sc map[string]any\n\tif err := json.Unmarshal([]byte(row.SettingsConfig), &sc); err != nil {\n\t\treturn config.ProviderConfig{}, fmt.Errorf(\"invalid settings_config JSON: %w\", err)","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/cmd/cc-connect/provider.go#L314-L350","documentation":"The SQL SELECT against the cc-switch providers table returned an error from db.Query. This indicates the query could not be executed — typically a schema mismatch (no `providers` table or missing columns like settings_config/is_current) or the database is locked/corrupt.","triggerScenarios":"queryCCSwitchDB executes `SELECT id, app_type, name, settings_config, is_current FROM providers [WHERE app_type = ?]` and the sqlite driver returns an error — older cc-switch schema without those columns, wrong file passed (not a cc-switch DB), or locked database.","commonSituations":"Pointing dbPath at a different sqlite file that has no providers table; cc-switch upgraded/downgraded changing the schema; concurrent access locking the DB; corrupted sqlite file.","solutions":["Confirm dbPath points to the actual cc-switch database containing a `providers` table (`sqlite3 file .tables`).","Check the table has columns id, app_type, name, settings_config, is_current; migrate or update cc-switch if the schema differs.","Retry if the DB was temporarily locked by another process.","Inspect the unwrapped sqlite error for `no such table`/`database is locked` hints."],"exampleFix":"// before\nrows, err := db.Query(query, args...)\nif err != nil {\n    return nil, fmt.Errorf(\"query cc-switch db: %w\", err)\n}\n// after\nrows, err := db.Query(query, args...)\nif err != nil {\n    if strings.Contains(err.Error(), \"no such table\") {\n        return nil, fmt.Errorf(\"query cc-switch db: %s does not look like a cc-switch database: %w\", dbPath, err)\n    }\n    return nil, fmt.Errorf(\"query cc-switch db: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"db, err := sql.Open(\"sqlite\", dbPath+\"?mode=ro\")\nif err != nil { return err }\nvar n int\nif err := db.QueryRow(\"SELECT count(*) FROM sqlite_master WHERE type='table' AND name='providers'\").Scan(&n); err != nil || n == 0 {\n    return fmt.Errorf(\"%s has no providers table (not a cc-switch db?)\", dbPath)\n}","typeGuard":null,"tryCatchPattern":"rows, err := queryCCSwitchDB(dbPath, filter)\nif err != nil {\n    var qErr error\n    if strings.Contains(err.Error(), \"query cc-switch db\") { qErr = err }\n    slog.Error(\"cc-switch query failed\", \"db\", dbPath, \"err\", qErr)\n    return fmt.Errorf(\"query cc-switch db: %w\", err)\n}\ndefer rows.Close() // always close on success path too","preventionTips":["Verify the schema with `sqlite3 <db> '.schema providers'` before importing.","Ensure no other process holds a write lock on the DB during import.","Point at the genuine cc-switch database file, not another sqlite file.","Keep cc-switch updated so schema stays compatible."],"tags":["go","sqlite","database","sql"],"backgroundTag":"sql-query-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}