@@ -590,12 +590,22 @@ void OpenTableForEditing(const char *tablename) {
590590 char * errmsg = NULL ;
591591 wchar_t wbuf [64 ];
592592 DWORD startTick , elapsed ;
593+ char * * results = NULL ;
594+ int nRows = 0 , nCols = 0 ;
595+ int i , total ;
593596
594597 if (!g_db || !tablename ) return ;
595598
596599 /* Clear any previous edit state */
597600 ClearEditMode ();
598601
602+ /* Load column metadata first - needed for empty tables */
603+ LoadColumnMetadata (tablename );
604+ if (g_colMetaCount < 1 ) {
605+ SetStatusResult (L"Error: no columns found" );
606+ return ;
607+ }
608+
599609 /* Build SELECT rowid, * FROM tablename */
600610 s = "SELECT rowid, * FROM " ;
601611 while (* s ) * p ++ = * s ++ ;
@@ -609,83 +619,100 @@ void OpenTableForEditing(const char *tablename) {
609619
610620 startTick = GetTickCount ();
611621
612- /* Use sqlite_get_table for simplicity */
613- {
614- char * * results = NULL ;
615- int nRows = 0 , nCols = 0 ;
616-
617- rc = sqlite_get_table (g_db , sql , & results , & nRows , & nCols , & errmsg );
618-
619- elapsed = GetTickCount () - startTick ;
620- g_lastQueryTime = elapsed ;
621-
622- if (rc == SQLITE_OK && results ) {
623- int i , total ;
624-
625- /* Free previous results */
626- FreeLastResults ();
627-
628- /* Store results - includes rowid as column 0 */
629- g_lastResultRows = nRows ;
630- g_lastResultCols = nCols ;
631- total = (nRows + 1 ) * nCols ;
632-
633- g_lastResult = (char * * )LocalAlloc (LMEM_FIXED , total * sizeof (char * ));
634- if (g_lastResult ) {
635- for (i = 0 ; i < total ; i ++ ) {
636- if (results [i ]) {
637- int len = 0 ;
638- const char * src = results [i ];
639- while (src [len ]) len ++ ;
640- g_lastResult [i ] = (char * )LocalAlloc (LMEM_FIXED , len + 1 );
641- if (g_lastResult [i ]) {
642- int j ;
643- for (j = 0 ; j <= len ; j ++ ) g_lastResult [i ][j ] = results [i ][j ];
644- }
645- } else {
646- g_lastResult [i ] = NULL ;
647- }
648- }
622+ rc = sqlite_get_table (g_db , sql , & results , & nRows , & nCols , & errmsg );
623+
624+ elapsed = GetTickCount () - startTick ;
625+ g_lastQueryTime = elapsed ;
626+
627+ if (rc != SQLITE_OK ) {
628+ if (errmsg ) {
629+ OutputLine (errmsg );
630+ sqlite_freemem (errmsg );
631+ }
632+ FreeColumnMetadata ();
633+ SetStatusResult (L"Error opening table" );
634+ return ;
635+ }
636+
637+ /* Free previous results */
638+ FreeLastResults ();
639+
640+ /* Handle empty table: nCols=0 from sqlite_get_table, use metadata */
641+ if (nCols == 0 ) {
642+ nCols = g_colMetaCount + 1 ; /* +1 for rowid */
643+ }
644+
645+ /* Store results - includes rowid as column 0 */
646+ g_lastResultRows = nRows ;
647+ g_lastResultCols = nCols ;
648+ total = (nRows + 1 ) * nCols ;
649+
650+ g_lastResult = (char * * )LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT , total * sizeof (char * ));
651+ if (g_lastResult ) {
652+ /* Build header row from column metadata if no results */
653+ if (!results || nRows == 0 ) {
654+ /* First column is rowid */
655+ g_lastResult [0 ] = (char * )LocalAlloc (LMEM_FIXED , 6 );
656+ if (g_lastResult [0 ]) {
657+ g_lastResult [0 ][0 ] = 'r' ; g_lastResult [0 ][1 ] = 'o' ;
658+ g_lastResult [0 ][2 ] = 'w' ; g_lastResult [0 ][3 ] = 'i' ;
659+ g_lastResult [0 ][4 ] = 'd' ; g_lastResult [0 ][5 ] = '\0' ;
649660 }
650- sqlite_free_table (results );
651-
652- /* Save current grid view state before entering edit mode */
653- g_gridViewBeforeEdit = g_gridView ;
654-
655- /* Load column metadata for edit mode */
656- LoadColumnMetadata (tablename );
657-
658- /* Set edit mode */
659- g_editMode = 1 ;
660- s = tablename ;
661- p = g_editTableName ;
662- while (* s && (p - g_editTableName ) < 127 ) * p ++ = * s ++ ;
663- * p = '\0' ;
664-
665- /* Update status */
666- {
667- wchar_t wtbl [128 ];
668- MultiByteToWideChar (CP_ACP , 0 , g_editTableName , -1 , wtbl , 128 );
669- wsprintfW (wbuf , L"Editing: %s (%d rows)" , wtbl , nRows );
661+ /* Remaining columns from metadata */
662+ for (i = 0 ; i < g_colMetaCount ; i ++ ) {
663+ int len = 0 ;
664+ const char * src = g_colMeta [i ].name ;
665+ while (src [len ]) len ++ ;
666+ g_lastResult [i + 1 ] = (char * )LocalAlloc (LMEM_FIXED , len + 1 );
667+ if (g_lastResult [i + 1 ]) {
668+ int j ;
669+ for (j = 0 ; j <= len ; j ++ ) g_lastResult [i + 1 ][j ] = src [j ];
670+ }
670671 }
671- SetStatusResult (wbuf );
672-
673- /* Populate grid and switch to grid view */
674- g_gridView = 1 ;
675- PopulateGrid ();
676- SwitchView (VIEW_RESULT );
677-
678- /* Disable grid/text toggle button while in edit mode */
679- SendMessage (g_hwndCB , TB_ENABLEBUTTON , IDM_EXECATCURSOR , FALSE);
680- SendMessage (g_hwndCB , TB_CHECKBUTTON , IDM_EXECATCURSOR , TRUE);
681672 } else {
682- if (errmsg ) {
683- OutputLine (errmsg );
684- sqlite_freemem (errmsg );
673+ /* Copy from results */
674+ for (i = 0 ; i < total ; i ++ ) {
675+ if (results [i ]) {
676+ int len = 0 ;
677+ const char * src = results [i ];
678+ while (src [len ]) len ++ ;
679+ g_lastResult [i ] = (char * )LocalAlloc (LMEM_FIXED , len + 1 );
680+ if (g_lastResult [i ]) {
681+ int j ;
682+ for (j = 0 ; j <= len ; j ++ ) g_lastResult [i ][j ] = results [i ][j ];
683+ }
684+ }
685685 }
686- SetStatusResult (L"Error opening table" );
687686 }
688687 }
688+ if (results ) sqlite_free_table (results );
689+
690+ /* Save current grid view state before entering edit mode */
691+ g_gridViewBeforeEdit = g_gridView ;
692+
693+ /* Set edit mode */
694+ g_editMode = 1 ;
695+ s = tablename ;
696+ p = g_editTableName ;
697+ while (* s && (p - g_editTableName ) < 127 ) * p ++ = * s ++ ;
698+ * p = '\0' ;
699+
700+ /* Update status */
701+ {
702+ wchar_t wtbl [128 ];
703+ MultiByteToWideChar (CP_ACP , 0 , g_editTableName , -1 , wtbl , 128 );
704+ wsprintfW (wbuf , L"Editing: %s (%d rows)" , wtbl , nRows );
705+ }
706+ SetStatusResult (wbuf );
707+
708+ /* Populate grid and switch to grid view */
709+ g_gridView = 1 ;
710+ PopulateGrid ();
711+ SwitchView (VIEW_RESULT );
712+
713+ /* Disable grid/text toggle button while in edit mode */
714+ SendMessage (g_hwndCB , TB_ENABLEBUTTON , IDM_EXECATCURSOR , FALSE);
715+ SendMessage (g_hwndCB , TB_CHECKBUTTON , IDM_EXECATCURSOR , TRUE);
689716}
690717
691718/*============================================================================
0 commit comments