MySQL Entension Module

Started by root, May 23, 2026, 06:31 PM

Previous topic - Next topic

root

ScriptBasic supports MySQL with a C API interface that is lightening fast. I don't have the MySQL Classic  Models example DB on my Windows laptop so these examples will be from my Linux servers.

' MySQL Test Program

IMPORT mysql.bas

dbh = mysql::RealConnect("localhost","root","PASSWORD","classicmodels")

mysql::query(dbh,"SELECT * FROM products LIMIT 25")

WHILE mysql::FetchHash(dbh,column)
  PRINT column{"productCode"}," - ",column{"productName"}," - ",FORMAT("%~$###.00~",column{"MSRP"}),"\n"
WEND

PRINTNL
PRINT "The database handle is: ",dbh,"\n"
PRINT "Affected rows by SELECT: ",mysql::AffectedRows(dbh),"\n"
PRINT "Character set name is: ",mysql::CharacterSetName(dbh),"\n"
PRINT "Last error is: ",mysql::ErrorMessage(dbh),"\n"
PRINT "Client info is: ",mysql::GetClientInfo(),"\n"
PRINT "Host info is: ",mysql::GetHostInfo(dbh),"\n"
PRINT "Proto info is: ",mysql::GetProtoInfo(dbh),"\n"
PRINT "Server info is: ",mysql::GetServerInfo(dbh),"\n"
PRINT "PING result: ",mysql::Ping(dbh),"\n"
PRINT "Thread ID: ",mysql::ThreadId(dbh),"\n"
PRINT "Status is: ",mysql::Stat(dbh),"\n"

mysql::Close(dbh)

Output:
rs@linux-dev:~/sbrt/examples$ sbc testmysql.sb
S10_1678 - 1969 Harley Davidson Ultimate Chopper - $ 95.70
S10_1949 - 1952 Alpine Renault 1300 - $214.30
S10_2016 - 1996 Moto Guzzi 1100i - $118.94
S10_4698 - 2003 Harley-Davidson Eagle Drag Bike - $193.66
S10_4757 - 1972 Alfa Romeo GTA - $136.00
S10_4962 - 1962 LanciaA Delta 16V - $147.74
S12_1099 - 1968 Ford Mustang - $194.57
S12_1108 - 2001 Ferrari Enzo - $207.80
S12_1666 - 1958 Setra Bus - $136.67
S12_2823 - 2002 Suzuki XREO - $150.62
S12_3148 - 1969 Corvair Monza - $151.08
S12_3380 - 1968 Dodge Charger - $117.44
S12_3891 - 1969 Ford Falcon - $173.02
S12_3990 - 1970 Plymouth Hemi Cuda - $ 79.80
S12_4473 - 1957 Chevy Pickup - $118.50
S12_4675 - 1969 Dodge Charger - $115.16
S18_1097 - 1940 Ford Pickup Truck - $116.67
S18_1129 - 1993 Mazda RX-7 - $141.54
S18_1342 - 1937 Lincoln Berline - $102.74
S18_1367 - 1936 Mercedes-Benz 500K Special Roadster - $ 53.91
S18_1589 - 1965 Aston Martin DB5 - $124.44
S18_1662 - 1980s Black Hawk Helicopter - $157.69
S18_1749 - 1917 Grand Touring Sedan - $170.00
S18_1889 - 1948 Porsche 356-A Roadster - $ 77.00
S18_1984 - 1995 Honda Civic - $142.25

The database handle is: 1
Affected rows by SELECT: 25
Character set name is: utf8mb4
Last error is:
Client info is: 8.0.44
Host info is: Localhost via UNIX socket
Proto info is: 10
Server info is: 8.0.44-0ubuntu0.24.04.2
PING result: -1
Thread ID: 0
Status is: Uptime: 837  Threads: 2  Questions: 1039  Slow queries: 0  Opens: 470  Flush tables: 3  Open tables: 389  Queries per second avg: 1.241
jrs@linux-dev:~/sbrt/examples$

THIS MySQL example is using AJAX to select the product line to display.This is running on the ScriptBasic web server.

' ScriptBasic AJAX & MySQL

IMPORT cgi.bas

cgi::Header 200,"text/html"
cgi::FinishHeader

PRINT """
<html>
<head>
<script>
function showItems(str) {
  if (str == "") {
    document.getElementById("results").innerHTML = "";
    return;
  } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("results").innerHTML = this.responseText;
      }
    };
    xmlhttp.open("GET","/home/qbo/getitems.sb?q="+str,true);
    xmlhttp.send();
  }
}
</script>
</head>
<body>

<form>
  <select name="items" onchange="showItems(this.value)">
    <option value="">Product Line</option>
    <option value="Classic Cars">Classic Cars</option>
    <option value="Motorcycles">Motorcycles</option>
    <option value="Planes">Planes</option>
    <option value="Ships">Ships</option>
    <option value="Trains">Trains</option>
    <option value="Trucks and Buses">Trucks and Buses</option>
    <option value="Vintage Cars">Vintage Cars</option>
  </select>
</form>
<br>
<div id="results"></div>

</body>
</html>
"""

' AJAX - getitems.sb

IMPORT cgi.bas
IMPORT mysql.bas

cgi::Header 200,"text/html"

PRINT """
<!DOCTYPE html>
<html>
<head>
<style>
table {
  width: 100%;
  border-collapse: collapse;
}

table, td, th {
  border: 1px solid black;
  padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>
"""

product_line = cgi::GetParam("q")

dbh = mysql::RealConnect("localhost","USER","PASSWORD","classicmodels")
mysql::query(dbh,"SELECT * FROM products WHERE productLine = '" & product_line & "'")

PRINT """
<table>
  <tr>
    <th>Product Code</th>
    <th>Product Line</th>
    <th>Product Vendor</th>
    <th>Product Name</th>
    <th>In Stock</th>
    <th>Cost</th>
    <th>MSRP</th>
  </tr>
"""

WHILE mysql::FetchHash(dbh,column)
  PRINT "  <tr>\n"
  PRINT "    <td>", column{"productCode"}, "</td>\n"
  PRINT "    <td>", column{"productLine"}, "</td>\n"
  PRINT "    <td>", column{"productVendor"}, "</td>\n"
  PRINT "    <td>", column{"productName"}, "</td>\n"
  PRINT "    <td align=\"right\">", column{"quantityInStock"}, "</td>\n"
  PRINT "    <td align=\"right\">", FORMAT("%~$###.00~",column{"buyPrice"}), "</td>\n"
  PRINT "    <td align=\"right\">", FORMAT("%~$###.00~",column{"MSRP"}), "</td>\n"
  PRINT "  </tr>\n"
WEND

PRINT """
</table>
</body>
</html>
"""

mysql::Close(dbh)