sql >> Database teknologi >  >> RDS >> Oracle

Tilslutning til Oracle-database ved hjælp af Server Side Swift

du kan bruge følgende metode til at oprette forbindelse til oracle database:(Dette var muligt ved hjælp af vapor community.)

-----for at få Oracle-driveren til at fungere, har jeg bundet denne metode i Ubuntu-------- Oracle-klienten skal installeres, således at overskriften og biblioteksstien kan defineres, du kan få disse fra oracles websted.

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

--installer den således downloadede pakke ved hjælp af følgende kommando

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

-- Installer libaio1 i ubuntu

sudo apt install libaio1

-- denne sti skal være i ~/.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

--download OCILIB-biblioteket fra Githubgit-klonen https://github.com/vrogier/ocilib.git (eller download den seneste version/testet på ocilib 4.5.2)-- udpak ocilib-fil-cd til ocilib-mappen, konfigurer make and 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

-- brug denne konfiguration, hvis du har brug for at håndtere unicodes, generelt har du ikke brug for dette

./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"

-- Ovenstående metode installerer OCILIB på din maskine.-- Til bruger OCILIB-bibliotek i dit Vapor-projekt Inkluder følgende i din Package.swift-fil

    // 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"])
        ]
    )

-- I Routes.swift skal du inkludere følgende:

    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

    }
    }

--For at køre Vapor 3 skal du linke biblioteksfilerne

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

-- For at vise unicode-tegn fra en hvilken som helst databasekolonne, skal du muligvis indstille NLS_LANG i serveren, der hoster Vapor-applikationen.

export NLS_LANG=AMERICAN_AMERICA.AL32UTF8



  1. SQL forbinder 2 tabeller til 1 tabel

  2. Returner en standardværdi, hvis en enkelt række ikke findes

  3. JDBC Metada henter begrænsningsoplysninger

  4. Kan ikke slippe tabel:En fremmednøgle-begrænsning mislykkes