Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Menghubungkan ke Database Oracle Menggunakan Server Side Swift

Anda dapat menggunakan metode berikut untuk terhubung ke database oracle:( Ini dimungkinkan dengan bantuan komunitas uap.)

-----untuk membuat driver Oracle berfungsi, saya telah Mengikat metode ini di Ubuntu--------------- klien Oracle perlu diinstal sedemikian rupa sehingga jalur header dan pustaka dapat ditentukan, Anda bisa mendapatkannya dari situs web oracle.

oracle-instantclinet*-basic-*.rpm
oracle-instantclinet*-devel-*.rpm
oracle-instantclinet*-sqlplus-*.rpm

--install paket yang diunduh menggunakan perintah berikut

sudo alien -i oracle-instantclinet*-basic-*.rpm
sudo alien -i oracle-instantclinet*-devel-*.rpm
sudo alien -i oracle-instantclinet*-sqlplus-*.rpm

-- Instal libaio1 di ubuntu

sudo apt install libaio1

-- jalur ini harus di ~/.bashrc

#oracle home and library path
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle/12.2/client64/lib:/usr/local/lib

--unduh pustaka OCILIB dari Githubgit clone https://github.com/vrogier/ocilib.git ( atau unduh versi terbaru / diuji pada ocilib 4.5.2 )-- ekstrak cd file ocilib ke folder ocilib, konfigurasikan make dan make install

    tar -zxf ocilib-4.5.2-gnu.tar.gz
    cd ocilib-4.5.2
    ./configure --with-oracle-headers-path=/usr/include/oracle/12.2/client64/ --with-oracle-lib-path=/usr/lib/oracle/12.2/client64/lib CFLAGS="-O2 -m64"
    make
    sudo make install

-- gunakan konfigurasi ini jika Anda perlu berurusan dengan unicode, umumnya Anda tidak memerlukan ini

./configure --with-oracle-headers-path=/usr/include/oracle/12.2/client64/ --with-oracle-lib-path=/usr/lib/oracle/12.2/client64/lib --with-oracle-charset=wide CFLAGS="-O2 -m64"

-- Metode di atas menginstal OCILIB di mesin Anda.-- Untuk pengguna perpustakaan OCILIB di proyek Vapor Anda Sertakan yang berikut ini di file Package.swift Anda

    // swift-tools-version:4.0
    import PackageDescription

    let package = Package(
        name: "myAPIProject",
        dependencies: [
            // 💧 A server-side Swift web framework.
            .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),

            // 🔵 Swift ORM (queries, models, relations, etc) built on SQLite 3.
            .package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"),

            //Oracle wrapper for swift
            .package(url: "https://github.com/h1257977/SwiftOracle.git", from: "0.1.7")
        ],
        targets: [
            .target(name: "App", dependencies: ["FluentSQLite","SwiftOracle", "Vapor"]),
            .target(name: "Run", dependencies: ["App"]),
            .testTarget(name: "AppTests", dependencies: ["App"])
        ]
    )

-- Di Routes.swift , sertakan yang berikut ini:

    import Vapor
    import SwiftOracle

    let service = OracleService(host: "192.168.1.12", port:"1521", service: "orcl")
    let b = Connection(service: service, user:"test", pwd:"oracle")

    final class VReq: Content {
        var name: String? 
        var age: String?

        init (NAME: String, AGE: String) {
        self.name = NAME
        self.age = AGE
    }

    final class VMdata {

            func getData() throws ->  [VReq] {
                try! b.open()
                b.autocommit = true
                let cursor = try! b.cursor()
                try! cursor.execute("select * from userlist")
                //iterates each row in the cursor and maps only the values (keys are unique) from the dictionary of each rows, if its nil it will replace with "null" 
                var items = cursor.map { row in row.dict.mapValues { "\($0 ?? "NULL")" }} //  output as [[String:String]]

                //takes each dictionary in the items array and returns a VReq
                let result = items.map { dict in VReq(NAME: dict["NAME"] ?? "NULL", ADDRESS: dict["ADDRESS"] ?? "NULL", USER_AGE: dict["USER_AGE"] ?? "NULL")}
                return result
                }

    }

    public func routes(_ router: Router) throws {

    router.get("test") { req -> [VReq] in

        let val = VMdata()
        let vdata = try! val.getData()
        return vdata

    }
    }

--Untuk menjalankan Vapor 3 Anda perlu menautkan file perpustakaan

swift build -Xlinker -L/usr/local/lib && ./.build/x86_64-unknown-linux/debug/Run --hostname 0.0.0.0

-- Untuk menampilkan karakter unicode dari kolom basis data mana pun, Anda mungkin harus menyetel NLS_LANG di aplikasi server hosting Vapor.

export NLS_LANG=AMERICAN_AMERICA.AL32UTF8



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. PILIH kueri dengan karakter Literal (Colon, Semi-colon) di Oracle

  2. Oracle SQL Loop melalui Rentang Tanggal

  3. oracle pl/sql DBMS_LOCK kesalahan

  4. Kesalahan- ORA-22835:Buffer terlalu kecil untuk konversi CLOB ke CHAR atau BLOB ke RAW

  5. Oracle LIMIT dan batasan 1000 kolom