Skip to main content
Version: v4

開發工具

若您要使用程式開發工具連線至 Canner Enterprise 取得資料,以下提供各程式語言建議使用的連線 Driver 、套件及範例程式。

各程式語言建議連線 Driver 或套件

LanguageDriver
JavaJDBC
JavaScriptpg
C# (.NET)Npgsql, ODBC
PHPpg_connect
Pythonpsycopg2

各程式語言連線範例程式

Java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCExample {
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(
"jdbc:postgresql://<host>:7432/<workspace sql name>", "canner", "<token>")) {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM <table>");
while (rs.next()) {
System.out.print("Column 1 returned ");
System.out.println(rs.getString(1));
}
rs.close();
st.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Javascript

import { Client } from 'pg';
const client = new Client({
user: 'canner',
host: '<host>',
database: '<workspace sql name>',
password: '<token>',
port: 7432,
});
await client.connect();

const res = await client.query('SELECT 1 as message');
console.log(res.rows[0].message); // 1
await client.end();

C# (.NET)

using System;
using System.Data;
using System.IO;
using System.Linq;
using Npgsql.Logging;

public void CSHARPExample()
{
using (var conn = new NpgsqlConnection("Host=<host>;Username=canner;Password=<token>;Database=<workspace sql name>"))
{
using (var cmd = new NpgsqlCommand("SELECT * FROM <table>", conn))
{
using (var reader = cmd.ExecuteReader())
{
// use reader to read data
}
}
}
}
using System.Data.Common;
using System.Data;
using System.Data.Odbc;

class Canner
{
static void Main(string[] args)
{

// Setup a connection string
string szConnect = "DRIVER={PostgreSQL UNICODE};" +
"DATABASE=<workspace sql name>;" +
"UID=canner;" +
"PWD=<token>;" +
"SERVER=<host>;" +
"PORT=7432;";

// Attempt to open a connection
OdbcConnection cnDB = new OdbcConnection(szConnect);

// The following code demonstrates how to catch & report an ODBC exception.
// To keep things simple, this is the only exception handling in this example.
// Note: The ODBC data provider requests ODBC3 from the driver. At the time of
// writing, the psqlODBC driver only supports ODBC2.5 - this will cause
// an additional error, but will *not* throw an exception.
try
{
cnDB.Open();
}
catch (OdbcException ex)
{
Console.WriteLine(ex.Message + "\n\n" + "StackTrace: \n\n" + ex.StackTrace);
// Pause for the user to read the screen.
Console.WriteLine("\nPress to continue...");
Console.Read();
return;
}

// Create a dataset
DataSet dsDB = new DataSet();
OdbcDataAdapter adDB = new OdbcDataAdapter();
OdbcCommandBuilder cbDB = new OdbcCommandBuilder(adDB);
adDB.SelectCommand = new OdbcCommand(
"SELECT * FROM <table>",
cnDB);
adDB.Fill(dsDB);

// Display the record count
Console.WriteLine("Table contains {0} rows.\n",
dsDB.Tables[0].Rows.Count);

// List the columns (using a foreach loop)
Console.WriteLine("Columns\n=======\n");

foreach (DataColumn dcDB in dsDB.Tables[0].Columns)
Console.WriteLine("{0} ({1})", dcDB.ColumnName, dcDB.DataType);
Console.WriteLine("\n");

// Iterate through the rows and display the data in the table (using a for loop).
// Display the data column last for readability.
Console.WriteLine("Data\n====\n");
foreach (DataTable table in dsDB.Tables)
{
foreach (DataRow row in table.Rows)
{
Console.WriteLine("nationkey: {0}", row["nationkey"]);
Console.WriteLine("name: {0}", row["name"]);
Console.WriteLine("--");
}
}
}
}

PHP

<?php
// Connecting, selecting database
$dbconn = pg_connect("host=<host> dbname=<workspace sql name> user=canner password=<token>")
or die('Could not connect: ' . pg_last_error());

// Performing SQL query
$query = '"SELECT * FROM tpch.sf1.lineitem limit 10';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());

// Use $result to get query result

// Free resultset
pg_free_result($result);

// Closing connection
pg_close($dbconn);
?>

Python

import psycopg2

# connection properties
host = "<host>"
user = "canner"
password = "<token>"
schema = "<workspace sql name>"

conn = psycopg2.connect(host=host, dbname=schema, user=user, password=password, port="7432")
cursor = conn.cursor()
sql_query = "SELECT * FROM <table>"
cursor.execute(sql_query)
result = cursor.fetchall()
for row in result:
print(row)
cursor.close()
conn.close()