Author Topic: Apple Swift on Ubuntu  (Read 12458 times)

Offline AIR

  • RETIRED
  • BASIC Developer
  • Posts: 168
Re: Apple Swift on Ubuntu
« Reply #30 on: December 13, 2015, 05:37:41 PM »
How can I make your SQLite3 Swift code an include or import?

By building on the CSQlite3 module I created.

Offline John

  • Forum Support / SB Dev
  • Posts: 1694
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #31 on: December 13, 2015, 06:01:12 PM »
AIR,

Here is your Swift example in Script BASIC clothing.

Code: Script BASIC
  1. IMPORT sqlite.bas
  2.  
  3. db = sqlite::OPEN("test.db")
  4.  
  5. PRINT sqlite::VERSION(),"\n"
  6.  
  7. sql = """
  8. DROP TABLE IF EXISTS Cars;
  9. CREATE TABLE Cars(Id INT, Name TEXT, Price INT);
  10. INSERT INTO Cars VALUES(1, 'Audi', 52642);
  11. INSERT INTO Cars VALUES(2, 'Mercedes', 57127);
  12. INSERT INTO Cars VALUES(3, 'Skoda', 9000);
  13. INSERT INTO Cars VALUES(4, 'Volvo', 29000);
  14. INSERT INTO Cars VALUES(5, 'Bentley', 350000);
  15. INSERT INTO Cars VALUES(6, 'Citroen', 21000);
  16. INSERT INTO Cars VALUES(7, 'Hummer', 41400);
  17. INSERT INTO Cars VALUES(8, 'Volkswagen', 21600);
  18. """
  19.  
  20. sqlite::EXECUTE(db, sql)
  21.  
  22. stmt = sqlite::QUERY(db, "SELECT Name,Price FROM Cars Order By Price DESC")
  23.  
  24. PRINTNL
  25. WHILE (sqlite::ROW(stmt) = sqlite::SQLITE3_ROW)
  26.   IF sqlite::FETCHHASH(stmt,column) THEN
  27.     PRINT column{"Name"}," ",column{"Price"},"\n"
  28.   END IF
  29. WEND
  30.  
  31. sqlite::CLOSE(db)
  32.  


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

Bentley 350000
Mercedes 57127
Audi 52642
Hummer 41400
Volvo 29000
Volkswagen 21600
Citroen 21000
Skoda 9000

real   0m1.232s
user   0m0.001s
sys   0m0.010s
jrs@laptop:~/sb/sb22/test$

« Last Edit: December 13, 2015, 06:05:34 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 1694
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #32 on: December 14, 2015, 10:34:20 AM »
Quote
By building on the CSQlite3 module I created.

@AIR - I can't seem to figure out how to get a module built that I can import.

What's the trick?

Ideally I would like to do something like this.

Code: Text
  1.     import Foundation
  2.     import Glibc
  3.     import SQLite    
  4.      
  5.     var db: COpaquePointer = nil
  6.     var stmt: COpaquePointer = nil
  7.      
  8.     print(sqlVersion(),"\n")
  9.      
  10.     sqlOpen("test.db", database: &db)
  11.      
  12.     var sql =       "DROP TABLE IF EXISTS Cars;" +
  13.                 "CREATE TABLE Cars(Id INT, Name TEXT, Price INT);" +
  14.                 "INSERT INTO Cars VALUES(1, 'Audi', 52642);" +
  15.                 "INSERT INTO Cars VALUES(2, 'Mercedes', 57127);" +
  16.                 "INSERT INTO Cars VALUES(3, 'Skoda', 9000);" +
  17.                 "INSERT INTO Cars VALUES(4, 'Volvo', 29000);" +
  18.                 "INSERT INTO Cars VALUES(5, 'Bentley', 350000);" +
  19.                 "INSERT INTO Cars VALUES(6, 'Citroen', 21000);" +
  20.                 "INSERT INTO Cars VALUES(7, 'Hummer', 41400);" +
  21.                 "INSERT INTO Cars VALUES(8, 'Volkswagen', 21600);"
  22.      
  23.     sqlExec(db, query: sql)
  24.      
  25.     sql = "SELECT Name,Price FROM Cars Order By Price DESC"
  26.      
  27.     sqlPrepare(db, query: sql, statement: &stmt)
  28.      
  29.     while sqlStep(stmt) == SQLITE_ROW {
  30.             var make  = sqlColumnText(stmt, column: 0)
  31.             var price = sqlColumnText(stmt, column: 1)
  32.             print(make,price)
  33.      
  34.     }    
  35.      
  36.     sqlClose(db)
  37.  
     


swiftc airlite.swift -o airlite

« Last Edit: December 14, 2015, 09:13:39 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 1694
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #33 on: December 14, 2015, 09:30:21 PM »
Quote from: AIR
People use Swift because a huge (<--cue Donald Trump Accent) company is behind it.  Kind of like how Objective C became popular.

Watch how Apple’s Swift evolved into an open source powerhouse

Quote from: AIR
At least with Objective C, I can drop straight C into it without having to jump through crazy hoops to get shit to work.

That's how I feel about Script BASIC. If all I'm interested is in getting something done or test a proof of concept, SB works for me. If it's not a one off project and will be distributed, then I'll take another look what is the best solution for that environment.

@AIR - Thanks for the nudge! I will take another look at NIM.
« Last Edit: December 14, 2015, 10:15:44 PM by John »