Author Topic: Script BASIC Windows  (Read 14607 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows
« on: November 13, 2015, 03:10:46 PM »
This series of posts show some of the features of Script BASIC for Windows. (32 and 64 bit)
« Last Edit: November 07, 2017, 08:04:21 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - SBx (IUP)
« Reply #1 on: November 14, 2015, 06:30:51 PM »
I have retired my XP VirtualBox and now have my Windows 32 bit development in a Windows 7 VirtualBox. I thought I would refresh some of my GUI examples under Windows 7.



SBx_buttons
Code: ScriptBasic
  1. ' SBx_buttons Example
  2.  
  3. INCLUDE "SBx"
  4.  
  5. SUB Btn1_clicked
  6.   PRINT "BUTTON 1 Event\n"
  7. END SUB
  8.  
  9. SUB Btn2_clicked
  10.   PRINT "BUTTON 2 Event\n"
  11. END SUB
  12.  
  13. SUB Btn3_clicked
  14.   PRINT "BUTTON 3 Event\n"
  15. END SUB
  16.  
  17. SUB Win_exit
  18.   Iup::ExitLoop = TRUE
  19. END SUB
  20.  
  21.  
  22. win = WINDOW()
  23. SETPROPERTY(win, "TITLE=\"SBx Buttons\", SIZE=300x")
  24. horzbox = HBOX()
  25. SETPROPERTY(horzbox, "GAP=5")
  26. btn1 = BUTTON()
  27. SETPROPERTY(btn1, "TITLE=Button1, EXPAND=HORIZONTAL")
  28. btn2 = BUTTON()
  29. SETPROPERTY(btn2, "TITLE=Button2, EXPAND=HORIZONTAL")
  30. btn3 = BUTTON()
  31. SETPROPERTY(btn3, "TITLE=Button3, EXPAND=HORIZONTAL")
  32. APPEND(horzbox, btn1)
  33. APPEND(horzbox, btn2)
  34. APPEND(horzbox, btn3)
  35. APPEND(win, horzbox)
  36. EVENT(win,"CLOSE_CB",ADDRESS(Win_exit()))
  37. EVENT(btn1,"ACTION",ADDRESS(Btn1_clicked()))
  38. EVENT(btn2,"ACTION",ADDRESS(Btn2_clicked()))
  39. EVENT(btn3,"ACTION",ADDRESS(Btn3_clicked()))
  40. SHOW(win)
  41.  

Console Output

C:\scriptbasic\examples>sbiup sbx_buttons
BUTTON 1 Event
BUTTON 2 Event
BUTTON 3 Event

« Last Edit: November 14, 2015, 08:48:52 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - Online Dictionary
« Reply #2 on: November 14, 2015, 06:51:01 PM »


Code: ScriptBasic
  1. IMPORT iup.bas
  2.  
  3. servers[0]="dict.org"
  4. servers[1]="dict1.us.dict.org"
  5. servers[2]="all.dict.org"
  6.  
  7. about="""This is a Demo
  8. of the IUP GUI Binding
  9. for Scriptbasic"""
  10.  
  11. ' Initialize IUP
  12. Iup::Open()
  13.  
  14. ' Create main window
  15.  
  16. win = Iup::Create("dialog")
  17.   Iup::SetAttributes(win, "TITLE=\"ScriptBasic IUP Online Dictionary\", SIZE=500x300")
  18.   Iup::SetCallback(win,"CLOSE_CB",ADDRESS(Win_exit()))
  19.  
  20. ' Create container to house ALL GUI objects
  21.  
  22. vbox = Iup::Create("vbox")
  23.   Iup::SetAttributes(vbox, "MARGIN=10x10")
  24.  
  25. ' Create server panel
  26.  
  27. topBox = Iup::Create("hbox")
  28.   Iup::SetAttributes(topBox, "GAP=10")
  29.   Iup::Append(vbox, topBox)
  30. serverFrame = Iup::Create("frame")
  31.   Iup::SetAttributes(serverFrame, "TITLE=Servers, EXPAND=YES")
  32.   Iup::Append(topBox, serverFrame)
  33. serverBox = Iup::Create("hbox")
  34.   Iup::SetAttributes(serverBox, "GAP=5")
  35.   Iup::Append(serverFrame, serverBox)
  36. serverCombo = Iup::Create("list")
  37.   Iup::SetAttributes(serverCombo, "DROPDOWN=YES, SIZE=120x, EXPAND=HORIZONTAL, VALUE=1")
  38.   Iup::Append(serverBox, serverCombo)
  39.   Iup::SetCallback(serverCombo, "ACTION", ADDRESS(serverCombo_selected()))
  40. btnFetch = Iup::Create("button")
  41.   Iup::SetAttributes(btnFetch, "TITLE=Fetch, SIZE = 50x")
  42.   Iup::Append(serverBox, btnFetch)
  43.   Iup::SetCallback(btnFetch, "ACTION", ADDRESS(btnFetch_clicked()))
  44.  
  45. ' Create control panel
  46.  
  47. controlFrame = Iup::Create("frame")
  48.   Iup::SetAttributes(controlFrame, "TITLE=Controls")
  49.   Iup::Append(topBox, controlFrame)
  50. controlBox = Iup::Create("hbox")
  51.   Iup::SetAttributes(controlBox, "GAP=5")
  52.   Iup::Append(controlFrame, controlBox)
  53. btnAbout = Iup::Create("button")
  54.   Iup::SetAttributes(btnAbout, "TITLE=About, SIZE = 50x")
  55.   Iup::Append(controlBox, btnAbout)
  56.   Iup::SetCallback(btnAbout, "ACTION", ADDRESS(btnAbout_clicked()))
  57. btnClear = Iup::Create("button")
  58.   Iup::SetAttributes(btnClear, "TITLE=Clear, SIZE = 50x")
  59.   Iup::Append(controlBox, btnClear)
  60.   Iup::SetCallback(btnClear, "ACTION", ADDRESS(btnClear_clicked()))
  61. btnExit = Iup::Create("button")
  62.   Iup::SetAttributes(btnExit, "TITLE=Exit, SIZE = 50x")
  63.   Iup::Append(controlBox, btnExit)
  64.   Iup::SetCallback(btnExit,"ACTION",ADDRESS(Win_exit()))
  65.  
  66. ' Create dictionary panel
  67.  
  68. dictFrame = Iup::Create("frame")
  69.   Iup::SetAttributes(dictFrame, "TITLE=Dictionaries")
  70.   Iup::Append(vbox, dictFrame)
  71. serverList = Iup::Create("list")
  72.   Iup::SetAttributes(serverList, "EXPAND=YES, VISIBLELINES=1")
  73.   Iup::Append(dictFrame, serverList)
  74.   Iup::SetCallback(serverList, "ACTION", ADDRESS(serverList_selected()))
  75.  
  76. ' Create text part
  77.  
  78. transFrame = IUP::Create("frame")
  79.   Iup::SetAttributes(transFrame, "TITLE=Translation")
  80.   Iup::Append(vbox, transFrame)
  81. text = Iup::Create("text")
  82.   Iup::SetAttributes(text, "MULTILINE=YES, EXPAND=YES")
  83.   Iup::Append(transFrame, text)
  84.  
  85. ' Create entry and search button
  86.  
  87. bottomBox = Iup::Create("hbox")
  88.   Iup::SetAttributes(bottomBox, "GAP=10")
  89.   Iup::Append(vbox, bottomBox)
  90. label = Iup::Create("label")
  91.   Iup::SetAttributes(label, "TITLE=\"Enter Word to Search For:\", SIZE=x12")
  92.   Iup::Append(bottomBox, label)
  93. entry = Iup::Create("text")
  94.   Iup::SetAttributes(entry, "EXPAND=HORIZONTAL")
  95.   Iup::Append(bottomBox, entry)
  96. btnSearch = Iup::Create("button")
  97.   Iup::SetAttributes(btnSearch,"TITLE=Search, SIZE=50x")
  98.   Iup::Append(bottomBox, btnSearch)
  99.   Iup::SetCallback(btnSearch, "ACTION", ADDRESS(btnSearch_clicked()))
  100. chkAll = Iup::Create("toggle")
  101.   Iup::SetAttributes(chkAll, "TITLE=ALL, SIZE=x12")
  102.   Iup::Append(bottomBox, chkAll)
  103. chkUTF = Iup::Create("toggle")
  104.   Iup::SetAttributes(chkUTF, "TITLE=UTF-8, SIZE=x12")
  105.   Iup::Append(bottomBox, chkUTF)
  106.  
  107. ' Add the main GUI container to the Window
  108.  
  109. Iup::Append(win, vbox)
  110.  
  111. ' Setup dialog defaults
  112.  
  113. Iup::Show(win)
  114. Iup::SetFocus(btnFetch)
  115. FOR i = 0 TO UBOUND(servers)
  116.   Iup::SetAttribute(serverCombo, "APPENDITEM", servers[i])
  117. NEXT
  118. Iup::SetAttribute(serverCombo, "VALUE", "1")
  119. Iup::Update(serverCombo)
  120. server_selection = servers[0]
  121.  
  122. ' Main processing loop
  123.  
  124. Iup::MainLoop()
  125. Iup::Close()
  126. END
  127.  
  128. ' Callback routines
  129.  
  130. SUB Win_exit
  131.   Iup::ExitLoop = TRUE
  132. END SUB
  133.  
  134. SUB btnAbout_clicked
  135.   Iup::Message("ABOUT", about)
  136. END SUB
  137.  
  138. SUB serverCombo_selected
  139.   server_selection = Iup::GetListText()
  140. END SUB
  141.  
  142. SUB serverList_selected
  143.   whichDictionary = Iup::GetListText()
  144. END SUB
  145.  
  146. SUB btnFetch_clicked
  147.   LOCAL dat, total, count
  148.   ON ERROR GOTO G_NetError
  149.   OPEN server_selection & ":2628" FOR SOCKET AS #1
  150.   PRINT#1,"SHOW DB\n"
  151.   LINE INPUT#1, dat
  152.   LINE INPUT#1, dat
  153.   count = 0
  154.   WHILE LEFT(dat, 1) <> "."
  155.     LINE INPUT#1, dat
  156.     IF LEFT(dat, 1) <> "." THEN total[count] = TRIM(dat)
  157.     count+=1
  158.   WEND
  159.   PRINT#1,"QUIT\n"
  160.   CLOSE(#1)
  161.   FOR cnt = 0 TO count - 2
  162.     Iup::SetAttribute(serverList, "APPENDITEM", total[cnt])
  163.   NEXT
  164.   Iup::SetAttribute(serverList, "VALUE", "1")
  165.   Iup::Update(serverCombo)
  166.   whichDictionary = total[0]
  167.   EXIT SUB
  168.  
  169.   G_NetError:
  170.   PRINT "Server ",server_selection," not available. (",ERROR,")\n"
  171. END SUB
  172.  
  173. SUB btnClear_clicked
  174.   Iup::ClearList(serverList)
  175.   Iup::SetAttribute(text, "VALUE", "")
  176.   Iup::SetAttribute(entry, "VALUE", "")
  177. END SUB
  178.  
  179. SUB btnSearch_clicked
  180.   LOCAL dict, dat, total, info
  181.   IUP::SetAttribute(text, "VALUE","Fetching....")
  182.   ON ERROR GOTO L_NetError
  183.   dict = LEFT(whichDictionary, INSTR(whichDictionary, " "))
  184.   OPEN server_selection & ":2628" FOR SOCKET AS 1
  185.   IF Iup::GetAttribute(chkAll, "VALUE") THEN
  186.     PRINT#1,"DEFINE * " & Iup::GetAttribute(entry,"VALUE") & "\n"
  187.   ELSE
  188.     PRINT#1,"DEFINE " & dict & " " & Iup::GetAttribute(entry,"VALUE") & "\n"
  189.   END IF
  190.   REPEAT
  191.     LINE INPUT#1, dat
  192.     IF LEFT(dat, 3) = "151" THEN
  193.       total$ &= "------------------------------\r\n"
  194.       total$ &= RIGHT(dat, LEN(dat) - LEN(Iup::GetAttribute(entry, "VALUE")) - LEN(dict))
  195.       total$ &= "------------------------------\r\n"
  196.       REPEAT
  197.         LINE INPUT#1, info
  198.         info = REPLACE(info, CHR(34), CHR(92) & CHR(34))
  199.         IF LEFT(info, 1) <> "." THEN total &= TRIM(info) & "\n"
  200.       UNTIL LEFT(info, 1) = "."
  201.       total &= "\n"
  202.     END IF
  203.   UNTIL LEFT(dat, 3) = "250" OR VAL(LEFT(dat, 3)) > 499
  204.   PRINT#1,"QUIT\n"
  205.   CLOSE(#1)
  206.   IF LEFT(dat, 3) = "552" THEN
  207.     total = "No match found."
  208.   ELSE IF LEFT(dat, 3) = "501" THEN
  209.     total = "Select a dictionary first!"
  210.   ELSE IF LEFT(dat, 3) = "550" THEN
  211.     total = "Invalid database!"
  212.   END IF
  213.   Iup::SetAttribute(text, "VALUE", total)
  214. EXIT SUB
  215.  
  216. L_NetError:
  217.   dat[0] = "Could not lookup word! (" & ERROR & ")"
  218.   Iup::SetAttribute(text, "VALUE", dat)
  219. END SUB
  220.  

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - Console CIO
« Reply #3 on: November 14, 2015, 07:03:18 PM »
Here is an example of using the Script BASIC CIO extension module to display available colors.



Code: ScriptBasic
  1. '
  2. ' This program lists all the possible console character
  3. ' colors on a windows console. This program can not
  4. ' be executed under UNIX
  5. '
  6. import cio.bas
  7.  
  8. cio::SetColor FWhite
  9. cio::cls
  10. cio::SetTitle "Testing console colors"
  11. for i=1 to 255
  12.   cio::gotoxy +(i \ 16) * 4 , +(i % 16) * 2
  13.   cio::gotoxy( (i \ 16) * 4 , +(i % 16) * 2 )
  14.   cio::gotoxy (i \ 16) * 4 , +(i % 16) * 2
  15.   cio::SetColor (i)
  16.   j = i
  17.   if i < 100 then j = "0" & j
  18.   print j
  19. next i
  20. cio::SetColor FWhite
  21. cio::SetCursor 0
  22. i = cio::getch()
  23.  

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - SDL_gfx
« Reply #4 on: November 14, 2015, 08:47:28 PM »
Here are a few SDL_gfx extension module examples.



Code: ScriptBasic
  1. ' ScriptBasic GFX - Mandelbrot
  2.  
  3. IMPORT gfx.inc
  4.  
  5. s = gfx::Window(640,480,"Script BASIC SDL_gfx Mandelbrot")
  6. ts = gfx::Time()
  7. FOR y = 0 TO 479
  8.   FOR x = 0 TO 639
  9.     cx = (x - 320) / 120
  10.     cy = (y - 240) / 120
  11.     rit = gfx::Mandelbrot(cx, cy, 510)
  12.     gfx::PixelRGBA s, x, y, rit * 32, rit * 16, rit * 8, 255
  13.   NEXT
  14. NEXT
  15. te = gfx::Time()
  16. gfx::stringColor s, 20, 15, "Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0x000000ff
  17. gfx::Update
  18. WHILE gfx::KeyName(1) <> "+escape"
  19. WEND
  20. gfx::Close
  21.  



Code: ScriptBasic
  1. ' ScriptBasic GFX - Alpha Circles
  2.  
  3. IMPORT gfx.inc
  4.  
  5. scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Circles")
  6. ' Random Value Arrays
  7. RANDOMIZE(gfx::Time())
  8. FOR i = 0 TO 512
  9.   rx[i] = RND() % 640
  10.   ry[i] = 60 + RND() % 480 - 80
  11.   rz[i] = RND() % 64
  12.   rr[i] = RND() AND  255
  13.   rg[i] = RND() AND  255
  14.   rb[i] = RND() AND  255
  15.   af = rx[i] / 640
  16.   ra[i] = INT(255 * af)
  17. NEXT
  18.  
  19. ts = gfx::Time()
  20. FOR i = 0 TO 512
  21.   gfx::filledCircleRGBA scrn, rx[i], ry[i], rz[i], rr[i], rg[i], rb[i], ra[i]
  22. NEXT
  23. te = gfx::Time()
  24. gfx::stringColor scrn, 20, 15, "Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
  25. gfx::Update
  26. WHILE gfx::KeyName(1) <> "+escape"
  27. WEND
  28. gfx::Close
  29.  



Code: ScriptBasic
  1. ' ScriptBasic GFX - Alpha Bezier Curve
  2.  
  3. IMPORT gfx.inc
  4.  
  5. ' Random Value Arrays
  6. scrn = gfx::Window(640, 480, "Script BASIC SDL_gfx - Alpha Bezier")
  7. RANDOMIZE(gfx::Time())
  8. FOR i = 0 TO 512
  9.   rx[i] = RND() % 640/2
  10.   rxx[i] = 640/2 + rx[i]  
  11.   ry[i] = 60 + RND() % 480 - 80
  12.   lw[i] = 2 + RND() % 7
  13.   rr[i] = RND() AND  255
  14.   rg[i] = RND() AND  255
  15.   rb[i] = RND() AND  255
  16.   af = rx[i] / 640
  17.   ra[i] = INT(255 * af)
  18. NEXT
  19. ts = gfx::Time()
  20. FOR i = 0 TO 512-3 STEP 3
  21.   a1[0] = rxx[i]
  22.   a1[1] = rxx[i + 1]
  23.   a1[2] = rxx[i + 2]
  24.   a2[0] = ry[i]
  25.   a2[1] = ry[i + 1]
  26.   a2[2] = ry[i + 2]
  27.   gfx::bezierRGBA scrn, a1, a2, 3, 100, rr[i], rg[i], rb[i], ra[i]
  28. NEXT
  29. te = gfx::Time()
  30. gfx::stringColor scrn, 20, 15,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
  31. gfx::Update
  32. WHILE gfx::KeyName(1) <> "+escape"
  33. WEND
  34. gfx::Close
  35.  



Code: ScriptBasic
  1. ' ScriptBasic GFX - Fonts & Rotation
  2.  
  3. IMPORT gfx.inc
  4. DECLARE SUB LoadFont ALIAS "loadstring" LIB "t"
  5.  
  6. font_5x7 = LoadFont("Fonts/5x7.fnt")
  7. font_6x10 = LoadFont("Fonts/6x10.fnt")
  8. font_7x13 = LoadFont("Fonts/7x13.fnt")
  9. font_9x15 = LoadFont("Fonts/9x15.fnt")
  10. font_10x20 = LoadFont("Fonts/10x20.fnt")
  11.  
  12. scrn = gfx::Window(640, 480, "ScriptBasic GFX - Font & Rotation")
  13. gfx::FontRotation 0
  14. gfx::SetFont font_5x7, 5, 7
  15. gfx::stringColor scrn, 20, 20,"Font 5x7" & CHR(0), 0xffffffff
  16. gfx::SetFont font_6x10, 6, 10
  17. gfx::stringColor scrn, 20, 30,"Font 6x10" & CHR(0), 0xffffffff
  18. gfx::SetFont font_7x13, 7, 13
  19. gfx::stringColor scrn, 20, 40,"Font 7x13" & CHR(0), 0xffffffff
  20. gfx::SetFont font_9x15, 9, 15
  21. gfx::stringColor scrn, 20, 55,"Font 9x15" & CHR(0), 0xffffffff
  22. gfx::SetFont font_10x20, 10, 20
  23. gfx::FontRotation 2
  24. gfx::stringColor scrn, 110, 70,"Font 10x20" & CHR(0), 0xffffffff
  25. gfx::Update
  26. WHILE gfx::KeyName(1) <> "+escape"
  27. WEND
  28. gfx::Close
  29.  
« Last Edit: November 14, 2015, 10:35:49 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - BBC Graphics
« Reply #5 on: November 14, 2015, 09:18:01 PM »
I can't find at the moment my Windows 32 bit of the BBC graphic extension module so here is the Linux 64 bit version as a place holder until I can find the Windows version.



Code: ScriptBasic
  1. ' UFO
  2.  
  3. IMPORT bbc.inc
  4.  
  5. BBC::OPEN "ScriptBasic BBC UFO"
  6.  
  7. t1 = BBC::TIME()
  8. BBC::MODE 31
  9. BBC::ORIGIN 800, 600
  10. xs = 2
  11. ys = 2
  12. BBC::GCOL 0, 14
  13. BBC::OFF
  14. a = 700
  15. b = A * A
  16. c = 600
  17. FOR x = 0 TO a STEP xs
  18.   s = x * x
  19.   p = SQR(b - s)
  20.   FOR i = -p TO p STEP 6 * ys
  21.     r = SQR(s + i * i) / a
  22.     q = (r - 1) * SIN(24 * r)
  23.     y = INT(i / 3 + q * c)
  24.     IF i = -p THEN
  25.       m = y
  26.       n = y
  27.     END IF
  28.     IF y > m THEN m = y
  29.     IF y < n THEN n = y
  30.     IF m = y OR n = y THEN
  31.       BBC::PLOT 69, NOT x, y
  32.       BBC::PLOT 69, x, y
  33.     END IF
  34.   NEXT
  35. NEXT
  36. t2 = BBC::TIME()
  37. t3 = (t2 - t1) / 1000
  38. BBC::OFF
  39. BBC::VDUSTR "Time: " & FORMAT("%.4f", t3) & " seconds."
  40. WHILE BBC::KEYNAME(1) <> "-escape"
  41. WEND
  42. BBC::CLOSE
  43.  
« Last Edit: November 14, 2015, 09:36:22 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - MySQL
« Reply #6 on: November 15, 2015, 05:30:23 PM »
Under Windows I install XAMPP to provide the Apache web server, MySQL database, Perl, PHP and a host of other tools and extensions. I'm using the original XAMPP branch that uses MySQL rather than MariaDB. (MySQL fork) The Script BASIC MySQL extension module won't work with MariaDB. I'm staying with the Oracle funded original that works with the SB driver.

Here is the XAMPP control panel in my Windows 7 VirtualBox.



With XAMPP comes phpMyAdmin which is a browser based database management tool and a gold standard when working with MySQL.

Table Structure


Table Query Results


This is an example of using the Script BASIC MySQL extension module interface to query the products table and return the first 10 rows. The example also has a few extended information functions that are handy when using the interface.

Code: ScriptBasic
  1. ' MySQL Test Program
  2.  
  3. INCLUDE mysql.bas
  4.  
  5. dbh = mysql::RealConnect("localhost","root","","classicmodels")
  6. mysql::query(dbh,"SELECT * FROM products LIMIT 10")
  7.  
  8. WHILE mysql::FetchHash(dbh,column)
  9.   PRINT column{"productCode"}," - ",column{"productName"},"\n"
  10. WEND
  11.  
  12. PRINTNL
  13. PRINT "The database handle is: ",dbh,"\n"
  14. PRINT "Affected rows by SELECT: ",mysql::AffectedRows(dbh),"\n"
  15. PRINT "Character set name is: ",mysql::CharacterSetName(dbh),"\n"
  16. PRINT "Last error is: ",mysql::ErrorMessage(dbh),"\n"
  17. PRINT "Client info is: ",mysql::GetClientInfo(),"\n"
  18. PRINT "Host info is: ",mysql::GetHostInfo(dbh),"\n"
  19. PRINT "Proto info is: ",mysql::GetProtoInfo(dbh),"\n"
  20. PRINT "Server info is: ",mysql::GetServerInfo(dbh),"\n"
  21. PRINT "PING result: ",mysql::Ping(dbh),"\n"
  22. PRINT "Thread ID: ",mysql::ThreadId(dbh),"\n"
  23. PRINT "Status is: ",mysql::Stat(dbh),"\n"
  24.  
  25. mysql::Close(dbh)
  26.  

Output

C:\scriptbasic\examples>scriba testmysql.sb
S10_1678 - 1969 Harley Davidson Ultimate Chopper
S10_1949 - 1952 Alpine Renault 1300
S10_2016 - 1996 Moto Guzzi 1100i
S10_4698 - 2003 Harley-Davidson Eagle Drag Bike
S10_4757 - 1972 Alfa Romeo GTA
S10_4962 - 1962 LanciaA Delta 16V
S12_1099 - 1968 Ford Mustang
S12_1108 - 2001 Ferrari Enzo
S12_1666 - 1958 Setra Bus
S12_2823 - 2002 Suzuki XREO

The database handle is: 1
Affected rows by SELECT: 10
Character set name is: latin1
Last error is:
Client info is: 5.5.39
Host info is: localhost via TCP/IP
Proto info is: 10
Server info is: 5.6.20
PING result: -1
Thread ID: 0
Status is: Uptime: 5611  Threads: 1  Questions: 306  Slow queries: 0  Opens: 94  Flush tables: 1  Open tables: 80  Queries per second avg: 0.054

C:\scriptbasic\examples>


Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - CSV & SQLite
« Reply #7 on: November 15, 2015, 08:22:01 PM »
I thought I would feature more of the Script BASIC core functionality in these examples along with the intent of showing how the SQLite interface works.


While searching for some .csv sample data to play with, I ran into this data set. It had the right amount of columns and rows. (Sacramento Police Crime Database  for January 2006)



Code: ScriptBasic
  1. ' result = FormatLine(in_str, fmt_str, quo_char, num_spc) Note: num_spc = -1 uses TAB
  2.  
  3. FUNCTION FormatLine(ln,fmtstr,qc,nsp)
  4.   SPLITAQ ln BY "," QUOTE qc TO col
  5.   SPLITA fmtstr BY "|" TO fmtcmd
  6.   rs = ""
  7.   FOR x = 0 to UBOUND(col)
  8.     SPLITA fmtcmd[x] BY ":" TO fmt
  9.     IF fmt[0] = "L" THEN
  10.       tmp = LEFT(col[x] & STRING(fmt[1]," "),fmt[1])
  11.       GOSUB Margin
  12.     ELSE IF fmt[0] = "R" THEN
  13.       IF fmt[2] <> undef THEN
  14.         tmp = FORMAT(fmt[2],col[x])
  15.       ELSE
  16.         tmp = col[x]
  17.       END IF
  18.       tmp = RIGHT(STRING(fmt[1]," ") & tmp, fmt[1])
  19.       GOSUB Margin
  20.     ELSE IF fmt[0] = "C" THEN
  21.       pad = fmt[1] - LEN(col[x])
  22.       pboth = pad \ 2
  23.       prt = pad % 2
  24.       tmp = STRING(pboth," ") & col[x] & STRING(pboth," ") & STRING(prt," ")
  25.       GOSUB Margin
  26.     END IF
  27.   NEXT
  28.   GOTO Done
  29.  
  30.   Margin:
  31.   IF nsp = -1 THEN
  32.     tmp &= "\t"
  33.   ELSE
  34.     tmp &= STRING(nsp," ")
  35.   END IF
  36.   rs &= tmp  
  37.   RETURN
  38.  
  39.   Done:
  40.   FormatLine = rs
  41. END FUNCTION
  42.  
  43. OPEN "SacramentocrimeJanuary2006.csv" FOR INPUT AS #1
  44. OPEN "sac.fmt" FOR OUTPUT AS #2
  45. fmtstr = "L:15|L:30|R:4|L:4|R:6|L:35|L:6|R:10:%~-##0.0000~|R:10:%~-##0.0000~"
  46. LINE INPUT #1, hdr
  47. WHILE NOT EOF(1)
  48.   LINE INPUT #1, csvln
  49.   csvln = CHOMP(csvln)
  50.   PRINT #2, FormatLine(csvln,fmtstr,"",2),"\n"
  51. WEND  
  52.  
  53. CLOSE(1)
  54. CLOSE(2)
  55.  

Output (7584 rows)
Code: [Select]
1/1/06 0:00      3108 OCCIDENTAL DR                 3  3C      1115  10851(A)VC TAKE VEH W/O OWNER        2404       38.5504   -121.3914 
1/1/06 0:00      2082 EXPEDITION WAY                5  5A      1512  459 PC  BURGLARY RESIDENCE           2204       38.4735   -121.4902 
1/1/06 0:00      4 PALEN CT                         2  2A       212  10851(A)VC TAKE VEH W/O OWNER        2404       38.6578   -121.4621 
1/1/06 0:00      22 BECKFORD CT                     6  6C      1443  476 PC PASS FICTICIOUS CHECK         2501       38.5068   -121.4270 
1/1/06 0:00      3421 AUBURN BLVD                   2  2A       508  459 PC  BURGLARY-UNSPECIFIED         2299       38.6374   -121.3846 

This is a CSV2SQL converter I wrote that might be helpful, This example was done under Linux and haven't run it on Windows yet for this post. The Linux time function gives you an idea of the conversion process time to execute.

Quote
By default SQLite will evaluate every INSERT / UPDATE statement within a unique transaction. If performing a large number of inserts, it's advisable to wrap your operation in a transaction:

Code: ScriptBasic
  1. IMPORT sqlite.bas
  2.  
  3. OPEN "SacramentocrimeJanuary2006.csv" FOR INPUT AS #1
  4. db = sqlite::open("sac116.db")
  5. fmtstr = "SSISISIRR"
  6. LINE INPUT #1, hdr
  7. hdr = CHOMP(hdr)
  8. SPLITA hdr BY "," TO col
  9. SPLITA fmtstr BY "" TO typ
  10. lastcol = UBOUND(col)
  11. sql = "CREATE TABLE crime ("
  12. FOR x = 0 TO lastcol
  13.   tmp = ""
  14.   IF typ[x] = "S" THEN
  15.     tstr = " TEXT"
  16.   ELSE IF typ[x] = "I" THEN
  17.     tstr = " INTEGER"
  18.   ELSE IF typ[x] = "R" THEN
  19.     tstr = " REAL"
  20.   END IF
  21.   tmp &= col[x] & tstr
  22.   IF x <> lastcol THEN tmp &= ", "
  23.   sql &= tmp
  24. NEXT
  25. sql &= ");"
  26. sqlite::execute(db, sql)
  27. sqlite::execute(db, "BEGIN TRANSACTION")
  28. WHILE NOT EOF(1)
  29.   sql = "INSERT INTO crime VALUES ("
  30.   LINE INPUT #1, csvln
  31.   csvln = CHOMP(csvln)
  32.   SPLITAQ csvln BY "," QUOTE "" TO col
  33.   FOR x = 0 TO lastcol
  34.     IF typ[x] = "S" THEN
  35.       tmp = "'" & col[x] & "'"
  36.     ELSE
  37.       tmp = col[x]
  38.     END IF
  39.     IF x <> lastcol THEN tmp &= ", "
  40.     sql &= tmp
  41.   NEXT
  42.   sql &= ");"
  43.   sqlite::execute(db, sql)
  44. WEND
  45. sqlite::execute(db, "END TRANSACTION")
  46. sqlite::close(db)
  47. CLOSE(1)
  48.  

Output

jrs@laptop:~/sb/sb22/test$ time scriba csv2sql.sb

real   0m0.763s
user   0m0.457s
sys   0m0.016s
jrs@laptop:~/sb/sb22/test$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .open sac116.db
sqlite> SELECT COUNT(*) FROM crime;
7584
sqlite> .q
jrs@laptop:~/sb/sb22/test$


Here are a few console mode sqlite3.exe commands that are handy when working with SQLite.

Code: Text
  1. sqlite> .schema crime
  2. CREATE TABLE crime (cdatetime TEXT, address TEXT, district INTEGER, beat TEXT, grid INTEGER, crimedescr TEXT, ucr_ncic_code INTEGER, latitude REAL, longitude RE
  3. AL);
  4. sqlite> .dbinfo
  5. database page size:  1024
  6. write format:        1
  7. read format:         1
  8. reserved bytes:      0
  9. file change counter: 2
  10. database page count: 790
  11. freelist page count: 0
  12. schema cookie:       1
  13. schema format:       4
  14. default cache size:  0
  15. autovacuum top root: 0
  16. incremental vacuum:  0
  17. text encoding:       1 (utf8)
  18. user version:        0
  19. application id:      0
  20. software version:    3007012
  21. number of tables:    1
  22. number of indexes:   0
  23. number of triggers:  0
  24. number of views:     0
  25. schema size:         163
  26. sqlite> SELECT * FROM crime LIMIT 1;
  27. 1/1/06 0:00  3108 OCCIDENTAL DR  3           3C          1115        10851(A)VC TAKE VEH W/O OWNER  2404           38.55042047  -121.3914158
  28. sqlite>
  29.  


This is using a SQLite database rather than a CSV Sacramento crime file.

Code: ScriptBasic
  1. ' result = FormatLine(in_str/array, fmt_str, quo_char, num_spc) Note: num_spc = -1 uses TAB
  2.  
  3. IMPORT sqlite.bas
  4.  
  5. FUNCTION FormatLine(ln,fmtstr,qc,nsp)
  6.   IF ISARRAY(ln) THEN
  7.     col = ln
  8.   ELSE
  9.     SPLITAQ ln BY "," QUOTE qc TO col
  10.   END IF
  11.      
  12.   SPLITA fmtstr BY "|" TO fmtcmd
  13.   rs = ""
  14.   FOR x = 0 to UBOUND(col)
  15.     SPLITA fmtcmd[x] BY ":" TO fmt
  16.     IF fmt[0] = "L" THEN
  17.       tmp = LEFT(col[x] & STRING(fmt[1]," "),fmt[1])
  18.       GOSUB Margin
  19.     ELSE IF fmt[0] = "R" THEN
  20.       IF fmt[2] <> undef THEN
  21.         tmp = FORMAT(fmt[2],col[x])
  22.       ELSE
  23.         tmp = col[x]
  24.       END IF
  25.       tmp = RIGHT(STRING(fmt[1]," ") & tmp, fmt[1])
  26.       GOSUB Margin
  27.     ELSE IF fmt[0] = "C" THEN
  28.       pad = fmt[1] - LEN(col[x])
  29.       pboth = pad \ 2
  30.       prt = pad % 2
  31.       tmp = STRING(pboth," ") & col[x] & STRING(pboth," ") & STRING(prt," ")
  32.       GOSUB Margin
  33.     END IF
  34.   NEXT
  35.   GOTO Done
  36.  
  37.   Margin:
  38.   IF nsp = -1 THEN
  39.     tmp &= "\t"
  40.   ELSE
  41.     tmp &= STRING(nsp," ")
  42.   END IF
  43.   rs &= tmp  
  44.   RETURN
  45.  
  46.   Done:
  47.   FormatLine = rs
  48. END FUNCTION
  49.  
  50. db = sqlite::Open("sac16.db")
  51. stmt = sqlite::Query(db,"SELECT * FROM crime LIMIT 1")
  52. sqlite::Row(stmt)
  53. sqlite::FetchArray(stmt,columns)
  54. fmtstr = "L:15|L:30|R:4|L:4|R:6|L:35|L:6|R:10:%~-##0.0000~|R:10:%~-##0.0000~"
  55. PRINT FormatLine(columns,fmtstr,"",2),"\n"
  56. sqlite::Close(db)
  57.  

Output
Code: [Select]
C:\scriptbasic\examples> scriba fmtsqlrow.sb
1/1/06 0:00      3108 OCCIDENTAL DR                 3  3C      1115  10851(A)VC TAKE VEH W/O OWNER        2404       38.5504   -121.3914 
C:\scriptbasic\examples>
« Last Edit: November 07, 2017, 07:52:34 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Windows - ODBC
« Reply #8 on: November 18, 2015, 11:00:26 PM »
ODBC is the common interface for various database environments. Here are a few examples of reading multiple databases I work with all the time.

Sage 100 Customer File ProvideX proprietary ISAM via their ODBC driver. (demo ABC company)
Code: ScriptBasic
  1. IMPORT odbc.bas
  2.  
  3. dbh = odbc::RealConnect("SOTAMAS90","JRS","PASSWORD")
  4.  
  5. odbc::query(dbh,"SELECT * FROM AR_Customer")
  6.  
  7. WHILE odbc::FetchHash(dbh,dbcol)
  8.   PRINT LEFT(dbcol{"CustomerNo"} & STRING(11," "), 11)," | ",dbcol{"CustomerName"},"\n"
  9. WEND
  10.  
  11. odbc::Close(dbh)
  12.  


C:\scriptbasic\examples>scriba odbc_sage_customers.sb
ABF         | American Business Futures
ABS         | ABS - Sage cloud for invoices
AVNET       | Avnet Processing Corp
BRESLIN     | Breslin Parts Supply
HILLSB      | Hillsboro Service Center
INACTIV     | Inactive Customer **INACTIVE**
MAVRK       | Maverick Papers
RSSUPPL     | R & S Supply Corp.
SHEPARD     | Shepard Motorworks
ALLENAP     | Allen's Appliance Repair
AMERCON     | American Concrete Service
ATOZ        | A To Z Carpet Supply
AUTOCR      | Autocraft Accessories
BAYPYRO     | Bay Pyrotronics Corp.
CAPRI       | Capri Sailing Ships
CUSTOM      | Custom Craft Products
GREALAR     | Greater Alarm Company
JELLCO      | Jellco Packing
ORANGE      | Orange Door & Window Co.

C:\scriptbasic\examples>



This example is the SQLite3 crime database used in a previous post but this time using the SQLite3 ODBC driver.

Code: ScriptBasic
  1. ' ODBC SQLite Driver Test Program
  2.  
  3. IMPORT odbc.bas
  4.  
  5. dbh = odbc::RealConnect("SQLite3_DSN","","")
  6.  
  7. odbc::query(dbh,"SELECT * FROM crime LIMIT 10;")
  8.  
  9. WHILE odbc::FetchHash(dbh,column)
  10.   PRINT column{"cdatetime"}," - ",column{"crimedescr"},"\n"
  11. WEND
  12.  
  13. odbc::Close(dbh)
  14.  


C:\scriptbasic\examples>scriba odbc_sqlite3.sb
1/1/06 0:00 - 10851(A)VC TAKE VEH W/O OWNER
1/1/06 0:00 - 459 PC  BURGLARY RESIDENCE
1/1/06 0:00 - 10851(A)VC TAKE VEH W/O OWNER
1/1/06 0:00 - 476 PC PASS FICTICIOUS CHECK
1/1/06 0:00 - 459 PC  BURGLARY-UNSPECIFIED
1/1/06 0:00 - 530.5 PC USE PERSONAL ID INFO
1/1/06 0:00 - 459 PC  BURGLARY VEHICLE
1/1/06 0:00 - 484 PC   PETTY THEFT/INSIDE
1/1/06 0:00 - 459 PC  BURGLARY BUSINESS
1/1/06 0:00 - 1708 US   THEFT OF MAIL

C:\scriptbasic\examples>


You can even use ODBC to read and write data to an Excel spreadsheet.



Code: ScriptBasic
  1. ' ODBC Excel Driver Test Program
  2.  
  3. IMPORT odbc.bas
  4.  
  5. dbh = odbc::RealConnect("Excel_DSN","","")
  6.  
  7. odbc::query(dbh,"SELECT * FROM [Sheet1$];")
  8.  
  9. WHILE odbc::FetchHash(dbh,column)
  10.   PRINT "| ",column{"Name"}," | ",column{"Phone"}," | ",column{"Title"},"\n"
  11. WEND
  12.  
  13. odbc::Close(dbh)
  14.  


C:\scriptbasic\examples>scriba odbc_excel.sb
| John | 800-FOR-CODE | Programmer
| Peter | 900-123-8008 | Author
| Dave | 101-202-3003 | Mr. COM

C:\scriptbasic\examples>


I was going to show MS Access and MS SQL Server Express ODBC examples but what I have shown already is getting redundant which is the point of this post in a nut shell.
« Last Edit: November 07, 2017, 07:55:43 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Script BASIC Array Sort
« Reply #9 on: January 01, 2016, 01:35:24 AM »
One of the other features I wanted to add to Script BASIC was an array sort function. I wanted it to work on any array indices. (index or associative)

Sort Routine (part of T ext. module)
Code: ScriptBasic
  1. SUB merge(left_side, right_side, result)
  2.   LOCAL left_size, left_ptr, right_size, right_ptr, result_ptr
  3.   left_size = UBOUND(left_side)
  4.   left_ptr = 0
  5.   right_size = UBOUND(right_side)
  6.   right_ptr = 0
  7.   result_ptr = 0
  8.   WHILE left_ptr <= left_size AND right_ptr <= right_size
  9.     IF left_side[left_ptr] <= right_side[right_ptr] THEN
  10.       result[result_ptr] = left_side[left_ptr]
  11.       left_ptr += 1
  12.       result_ptr += 1
  13.     ELSE
  14.       result[result_ptr] = right_side[right_ptr]
  15.       right_ptr += 1
  16.       result_ptr += 1
  17.     END IF
  18.   WEND
  19.   WHILE left_ptr <= left_size
  20.     result[result_ptr] = left_side[left_ptr]
  21.     left_ptr += 1
  22.     result_ptr += 1
  23.   WEND
  24.   WHILE right_ptr <= right_size
  25.     result[result_ptr] = right_side[right_ptr]
  26.     right_ptr += 1
  27.     result_ptr += 1
  28.   WEND
  29. END SUB
  30.  
  31. SUB sort(unsorted)
  32.   LOCAL left_side, right_side, the_middle, array_size, result, x, y, z
  33.   array_size = UBOUND(unsorted)
  34.   IF array_size = 0 THEN
  35.     EXIT FUNCTION
  36.   END IF
  37.   the_middle = FIX((array_size + 1) / 2)
  38.   y = 0
  39.   FOR x = 0 TO the_middle - 1
  40.     left_side[y] = unsorted[x]
  41.     y += 1
  42.   NEXT
  43.   z = 0
  44.   FOR x = the_middle TO array_size
  45.     right_side[z] = unsorted[x]
  46.     z += 1
  47.   NEXT
  48.   sort(left_side)
  49.   sort(right_side)
  50.   merge(left_side, right_side, result)
  51.   unsorted = result
  52. END SUB
  53.  

Code: ScriptBasic
  1. ' Script BASIC Array Sort
  2.  
  3. IMPORT t.bas
  4.  
  5. OPTION COMPARE sbCaseInsensitive
  6.  
  7. s = "pear,cranberry,orange,apple,Carrot,banana,grape,Apricot"
  8. SPLITA s BY "," TO a
  9.  
  10. t::sort(a)
  11.  
  12. FOR x = 0 TO UBOUND(a)
  13.   PRINT a[x],"\n"
  14. NEXT
  15.  

The first run was without the sbCaseInsensitive option enable. (default)


jrs@laptop:~/sb/sb22/test$ scriba array_sort_food.sb
Apricot
Carrot
apple
banana
cranberry
grape
orange
pear
jrs@laptop:~/sb/sb22/test$ scriba array_sort_food.sb
apple
Apricot
banana
Carrot
cranberry
grape
orange
pear
jrs@laptop:~/sb/sb22/test$


As a stress test, I thought I would sort each line in a text version of the Bible. (30383 lines / 4,047,392 bytes)

Code: ScriptBasic
  1. ' Script BASIC Array Sort
  2.  
  3. IMPORT t.bas
  4.  
  5. OPEN "bible.txt" FOR INPUT AS #1
  6. s = INPUT(LOF(1),1)
  7. SPLITA s BY "\n" TO a
  8.  
  9. t::sort(a)
  10.  
  11. FOR x = UBOUND(a) - 10 TO UBOUND(a)
  12.   PRINT a[x],"\n"
  13. NEXT
  14.  

Output
Code: [Select]
jrs@laptop:~/sb/sb22/test$ time scriba sort.sb
Zebulun and Naphtali were a people that jeoparded their lives unto the death in the high places of the field.
Zebulun shall dwell at the haven of the sea; and he shall be for an haven of ships; and his border shall be unto Zidon.
Zedekiah was one and twenty years old when he began to reign, and he reigned eleven years in Jerusalem. And his mother's name was Hamutal the daughter of Jeremiah of Libnah.
Zedekiah was one and twenty years old when he began to reign, and reigned eleven years in Jerusalem.
Zelek the Ammonite, Naharai the Berothite, the armourbearer of Joab the son of Zeruiah,
Zelek the Ammonite, Nahari the Beerothite, armourbearer to Joab the son of Zeruiah,
Zenan, and Hadashah, and Migdalgad,
Zion heard, and was glad; and the daughters of Judah rejoiced because of thy judgments, O LORD.
Zion shall be redeemed with judgment, and her converts with righteousness.
Zion spreadeth forth her hands, and there is none to comfort her: the LORD hath commanded concerning Jacob, that his adversaries should be round about him: Jerusalem is as a menstruous woman among them.
Ziph, and Telem, and Bealoth,

real 0m13.069s
user 0m12.173s
sys 0m0.810s
jrs@laptop:~/sb/sb22/test$
« Last Edit: January 01, 2016, 02:39:52 AM by John »