This is a sample of howto Use Postgresql with Marinas-GUI
Is the same clients example adapted to use Tposgres classes from HBPGSQL library
It shows howto connect to an existing database , create a table and edit or delete records from it.
Note that you need to put your database , user and password in the correct variables.
Probably somebody could improve this sample , I am not an SQL guru....
Improvments and corrections would be accepted
Now Updated ( code reduced and formated using hbformat )
Este es un ejemplo de como usar Postgresql con Marinas-GUI
Es el mismo ejemplo clients adaptado para usar las clases Tpostgres provistas por la libreria HBPGSQL
Muestra como conectarse a una base de datos existente , crear una tabla y editar o borrar registros.
Notese que debera colocar los datos correctos de login y base de datos en las variables correspondientes.
Probablemente alguien pueda mejorar este ejemplo , no soy ningun guru del SQL ...
Se aceptan correcciones y mejoras :-)
Ahora fue actualizado ( codigo fue reducido y formateado para mejor lectura con hbformat )
Contact: bruno.luciani at gmail dot com
Here you can donwload prg code an Mip file to compile in Marinas-IDE
Take care with library names in windows , this project was tested on Linux Ubuntu 10.04 , some library
could have diferent name and location.
Aqui pueden descargar el codigo prg y su correpondiente archivo MIP para compilar en Marinas-IDE
Tengan en cuenta que el proyecto fue probado en Linux Ubuntu 10.04 , y las librerias a agregar en Windows
pueden diferir su nombre y ubicacion , especialmente la de postgres.
Linux
Download : ClientsPG.rar
Windows
Download : ClientsPGWIN.rar
clientspg.prg - Postgres Database example for marinas | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
/* * Borrowed from simple.prg 14094 2010-03-07 18:03:17Z vszakats $ * Adapted for Marinas-GUi by Bruno Luciani */ #include "marinas-gui.ch" #include "common.ch" #define DEMOTITLE "Multiplatform Postgres Demo" REQUEST HB_LANG_ESWIN REQUEST HB_CODEPAGE_ESWIN Function Main () private oServer, oQuery, oRow, cQuery cHost:="localhost" cDatabase:="database" cUser:="user" cPass:="password" conectar() HB_CDPSELECT('ESWIN') HB_LANGSELECT('ESWIN') REQUEST DBFCDX , DBFFPT RDDSETDEFAULT( "DBFCDX" ) SET ENTERTAB .T. // Replace Minigui SET NAVIGATION EXTENDED lNewRecord := .F. ANNOUNCE RDDSYS REQUEST DBFCDX , DBFFPT RDDSETDEFAULT( "DBFCDX" ) SET FILECASE LOWER SET DIRCASE LOWER SET DELETE ON SET DECIMALS TO 3 SET FIXED ON SET DATE BRITISH SET DATE FORMAT TO "dd/mm/yyyy" SET SCOREBOARD OFF SET AUTOPEN OFF // Main Window Creation CREATE WINDOW d_window ROW 0 COL 0 WIDTH 800 HEIGHT 600 CAPTION DEMOTITLE ONINIT {|| mg_Set( "d_window" , "Style_Grid_1" , "value" , 1 ) } ONRELEASE {|| closebase() } ICON "resource/d_test.png" MAIN .T. mainmenu() mtoolbar() mstatusbar() creategrid() createbase() refresh() buttons() END WINDOW mg_Do( "d_window" , "center" ) mg_Do( "d_window" , "activate" ) Return .T. //Main menu creation function Function mainmenu() CREATE MAIN MENU OF d_window CREATE POPUP "&ABM" CREATE ITEM "New Record" ONCLICK new() ITEMNAME item1 FONTNAME "mg_monospace" PICTURE "resource/d_test.png" END ITEM CREATE ITEM "Edit Record" ONCLICK edit() FONTNAME "mg_monospace" PICTURE "resource/d_test.png" END ITEM SEPARATOR CREATE ITEM "Delete Record" ONCLICK DeleteItem() FONTNAME "mg_monospace" CHECKED .T. END ITEM SEPARATOR CREATE ITEM "Exit" ONCLICK mg_Do( mg_GetLastActivatedWindowName() , "release" ) PICTURE "resource/d_test_stop.png" END ITEM END POPUP CREATE POPUP "M&enu 2" CREATE ITEM "Item 1" ONCLICK mg_MsgInfo( "Click from Item 1" ) PICTURE "resource/d_test.png" END ITEM CREATE ITEM "Item 2" ONCLICK mg_MsgInfo( "Click from Item 2" ) END ITEM CREATE ITEM "Item 3 (CheckMark)" ONCLICK mg_MsgInfo( "Click from Item 3" ) END ITEM END POPUP CREATE POPUP "&Help" CREATE ITEM "About" ONCLICK { || mg_MsgInfo("Marinas Version : "+mg_Version() ) } PICTURE "resource/d_test.png" END ITEM END POPUP END MENU Return .T. // Toolbar creation function Function mtoolbar() CREATE TOOLBAR myTollbar TOOLTIP "ToolBar ToolTip" CREATE TOOLBUTTON ToolBar_Button_1 CAPTION "Exit Demo" TOOLTIP "Button Exit Tooltip" ONCLICK mg_Do( "d_window" , "release" ) PICTURE "resource/d_test_stop.png" END TOOLBUTTON TOOLBARSEPARATOR CREATE TOOLBUTTON ToolBar_Button_2 CAPTION "Data Entry" TOOLTIP "Start Modal Window" ONCLICK new() PICTURE "resource/d_window_modal.png" END TOOLBUTTON TOOLBARSEPARATOR CREATE TOOLBUTTON ToolBar_Button_4 CAPTION "About" TOOLTIP "Button About Tooltip" ONCLICK mg_MsgInfo( mg_Version() ) PICTURE "resource/d_test.png" END TOOLBUTTON TOOLBARSEPARATOR END TOOLBAR Return .T. // StatusBar creation function Function mstatusbar() CREATE STATUSBAR CAPTION "Marinas-Gui StatusBar Ready !!!" TOOLTIP "StatusBar ToolTip" END STATUSBAR Return .T. // Edit window ( modal ) // Funcion que crea el Grid para el despligue de los datos de la base clientes Function CreateGrid() CREATE GRID Style_Grid_1 ROW 20 COL 40 WIDTH 680 HEIGHT 300 VALUE 1 COLUMNWIDTHALL {30,60,150,90,120,120,90} COLUMNALIGNALL {"Qt_AlignRight","CENTER","CENTER","CENTER","CENTER","CENTER","CENTER"} BACKCOLOR {255,223,255} SELECTIONBACKCOLOR {255,0,0} COLUMNHEADERALL {'Rec','Code','Name','Telephone ','Adress','City','Start Date'} END GRID return .T. // Main window edit button's creation function buttons() CREATE BUTTON Style_Button_4 ROW 450 COL 540 WIDTH 60 HEIGHT 28 CAPTION "New" ONCLICK new() END BUTTON CREATE BUTTON Style_Button_5 ROW 450 COL 620 WIDTH 60 HEIGHT 28 CAPTION "Edit" ONCLICK Edit() END BUTTON CREATE BUTTON Style_Button_6 ROW 450 COL 700 WIDTH 60 HEIGHT 28 CAPTION "Delete" ONCLICK DeleteItem() END BUTTON return .T. // Database Creation if not exist Function createbase() IF oServer:TableExists( "clients" ) if mg_msgyesno("Do you like to start using a fresh database ?") oQuery := oServer:Execute( "DROP TABLE clients") // mg_log(oQuery := oServer:Execute( "DROP TABLE clients" ),.T.,.T.,.T.) oQuery:Destroy() Else return endif endif cQuery := "CREATE TABLE clients(" cQuery += " Codigo integer not null primary key, " cQuery += " Nombre Varchar(40), " cQuery += " Telefono varchar(15), " cQuery += " Direccion varchar(30), " cQuery += " Ciudad varchar(30), " cQuery += " Fecha Date ) " oQuery := oServer:Query( cQuery ) IF oQuery:neterr() mg_log( hb_utf8tostr(oQuery:ErrorMsg()),.T.,.T.,.T. ) ENDIF oQuery:Destroy() oServer:StartTransaction() FOR i := 1 TO 50 cQuery := "INSERT INTO clients(codigo, nombre, telefono, direccion, ciudad, fecha) " +; "VALUES( " + str( i+1000 ) + " ,'Giuseppe Juliani','004568989899','Via Romana 8989','Florence','30-04-2011'"+" )" // mg_log:=(cQuery,.T.,.T.,.T.) oQuery := oServer:Query( cQuery ) IF oQuery:neterr() mg_log(hb_utf8tostr(oQuery:errorMsg()),.T.,.T.,.T.) ENDIF oQuery:destroy() NEXT oServer:Commit() return .T. //New Record function /////////////// FUNCTION new() lNewRecord := .T. load() refresh() RETURN // Data edit function FUNCTION edit() lNewRecord := .F. load() refresh() RETURN FUNCTION load() LOCAL item_grid := Array( 0 ) IF !( lNewRecord ) registro := mg_Get( "d_window", "Style_Grid_1", "value" ) IF registro = 0 mg_msginfo( "You need to select an item to edit", "Error Editing !" ) RETURN ENDIF item_grid := item_grid := mg_Get( "d_window" , "Style_Grid_1" , "item" , registro ) nRecord := item_grid[1] // mg_log(str(nRecord)) oQuery := oServer:Query( "SELECT codigo, nombre, telefono, direccion, ciudad , fecha FROM Clients" ) oQuery:goto( nRecord ) ENDIF CREATE WINDOW Modal_1 ROW 0 ; COL 0 WIDTH 400 ; HEIGHT 300 CAPTION "Data Entry "+" - " + Time() BACKCOLOR { 219 , 244 , 208 } MODAL .T. CREATE KEY Plus ACTION {|| mg_Do( cWin , "Style_Button_1" , "ONCLICK" ) } END KEY CREATE FRAMEBOX Style_Frame_1 ROW 5 COL 10 WIDTH 375 HEIGHT 275 CAPTION "Client" END FRAMEBOX CREATE BUTTON Style_Button_1 ROW 230 COL 310 WIDTH 60 HEIGHT 28 CAPTION "Save" ONCLICK SaveData() END BUTTON * Aca van los textbox CREATE LABEL Label_1 ROW 20 COL 20 VALUE "Code:" AUTOSIZE .T. TRANSPARENT .F. TOOLTIP "Label Tooltip" END LABEL CREATE TEXTBOX Text_1 ROW 20 COL 80 WIDTH 100 HEIGHT 24 ALIGN Qt_AlignRight TOOLTIP "Client Number" UPPERCASE .T. MAXLENGTH 5 * BACKCOLOR {0,0,0,} * VALUE "" END TEXTBOX CREATE LABEL Label_2 ROW 50 COL 20 VALUE "Name:" AUTOSIZE .T. TRANSPARENT .F. TOOLTIP "Label Tooltip" END LABEL CREATE TEXTBOX Text_2 ROW 50 COL 80 WIDTH 200 HEIGHT 24 ALIGN Qt_AlignLeft TOOLTIP "Client Name" UPPERCASE .T. MAXLENGTH 30 * BACKCOLOR {0,0,0,} * VALUE "" END TEXTBOX CREATE LABEL Label_3 ROW 80 COL 20 VALUE "Telephone:" AUTOSIZE .T. TRANSPARENT .F. TOOLTIP "Label Tooltip" END LABEL CREATE TEXTBOX Text_3 ROW 80 COL 80 WIDTH 150 HEIGHT 24 ALIGN Qt_AlignLeft TOOLTIP "Client Telephone" UPPERCASE .T. MAXLENGTH 13 * BACKCOLOR {0,0,0,} * VALUE "" END TEXTBOX CREATE LABEL Label_4 ROW 110 COL 20 VALUE "Adress:" AUTOSIZE .T. TRANSPARENT .F. TOOLTIP "Label Tooltip" END LABEL CREATE TEXTBOX Text_4 ROW 110 COL 80 WIDTH 300 HEIGHT 24 ALIGN Qt_AlignLeft TOOLTIP "Client Adress" UPPERCASE .T. MAXLENGTH 30 * BACKCOLOR {0,0,0,} * VALUE "" END TEXTBOX CREATE LABEL Label_5 ROW 140 COL 20 VALUE "City:" AUTOSIZE .T. TRANSPARENT .F. TOOLTIP "Label Tooltip" END LABEL CREATE TEXTBOX Text_5 ROW 140 COL 80 WIDTH 300 HEIGHT 24 ALIGN Qt_AlignLeft TOOLTIP "Client city" UPPERCASE .T. MAXLENGTH 30 * BACKCOLOR {0,0,0,} * VALUE "" END TEXTBOX CREATE LABEL Label_6 ROW 170 COL 20 VALUE "Fecha Ing.:" AUTOSIZE .T. TRANSPARENT .F. TOOLTIP "Label Tooltip" END LABEL CREATE TEXTBOX Text_6 ROW 170 COL 80 WIDTH 100 HEIGHT 24 ALIGN Qt_AlignRight TOOLTIP "Client start date " UPPERCASE .T. MAXLENGTH 8 * BACKCOLOR {0,0,0,} VALUE dtoc(date()) END TEXTBOX if !(lNewRecord) oRow := oQuery:getrow() mg_Set("Modal_1","text_1","value",str( oQuery:Fieldget( 1 ))) mg_Set("Modal_1","text_2","value", oQuery:Fieldget( 2 )) mg_Set("Modal_1","text_3","value", oQuery:Fieldget( 3 )) mg_Set("Modal_1","text_4","value", oQuery:Fieldget( 4 )) mg_Set("Modal_1","text_5","value", oQuery:Fieldget( 5 )) mg_Set("Modal_1","text_6","value",Dtoc( oQuery:Fieldget( 6 ))) endif mg_Do("Modal_1","text_1","setfocus") END WINDOW mg_Do( "Modal_1" , "center" ) mg_Do( "Modal_1" , "activate" ) return .T. // Save new or edited data , depends on lNewRecord variable FUNCTION SaveData() IF ( lNewRecord ) oRow := oQuery:Blank() ENDIF oRow:FieldPut( 1, Val( mg_get("Modal_1","text_1","value" ) ) ) oRow:FieldPut( 2, mg_Get( "Modal_1","text_2","value" ) ) oRow:FieldPut( 3, mg_Get( "Modal_1","text_3","value" ) ) oRow:FieldPut( 4, mg_Get( "Modal_1","text_4","value" ) ) oRow:FieldPut( 5, mg_Get( "Modal_1","text_5","value" ) ) oRow:FieldPut( 6, CToD( mg_Get("Modal_1","text_6","value" ) ) ) IF ( lNewRecord ) oQuery:Append( oRow ) ELSE oQuery:Update( oRow ) ENDIF IF oQuery:NetErr() mg_log( hb_utf8tostr( oQuery:errorMsg() ), .T. , .F. , .F. ) ENDIF oQuery:Destroy() mg_Do( "Modal_1", "Release" ) RETURN .T. // Connect funtion to database in Postgres SQL server FUNCTION conectar() oServer := TPQServer():New( cHost, cDatabase, cUser, cPass ) IF oServer:NetErr() mg_log( hb_utf8tostr( oServer:errorMsg() ), .T. , .T. , .T. ) QUIT ENDIF oServer:SetVerbosity( 2 ) oServer:traceon( "simple.log" ) RETURN // Open base function and index check FUNCTION openbase() conectar() RETURN .T. // close database on main window release FUNCTION closebase() oServer:Destroy() mg_msginfo( "Server Disconnected !", "Postgres Server" ) RETURN .T. // Grid Data Refresh FUNCTION refresh() LOCAL aStruct mg_Do( "d_window", "Style_Grid_1", "deleteallitems" ) oQuery := oServer:Query( "SELECT codigo, nombre, telefono, direccion, ciudad , fecha FROM Clients ORDER by codigo" ) DO WHILE ! oQuery:Eof() aData := { oQuery:RecNo(), oQuery:FieldGet( 1 ), oQuery:FieldGet( 2 ), oQuery:FieldGet( 3 ), oQuery:FieldGet( 4 ), oQuery:FieldGet( 5 ), oQuery:FieldGet( 6 ) } mg_Do( "d_window", "Style_Grid_1", "additem", aData ) oQuery:Skip() ENDDO oQuery:Destroy() RETURN .T. // Item delete from Database function FUNCTION DeleteItem() LOCAL item_grid := Array( 0 ) registro := mg_Get( "d_window", "Style_Grid_1", "value" ) IF registro = 0 mg_msginfo( "You need to select an Item to delete", "Error deleting !" ) RETURN ENDIF item_grid := mg_Get( "d_window" , "Style_Grid_1" , "item" , registro ) nRecord := item_grid[1] mg_msginfo( "You try to delete Client " + hb_osnewline() + "Client: " + item_grid[3] + hb_osnewline() + "Record number: " + Str( item_grid[1] ) ) oQuery := oServer:Query( "SELECT codigo, nombre, telefono, direccion, ciudad , fecha FROM Clients" ) oQuery:goto( nRecord ) oRow := oQuery:getrow() oQuery:Delete( oRow ) IF oQuery:NetErr() mg_log( hb_utf8tostr( oQuery:errorMsg() ), .T. , .F. , .F. ) ENDIF oQuery:Destroy() refresh() RETURN |