LoginSignup
0
0

More than 1 year has passed since last update.

Xojo でデータベース接続パラメータを JSON 化してロードする

Last updated at Posted at 2023-01-18

環境

  • Xojo 2019 Ver3.2 (Web1.0 ベースの 旧Xojo)
  • Webアプリ(スタンドアローン ないし デバッグ実行)
  • Windows10 Pro 上でIDEを使用

方法

以下の2つの方法を試してみました。

  • Xojo アプリのリソースに json ファイルを含める方法
  • ファイル読み込みでメモリにロードする方法

テストの json ファイル

dbaccess.json ファイル

{"host":"localhost","database":"test","user":"oreore","password":"hogehoge"}

Xojo アプリのリソースに json ファイルを含める方法

dbaccess.json ファイルをContentsにドラッグ&ドロップします。

image.png

この状態で、以下のようにするとアクセスできます。

MessageBox dbaccess

image.png

場所は WebPage1 の配下に置いても、あるいは Insert-Folder で、フォルダの中に含めても同様にアクセスできるようです。

image.png

要素にアクセスするには以下のようにします。

dim j as new JSONItem(dbaccess)
dim host1 as string = j.value("host")
TextField1.Text=host1.ToText

コンパイル時、dbaccess.json ファイルは以下に含まれていました。

$ tree test
test
├── config.cfg
├── output.log
├── test
├── test Libs
│   ├── XojoConsoleFramework64.so
│   ├── libGZip64.so
│   ├── libRBCrypto64.so
│   ├── libRBInternetEncodings64.so
│   ├── libRBRegEx64.so
│   ├── libSSLSocket64.so
│   └── libc++.so.1
├── test Resources
│   ├── appicon128.png
│   ├── appicon256.png
│   ├── dbaccess.json
│   ├── favicon.ico
│   ├── favicon@2x.ico
│   ├── homescreen120.png
│   ├── homescreen152.png
│   ├── homescreen60.png
│   ├── homescreen76.png
│   └── ja.UTF-8
│       └── LC_MESSAGES
│           └── localizable.mo
└── test.cgi

4 directories, 21 files

ファイル読み込みでメモリにロードする方法

デバッグなどにも共通して使える場所として、CurrentWorkingDirectory に置いた json ファイルを読み込むようにしました。

cf.,「Xojo Web アプリで CurrentWorkingDirectory を調べる」
https://qiita.com/nanbuwks/items/0d0aa59b8d555899deb6


Var jsonFile As TextInputStream
Var jsoncontents As String
var f,dir as Folderitem
dir = SpecialFolder.CurrentWorkingDirectory
f = dir.Child("dbaccess.json")
If Not( f.Exists ) Then
  MessageBox "dbaccess.json"+" が見つかりません。 "+dir.NativePath()+" にデータベースパラメータファイルを配置してください。"
else
  jsonFile = TextInputStream.Open(f)
  jsonFile.Encoding = Encodings.UTF8
  jsoncontents = jsonFile.ReadAll
  jsonFile.Close
  
  dim j as new JSONItem(jsoncontents)
  dim host as string = j.value("host")
  TextField1.Text=host.ToText
  
  MessageBox dbaccess
  dim j2 as new JSONItem(dbaccess)
  dim host2 as string = j.value("host")
  TextField1.Text=host2.ToText
End If
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0