72 lines
1.9 KiB
QBasic
72 lines
1.9 KiB
QBasic
' sqltest.bas -- SQL Database Demo
|
|
'
|
|
' Demonstrates the SQL include library:
|
|
' SQLOpen / SQLClose - open and close a database
|
|
' SQLExec - execute non-query statements
|
|
' SQLQuery / SQLNext - run a query and iterate rows
|
|
' SQLField$ / SQLFieldInt / SQLFieldDbl - read column values
|
|
' SQLFieldCount / SQLFieldName$ - inspect result schema
|
|
' SQLEof / SQLFreeResult / SQLAffected - cursor and status
|
|
' SQLError$ - error reporting
|
|
'
|
|
' Add sql.bas to your project, then click Run.
|
|
|
|
DIM db AS LONG
|
|
DIM rs AS LONG
|
|
|
|
PRINT "Opening database..."
|
|
db = SQLOpen("sqltest.db")
|
|
|
|
IF db = 0 THEN
|
|
PRINT "Failed to open database!"
|
|
END
|
|
END IF
|
|
|
|
PRINT "Database opened (handle"; db; ")"
|
|
PRINT
|
|
|
|
' Create table and insert sample data
|
|
SQLExec db, "CREATE TABLE IF NOT EXISTS parts (id INTEGER PRIMARY KEY, name TEXT, price REAL)"
|
|
SQLExec db, "DELETE FROM parts"
|
|
SQLExec db, "INSERT INTO parts VALUES (1, 'Resistor 10K', 0.05)"
|
|
SQLExec db, "INSERT INTO parts VALUES (2, 'Capacitor 100uF', 0.25)"
|
|
SQLExec db, "INSERT INTO parts VALUES (3, 'LED Red', 0.10)"
|
|
SQLExec db, "INSERT INTO parts VALUES (4, '555 Timer', 0.75)"
|
|
SQLExec db, "INSERT INTO parts VALUES (5, 'Arduino Nano', 12.50)"
|
|
|
|
PRINT "Inserted 5 rows. Affected:"; SQLAffected(db)
|
|
PRINT
|
|
|
|
' Query and display results
|
|
rs = SQLQuery(db, "SELECT * FROM parts ORDER BY price")
|
|
|
|
IF rs = 0 THEN
|
|
PRINT "Query failed: "; SQLError$(db)
|
|
SQLClose db
|
|
END
|
|
END IF
|
|
|
|
PRINT "Columns:"; SQLFieldCount(rs)
|
|
|
|
DIM i AS INTEGER
|
|
FOR i = 0 TO SQLFieldCount(rs) - 1
|
|
PRINT " ["; i; "] "; SQLFieldName$(rs, i)
|
|
NEXT i
|
|
|
|
PRINT
|
|
PRINT "Parts (sorted by price):"
|
|
PRINT "-----------------------------------"
|
|
|
|
DO WHILE SQLNext(rs)
|
|
PRINT "#"; SQLFieldInt(rs, 0);
|
|
PRINT " "; SQLField$(rs, "name");
|
|
PRINT " $"; SQLFieldDbl(rs, 2)
|
|
LOOP
|
|
|
|
PRINT "-----------------------------------"
|
|
|
|
SQLFreeResult rs
|
|
SQLClose db
|
|
|
|
PRINT
|
|
PRINT "Done!"
|