auth_test_01.bmx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. SuperStrict
  2. Framework Database.SQLite
  3. Import BRL.filesystem
  4. Import BRL.StandardIO
  5. DeleteFile("autotest.db")
  6. Local db:TDBConnection = LoadDatabase("SQLITE", "autotest.db")
  7. If Not db Then
  8. Print "Problem initialising database environment."
  9. End
  10. End If
  11. If db.hasError() Then
  12. errorAndClose(db)
  13. End If
  14. If db.isOpen() Then
  15. ' create a table
  16. Local s:String = "CREATE TABLE person (id integer primary key AUTOINCREMENT, " + ..
  17. " forename varchar(30)," + ..
  18. " surname varchar(30) )"
  19. db.executeQuery(s)
  20. ' insert some data
  21. Local names:String[][] = [ ..
  22. [ "Alfred", "Aho" ], ..
  23. [ "Brian", "Kernighan" ], ..
  24. [ "Peter", "Weinberger" ] ]
  25. For Local i:Int = 0 Until names.length
  26. db.executeQuery("INSERT INTO person values (NULL, '" + names[i][0] + "', '" + names[i][1] + "')")
  27. If db.hasError() Then
  28. errorAndClose(db)
  29. End If
  30. Next
  31. ' add a user - this will enable authentication
  32. ' cast to SQLite to access SQLite-only functionality
  33. Local sqliteDB:TDBSQLite = TDBSQLite(db)
  34. sqliteDB.addUser("user", "password", True)
  35. If db.hasError() Then
  36. errorAndClose(db)
  37. End If
  38. ' close
  39. db.close()
  40. ' open and attempt to use without authenticating...
  41. db = LoadDatabase("SQLITE", "autotest.db")
  42. Local query:TDatabaseQuery = db.executeQuery("SELECT * from person")
  43. ' we should get an error
  44. If db.hasError() Then
  45. Print "EXPECTED : " + db.error().toString()
  46. End If
  47. db.close()
  48. ' connect with username and password...
  49. db = LoadDatabase("SQLITE", "autotest.db", , , "user", "password")
  50. Print "Trying again!..."
  51. query = db.executeQuery("SELECT * from person")
  52. ' we should get an error
  53. If db.hasError() Then
  54. errorAndClose(db)
  55. End If
  56. While query.nextRow()
  57. Local record:TQueryRecord = query.rowRecord()
  58. Print "Name = " + TDBString(record.value(1)).value + " " + TDBString(record.value(2)).value
  59. Wend
  60. End If
  61. Function errorAndClose(db:TDBConnection)
  62. Print db.error().toString()
  63. db.close()
  64. End
  65. End Function